|
|
package com.sunyo.wlpt.message.bus.service.bean;
|
|
|
|
|
|
|
|
|
import com.sunyo.wlpt.message.bus.service.domain.es.ElasticSearchInfo;
|
|
|
import com.sunyo.wlpt.message.bus.service.mapper.ElasticSearchInfoMapper;
|
|
|
import com.sunyo.wlpt.message.bus.service.service.ElasticSearchInfoService;
|
|
|
import org.apache.http.HttpHost;
|
|
|
import org.elasticsearch.client.RestClientBuilder;
|
|
|
import org.elasticsearch.client.RestHighLevelClient;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
...
|
...
|
@@ -9,12 +13,50 @@ import org.springframework.context.annotation.Configuration; |
|
|
import org.springframework.data.elasticsearch.client.ClientConfiguration;
|
|
|
import org.springframework.data.elasticsearch.client.RestClients;
|
|
|
|
|
|
@Configuration
|
|
|
import javax.annotation.Resource;
|
|
|
import java.util.List;
|
|
|
|
|
|
/**
|
|
|
* 这个类作废
|
|
|
* 因为解决了王勇那个初始化ES配置的问题
|
|
|
* 他的问题存在esservice 与 RestHighLevelClient bean之间项目调用的问题。
|
|
|
* 现在将ESSERVICE bean 换成 mapper bean 解决了
|
|
|
*/
|
|
|
//@Configuration
|
|
|
public class EsRestClientConfig {
|
|
|
|
|
|
@Bean(name = "EsHighLevelClient")
|
|
|
/**
|
|
|
* 这里不能用 那个service 存在bean相互调用的问题
|
|
|
*/
|
|
|
@Resource
|
|
|
private ElasticSearchInfoMapper elasticSearchInfoMapper;
|
|
|
|
|
|
|
|
|
// @Bean(name = "EsHighLevelClient")
|
|
|
public RestHighLevelClient highLevelClient() {
|
|
|
final ClientConfiguration clientConfiguration= ClientConfiguration.builder().connectedTo("192.168.1.73:9200").build();
|
|
|
HttpHost[] httpHosts = getHttpHosts();
|
|
|
HttpHost host = httpHosts[0];
|
|
|
final ClientConfiguration clientConfiguration= ClientConfiguration.builder().connectedTo(host.getHostName()+":"+host.getPort()).build();
|
|
|
return RestClients.create(clientConfiguration).rest();
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 获取ES集群信息
|
|
|
*
|
|
|
* @return ES集群信息
|
|
|
*/
|
|
|
public HttpHost[] getHttpHosts()
|
|
|
{
|
|
|
List<ElasticSearchInfo> elasticSearchInfos = elasticSearchInfoMapper.selectList();;
|
|
|
int size = elasticSearchInfos.size();
|
|
|
HttpHost[] httpHosts = new HttpHost[size];
|
|
|
for (int i = 0; i < size; i++) {
|
|
|
String hostname = elasticSearchInfos.get(i).getHostname();
|
|
|
Integer port = elasticSearchInfos.get(i).getPort();
|
|
|
String scheme = elasticSearchInfos.get(i).getScheme();
|
|
|
httpHosts[i] = new HttpHost(hostname, port, scheme);
|
|
|
}
|
|
|
return httpHosts;
|
|
|
}
|
|
|
|
|
|
} |
...
|
...
|
|