基础条件:服务器安装好docker和docker-compose环境
一、eleasticsearch docker-compose.yml文件编写 单机版本
version: '2'
services:
# elasticsearch
elasticsearch:
networks:
ycl_network:
ipv4_address: 172.18.1.10
image: elasticsearch:6.8.8
container_name: elasticsearch # docker启动后的名称
privileged: true
restart: always #出现异常自动重启
ports:
- 9200:9200
- 9300:9300
environment:
cluster.name: elasticsearch # es 名称
ES_JAVA_OPTS: "-Xms512m -Xmx512m"
volumes:
- "/usr/local/docker/elasticsearch/data:/usr/share/elasticsearch/data"
networks:
ycl_network:
external: true
然后执行docker-compose up -d
##6.0版本后http请求存在跨域问题 和认证问题 修改elasticsearch.yml文件
http.cors.enabled: true
http.cors.allow-origin: "*"
http.cors.allow-headers: Authorization,Content-Type
###重要### x-pack 安全认证配置
curl -H "Content-Type:application/json" -XPOST http://127.0.0.1:9200/_xpack/license/start_trial?acknowledge=true
#重启docker容器后 并进入docker容器 修改elasticsearch.yml文件
xpack.security.enabled: true
#然后进入docker容器设置密码
docker exec it elasticsearch /bin/sh
bin/elasticsearch-setup-passwords interactive
复制代码
二、eleasticsearch-head 安装 docker-compose.yml
version: '2'
services:
# elasticsearch-head
elasticsearch-head:
networks:
ycl_network:
ipv4_address: 172.18.1.30
image: mobz/elasticsearch-head:5
container_name: elasticsearch-head # docker启动后的名称
restart: always #出现异常自动重启
ports:
- 9100:9100
networks:
ycl_network:
external: true
#备注 由于eleasticsearch6之后的版本增加了安全校验,所以容器启动后要改动一下http请求的header
#先进入容器
vim _site/vendor.js
6886行: /contentType: “application/x-www-form-urlencoded改成
contentType: "application/json;charset=UTF-8"
7573行: var inspectData = s.contentType === “application/x-www-form-urlencoded” && 改成
var inspectData = s.contentType === "application/json;charset=UTF-8" &&
#es最终配置如下才能访问
http.cors.enabled: true
http.cors.allow-origin: "*"
xpack.security.enabled: true
http.cors.allow-headers: Authorization,Content-Type
#访问地址
http://192.168.8.13:9100/?auth_user=elastic&auth_password=mima
复制代码
三、springboot集成
#引入依赖包
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
<exclusions>
<exclusion>
<groupId>org.elasticsearch.client</groupId>
<artifactId>transport</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>x-pack-transport</artifactId>
<version>6.4.3</version>
</dependency>
#配置中央仓库
<repositories>
<repository>
<id>elasticsearch-releases</id>
<url>https://artifacts.elastic.co/maven</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
#yml配置
spring:
data:
elasticsearch:
cluster-name: elasticsearch
cluster-nodes: 192.168.8.13:9300
properties:
xpack.security.user: elastic:XXXXX
#重写elasticsearch client配置
/**
* @author huzhihui
* @version $ v 0.1 2020/4/20 15:11 huzhihui Exp $$
*/
@Configuration
@EnableConfigurationProperties(ElasticsearchProperties.class)
public class ElasticsearchConfig {
private final ElasticsearchProperties properties;
public ElasticsearchConfig(ElasticsearchProperties properties) {
this.properties = properties;
}
@Bean
public TransportClient transportClient(){
return new PreBuiltXPackTransportClient(settings())
.addTransportAddresses(addresses());
}
/**
* .put("client.transport.sniff", true)
* .put("client.transport.ignore_cluster_name", false)
* .put("client.transport.ping_timeout", clientPingTimeout)
* .put("client.transport.nodes_sampler_interval", clientNodesSamplerInterval)
*
* @return Settings
*/
private Settings settings() {
Settings.Builder builder = Settings.builder();
builder.put("cluster.name", properties.getClusterName());
properties.getProperties().forEach(builder::put);
return builder.build();
}
private TransportAddress[] addresses() {
String clusterNodesStr = properties.getClusterNodes();
Assert.hasText(clusterNodesStr, "Cluster nodes source must not be null or empty!");
String[] nodes = StringUtils.delimitedListToStringArray(clusterNodesStr, ",");
return Arrays.stream(nodes).map(node -> {
String[] segments = StringUtils.delimitedListToStringArray(node, ":");
Assert.isTrue(segments.length == 2,
() -> String.format("Invalid cluster node %s in %s! Must be in the format host:port!", node, clusterNodesStr));
String host = segments[0].trim();
String port = segments[1].trim();
Assert.hasText(host, () -> String.format("No host name given cluster node %s!", node));
Assert.hasText(port, () -> String.format("No port given in cluster node %s!", node));
return new TransportAddress(toInetAddress(host), Integer.valueOf(port));
}).toArray(TransportAddress[]::new);
}
private static InetAddress toInetAddress(String host) {
try {
return InetAddress.getByName(host);
} catch (UnknownHostException ex) {
throw new IllegalArgumentException(ex);
}
}
}
复制代码
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END