ElasticSearch6.8.1集群搭建及Java客户端编写

一、环境准备

1>准备三台centos7.2(本次实验环境),

192.168.105.55

192.168.105.56

192.168.105.57

并按装JDK1.8

创建用户和组 elastic

groupadd   elastic

adduser -g elastic -d /home/elastic   elastic

2>必须要的系统配置:

/etc/security/limits.conf

*  -  nofile  65535

/etc/sysctl.conf

vm.max_map_count = 262144

要执行sysctl -p 持久化配置,不然切换用户后,可能没有生效

二、安装配置

1、上传elasticsearch-6.8.1.tar.gz包只三台服务器/home/elastic目录下,并创建目录/elastic/data 和/elastic/log

2、解压安装包 tar -xzvf elasticsearch-6.8.1.tar.gz,并将文件夹重命名为elasticsearch

3、修改配置文件elasticsearch/config/elasticsearch.yml

三台分别的node-1,node-2,node-3;

network.host分别为三台机器的ip

cluster.name: appEsCls
node.name: node-1
path.data: /home/elastic/elastic/data
path.logs: /home/elastic/elastic/log
network.host: 192.168.105.55
http.port: 9200
discovery.zen.ping.unicast.hosts: ["192.168.105.55", "192.168.105.56","192.168.105.57"]
gateway.recover_after_nodes: 3

然后启动集群

三台机器分别执行elasticsearch/bin/elasticsearch -d (-d表示后台执行,可以不用)

启动成功后查看日志: /home/elastic/log/appEsCls.log

分别有[node-X] started

然后查看集群状态及节点状态(可以分别查看各个节点的情况)

curl  http://192.168.105.55:9200/_cat/health?v

curl  http:/192.168.105.57:9200/_cat/nodes?v

master下面的*表示该节点为master节点

三、x-pack设置elasticsearch安全访问

 

1.任意一台服务器上执行命令

 

./elasticsearch-certgen
#####################################
Please enter the desired output file [certificate-bundle.zip]: cert.zip (压缩包名称)
Enter instance name: appEsCls(实例名)
Enter name for directories and files [p4mES]: elasticsearch(文件夹名)
Enter IP Addresses for instance (comma-separated if more than one) []: 192.168.105.55,192.168.105.56,192.168.105.57(实例ip,多个ip用逗号隔开)
Enter DNS names for instance (comma-separated if more than one) []: node-1,node-2,node-3(节点名,多个节点用逗号隔开)
Would you like to specify another instance? Press 'y' to continue entering instance information: (到达这一步,不需要按y重新设置,按空格键就完成了)
Certificates written to /home/elastic/elasticsearch/bin/cert.zip(这个是生成的文件存放地址,不用填写)

 


2. 将压缩文件cert.zip分别拷贝纸三台机器的 /home/elastic/elasticsearch/config文件夹下并解压,
生成ca和elasticsearch并修改配置文件elasticsearch.yml
增加如下配置:
xpack.security.enabled: true
xpack.security.transport.ssl.enabled: true
xpack.ssl.key: elasticsearch/elasticsearch.key
xpack.ssl.certificate: elasticsearch/elasticsearch.crt
xpack.ssl.certificate_authorities: ca/ca.crt

 

3. 重启三台节点

执行elasticsearch/bin/elasticsearch-setup-passwords interactive

自定义设置elastic、kibana....等所有工具的登录密码 最高级账号elastic 可以登录所有组件

然后再重启三台节点

这时,执行curl命令则需要验证密码了

curl  http://192.168.105.57:9200?pretty

curl -u elastic:123456 http://192.168.105.57:9200

四、Java客户端编写

1、普通客户端程序

pom.xml:

<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>${elasticsearch.version}</version>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>transport</artifactId>
<version>${elasticsearch.version}</version>
</dependency>

<dependency>
<groupId>org.elasticsearch.plugin</groupId>
<artifactId>transport-netty4-client</artifactId>
<version>${elasticsearch.version}</version>
</dependency>

code:

 

@Configuration
public class ElasticSearchClientConfig {

@Value("${elasticsearch.cluster-nodes}")
private String clusterNodes;

@Value("${elasticsearch.cluster-name}")
private String clusterName;

@Bean
public Client client() {
Settings settings = Settings.builder().put("cluster.name", clusterName)
.put("client.transport.sniff", true).build();

TransportClient client = new PreBuiltTransportClient(settings);
try {
if (clusterNodes != null && !"".equals(clusterNodes)) {
for (String node : clusterNodes.split(",")) {
String[] nodeInfo = node.split(":");
client.addTransportAddress(new TransportAddress(InetAddress.getByName(nodeInfo[0]), Integer.parseInt(nodeInfo[1])));
}
}
} catch (UnknownHostException e) {
}

return client;
}
}

 

2、带X-PACK授权控制的客户端编写

pom.xml:

<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>${elasticsearch.version}</version>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>x-pack-transport</artifactId>
<version>${elasticsearch.version}</version>
</dependency>

<dependency>
<groupId>org.elasticsearch.plugin</groupId>
<artifactId>transport-netty4-client</artifactId>
<version>${elasticsearch.version}</version>
</dependency>
注意:version 为6.8.1的x-pack-transport的jar可能无法下载,需要添加repository:
https://artifacts.elastic.co/maven
<profiles>
<profile>
<id>dev</id>
<repositories>
<repository> <!-- 增加elastic仓库 -->
<id>elasticsearch-releases</id>
<url>https://artifacts.elastic.co/maven</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
</pluginRepositories>
</profile>
</profiles>

code:
@Configuration
public class ElasticSearchClientConfig{

@Value("${elasticsearch.cluster-nodes}")
private String clusterNodes;

@Value("${elasticsearch.cluster-name}")
private String clusterName;
@Value("${elasticsearch.user-password}")
private String userPwd;

@Bean
public Client client() {
Settings settings = Settings.builder().put("cluster.name", clusterName)
.put("xpack.security.user", userPwd)
.put("xpack.ssl.key", "E:/elasticsearch/elasticsearch.key")
.put("xpack.ssl.certificate", "E:/
elasticsearch/elasticsearch.crt")

.put("xpack.ssl.certificate_authorities", "E:/ca/ca.crt")
.put("xpack.security.transport.ssl.enabled", "true").build();
        TransportClient client = new PreBuiltXPackTransportClient(settings);
try {
if (clusterNodes != null && !"".equals(clusterNodes)) {
for (String node : clusterNodes.split(",")) {
String[] nodeInfo = node.split(":");
client.addTransportAddress(new TransportAddress(InetAddress.getByName(nodeInfo[0]), Integer.parseInt(nodeInfo[1])));
}
}
} catch (UnknownHostException e) {
}

return client;
}
}




转载于:https://www.cnblogs.com/fred925/p/11181414.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值