docker pull elasticsearch:6.8.82、创建配置文件存放目录
cd ~/es-cluster mkdir es1 mkdir es23、创建配置文件
cd es1 vim elasticsearch.yml
添加以下内容
cluster.name: my-elasticsearch #集群名称 network.host: 0.0.0.0 node.name: node-1 #节点名称 #集群节点 discovery.zen.ping.unicast.hosts: ["120.25.237.2:9301", "120.25.237.2:9302"] #配置跨域 http.cors.enabled: true http.cors.allow-origin: "*" #端口 http.port: 9201 transport.tcp.port: 9301
cd ../es2 vim elasticsearch.yml
添加以下内容
cluster.name: my-elasticsearch #集群名称 network.host: 0.0.0.0 node.name: node-2 #节点名称 #集群节点 discovery.zen.ping.unicast.hosts: ["120.25.237.2:9301", "120.25.237.2:9302"] #配置跨域 http.cors.enabled: true http.cors.allow-origin: "*" #端口 http.port: 9202 transport.tcp.port: 93024、运行容器
在es-cluster目录下执行
cd ~/es-cluster
docker run -id --name=es1 -e "ES_JAVA_OPTS=-Xms128m -Xmx128m" -p 9201:9201 -p 9301:9301 -v $PWD/es1/elasticsearch.yml:/usr/share/elasticsearch/config/elasticsearch.yml elasticsearch:6.8.8 --------------------------------------- docker run -id --name=es2 -e "ES_JAVA_OPTS=-Xms128m -Xmx128m" -p 9202:9202 -p 9302:9302 -v $PWD/es2/elasticsearch.yml:/usr/share/elasticsearch/config/elasticsearch.yml elasticsearch:6.8.8
-e “ES_JAVA_OPTS=-Xms128m -Xmx128m”:指定内存,es默认启动所需最小内存为2G。
查看是否启动成功
docker ps
在浏览器地址栏输入
http://120.25.237.2:9201/_cat/nodes
或者是通过elasticsearch-head查看
5、添加ik分词器下载地址:https://github.com/medcl/elasticsearch-analysis-ik/releases
一定要找与安装的es版本相同的ik分词器版本,否则容器会启动失败
这里,我装的es版本是6.8.8,所以下载的ik分词器版本也是6.8.8的
解压,之后将所有文件移动到ik文件夹中
tar -zxvf elasticsearch-analysis-ik-6.8.8.zip mkdir ik
将ik文件夹复制到容器中
docker cp ./ik/ es1:/usr/share/elasticsearch/plugins docker cp ./ik/ es2:/usr/share/elasticsearch/plugins
将容器重启
docker restart es1 docker restart es2二、测试
这里使用spring boot整合elasticsearch
1、pom.xml文件2、配置文件org.springframework.boot spring-boot-starter-parent 2.0.4.RELEASE org.springframework.boot spring-boot-starter-data-elasticsearch org.springframework.boot spring-boot-starter-test
spring:
data:
elasticsearch:
cluster-name: my-elasticsearch #集群名称
cluster-nodes: 120.25.237.2:9301,120.25.237.2:9302 #集群地址
3、实体类
@document(indexName = "sb_kiu",type = "ki")
public class Article {
@Id
@Field(type = FieldType.Integer, store = true)
private Integer id;
@Field(type = FieldType.Text, store = true, analyzer = "ik_smart")
private String title;
@Field(type = FieldType.Text, store = true, analyzer = "ik_smart")
private String content;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
@Override
public String toString() {
return "Article{" +
"id=" + id +
", title='" + title + ''' +
", content='" + content + ''' +
'}';
}
}
4、repository接口
public interface ArticleRepository extends ElasticsearchRepository5、添加索引{ }
@RunWith(SpringRunner.class)
@SpringBootTest
public class EsDemo01BasicTest {
@Autowired
private ArticleRepository repository;
@Autowired
private ElasticsearchTemplate template;
@Test
public void testCreateIndex() {
//创建索引并配置映射关系
template.createIndex(Article.class);
}
}
通过elasticsearch-head查看
连接9201
连接9202
可以看到两个es都已经有了索引库
6、添加数据@RunWith(SpringRunner.class)
@SpringBootTest
public class EsDemo01BasicTest {
@Autowired
private ArticleRepository repository;
@Autowired
private ElasticsearchTemplate template;
@Test
public void testAdddocument() {
Article airticle = new Article();
airticle.setId(1);
airticle.setTitle("女护士路遇昏迷男子跪地抢救:救人是职责更是本能");
airticle.setContent("这是一个美丽的女护士妹妹");
//添加文档
repository.save(airticle);
}
}
通过elasticsearch-head查看
连接9201
连接9202
两个es里都有了数据



