ElasticSearch多源配置
直接上代码
yml文件配置信息
elasticsearch: works: #单篇稿件的ES库配置信息 node1: http://***.**.***.***:9201 #localhost:9200 node2: http://***.**.***.***:9202 node3: http://***.**.***.***:9203 connTimeout: 30000 # 连接超时时间 socketTimeout: 30000 # socket超时时间 connectionRequestTimeout: 30000 # 链接请求超时时间 index: spreadimpact #索引 maxConnectNum: 100 #最大连接数 maxConnectPerRoute: 100 #最大路由连接数 yq: #全网数据的ES库配置信息 # es连接地址和端口 host: ***.***.***.*** # es端口 port: 9200 # 连接超时时间 connTimeout: 10000 # socket超时时间 socketTimeout: 10000 # 链接请求超时时间 connectionRequestTimeout: 20000 # 索引 index: nday_test
1.SystemConfiguration类
@Slf4j @Configuration @Getter public class SystemConfiguration { @Value("${elasticsearch.works.node1}") private String node1; @Value("${elasticsearch.works.node2}") private String node2; @Value("${elasticsearch.works.node3}") private String node3; @Value("${elasticsearch.works.connTimeout}") private int connTimeout; @Value("${elasticsearch.works.socketTimeout}") private int socketTimeout; @Value("${elasticsearch.works.connectionRequestTimeout}") private int connectionRequestTimeout; /**最大连接数*/ @Value("${elasticsearch.works.maxConnectNum}") private int maxConnectNum; /** 最大路由连接数 */ @Value("${elasticsearch.works.maxConnectPerRoute}") private int maxConnectPerRoute; /** *下面是专题活动ES库的配置信息 */ @Value("${elasticsearch.yq.host}") private String host; @Value("${elasticsearch.yq.port}") private int port; @Value("${elasticsearch.yq.connTimeout}") private int connectTimeOut; @Value("${elasticsearch.yq.socketTimeout}") private int socketTimeOut; @Value("${elasticsearch.yq.connectionRequestTimeout}") private int connectionRequestTimeOut; }
2.SpringEsConfig类
@Slf4j @Configuration public class ElasticSearchConfiguration { @Autowired SystemConfiguration sysConfig; /** * * @return RestHighLevelClient */ @Bean(destroyMethod = "close", name = "client") public RestHighLevelClient initRestClient1() { RestClientBuilder builder = RestClient.builder( HttpHost.create(sysConfig.getNode1()), HttpHost.create(sysConfig.getNode2()), HttpHost.create(sysConfig.getNode3())); builder.setRequestConfigCallback(requestConfigBuilder -> requestConfigBuilder .setConnectTimeout(sysConfig.getConnTimeout()) .setSocketTimeout(sysConfig.getSocketTimeout()) .setConnectionRequestTimeout(sysConfig.getConnectionRequestTimeout())); builder.setHttpClientConfigCallback(httpClientBuilder -> httpClientBuilder.setMaxConnTotal(sysConfig.getMaxConnectNum()) .setMaxConnPerRoute(sysConfig.getMaxConnectPerRoute())); return new RestHighLevelClient(builder); } /** * * @return RestHighLevelClient */ @Bean(destroyMethod = "close", name = "yq-client") public RestHighLevelClient initRestClient2() { RestClientBuilder builder = RestClient.builder(new HttpHost(sysConfig.getHost(), sysConfig.getPort())) .setRequestConfigCallback(requestConfigBuilder -> requestConfigBuilder .setConnectTimeout(sysConfig.getConnectTimeOut()) .setSocketTimeout(sysConfig.getSocketTimeOut()) .setConnectionRequestTimeout(sysConfig.getConnectionRequestTimeout())); return new RestHighLevelClient(builder); }
3. ESHighClient注入使用
@Service public class CblWorksServiceImpl implements ICblWorksService { private final RestHighLevelClient restHighLevelClient; public CblWorksServiceImpl(@Qualifier("yq-client") RestHighLevelClient restHighLevelClient) {//这里使用@Qualifier注解通过名称进行替换实例对象 this.restHighLevelClient = restHighLevelClient; } //此处进行client的使用 @Override public AreaForwardOrderListVO getAreaForwardOrderList(String title) { //1.创建请求 SearchRequest searchRequest = new SearchRequest(index); //2.组合查询条件 BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery(); boolQueryBuilder.must(QueryBuilders.matchPhraseQuery("title", title)); //3.条件放入到request中 searchSourceBuilder.query(boolQueryBuilder); searchRequest.source(searchSourceBuilder); SearchResponse searchResponse = null; List<String> xxData = new ArrayList<>(); List<Integer> yyData = new ArrayList<>(); AreaForwardOrderListVO vo = new AreaForwardOrderListVO(); try { searchResponse = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT); if (searchResponse != null) { SearchHit[] searchHits = searchResponse.getHits().getHits(); //下面进行相关的数据处理 ... } } return vo;
} }
参考:https://my.oschina.net/u/3795437/blog/3031851
浙公网安备 33010602011771号