docker pull elasticsearch:6.8.8
cd ~/es-cluster
mkdir es1
mkdir es2
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: 9302
在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查看
下载地址: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
<parent>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-parentartifactId>
<version>2.0.4.RELEASEversion>
parent>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-data-elasticsearchartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
dependency>
dependencies>
spring:
data:
elasticsearch:
cluster-name: my-elasticsearch #集群名称
cluster-nodes: 120.25.237.2:9301,120.25.237.2:9302 #集群地址
@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 + '\'' +
'}';
}
}
public interface ArticleRepository extends ElasticsearchRepository<Article,Integer> {
}
@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都已经有了索引库
@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