transport方式连接Elasticsearch6.3
作者:互联网
连接ES有3中方式:
①transport方式
②rest方式
③JEST方式(第三方)
我自己项目使用第一种方式,代码和配置如下:
1、引入依赖
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>6.2.3</version>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>transport</artifactId>
<version>6.2.3</version>
<exclusions>
<exclusion>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
</exclusion>
</exclusions>
</dependency>
2、配置
es.cluster-name=csmses
es.ip=30.23.8.172,30.23.8.173,30.23.8.174,30.23.8.175,30.23.8.176
es.port=9300
es.pool=5
3、代码
/**
* TransportClient方式连接ES
* @author wangymd
*
*/
@Configuration
public class EsConfig {
private Log log = LogFactory.getLog(EsConfig.class);
@Autowired
private Environment env;
@Bean
public TransportClient transportClient() throws UnknownHostException{
Settings settings = Settings.builder()
.put("cluster.name", env.getProperty("es.cluster-name"))
.put("client.transport.sniff", true)
.put("thread_pool.search.size", Integer.valueOf(env.getProperty("es.pool")))
.build();
TransportClient esClient = new PreBuiltTransportClient(settings);
String ipStr = env.getProperty("es.ip");//多个IP
String port = env.getProperty("es.port");//端口一致9300
log.info("@@@@@@@@TransportClient方式连接ES@@@@@@@@。IP:" + ipStr + ", PORT:" + port);
String[] ips = ipStr.split(",");
for (String ip : ips) {
if(StringUtils.isNotEmpty(ip) && RegExpValidatorUtils.isIP(ip)){
TransportAddress transportAddress = new TransportAddress(InetAddress.getByName(ip), Integer.valueOf(port));
esClient.addTransportAddress(transportAddress);
}
}
return esClient;
}
}
标签:Elasticsearch6.3,ip,连接,30.23,elasticsearch,transport,env,port,es 来源: https://www.cnblogs.com/wangymd/p/11264172.html