解决王勇初始化es 配置类 service bean与 ES HIGHLEAVEL bean 相互调用的问题导致程序启动不来
正在显示
3 个修改的文件
包含
50 行增加
和
7 行删除
| 1 | package com.sunyo.wlpt.message.bus.service.bean; | 1 | package com.sunyo.wlpt.message.bus.service.bean; |
| 2 | 2 | ||
| 3 | 3 | ||
| 4 | +import com.sunyo.wlpt.message.bus.service.domain.es.ElasticSearchInfo; | ||
| 5 | +import com.sunyo.wlpt.message.bus.service.mapper.ElasticSearchInfoMapper; | ||
| 6 | +import com.sunyo.wlpt.message.bus.service.service.ElasticSearchInfoService; | ||
| 7 | +import org.apache.http.HttpHost; | ||
| 4 | import org.elasticsearch.client.RestClientBuilder; | 8 | import org.elasticsearch.client.RestClientBuilder; |
| 5 | import org.elasticsearch.client.RestHighLevelClient; | 9 | import org.elasticsearch.client.RestHighLevelClient; |
| 6 | import org.springframework.beans.factory.annotation.Autowired; | 10 | import org.springframework.beans.factory.annotation.Autowired; |
| @@ -9,12 +13,50 @@ import org.springframework.context.annotation.Configuration; | @@ -9,12 +13,50 @@ import org.springframework.context.annotation.Configuration; | ||
| 9 | import org.springframework.data.elasticsearch.client.ClientConfiguration; | 13 | import org.springframework.data.elasticsearch.client.ClientConfiguration; |
| 10 | import org.springframework.data.elasticsearch.client.RestClients; | 14 | import org.springframework.data.elasticsearch.client.RestClients; |
| 11 | 15 | ||
| 12 | -@Configuration | 16 | +import javax.annotation.Resource; |
| 17 | +import java.util.List; | ||
| 18 | + | ||
| 19 | +/** | ||
| 20 | + * 这个类作废 | ||
| 21 | + * 因为解决了王勇那个初始化ES配置的问题 | ||
| 22 | + * 他的问题存在esservice 与 RestHighLevelClient bean之间项目调用的问题。 | ||
| 23 | + * 现在将ESSERVICE bean 换成 mapper bean 解决了 | ||
| 24 | + */ | ||
| 25 | +//@Configuration | ||
| 13 | public class EsRestClientConfig { | 26 | public class EsRestClientConfig { |
| 14 | 27 | ||
| 15 | - @Bean(name = "EsHighLevelClient") | 28 | + /** |
| 29 | + * 这里不能用 那个service 存在bean相互调用的问题 | ||
| 30 | + */ | ||
| 31 | + @Resource | ||
| 32 | + private ElasticSearchInfoMapper elasticSearchInfoMapper; | ||
| 33 | + | ||
| 34 | + | ||
| 35 | +// @Bean(name = "EsHighLevelClient") | ||
| 16 | public RestHighLevelClient highLevelClient() { | 36 | public RestHighLevelClient highLevelClient() { |
| 17 | - final ClientConfiguration clientConfiguration= ClientConfiguration.builder().connectedTo("192.168.1.73:9200").build(); | 37 | + HttpHost[] httpHosts = getHttpHosts(); |
| 38 | + HttpHost host = httpHosts[0]; | ||
| 39 | + final ClientConfiguration clientConfiguration= ClientConfiguration.builder().connectedTo(host.getHostName()+":"+host.getPort()).build(); | ||
| 18 | return RestClients.create(clientConfiguration).rest(); | 40 | return RestClients.create(clientConfiguration).rest(); |
| 19 | } | 41 | } |
| 42 | + | ||
| 43 | + /** | ||
| 44 | + * 获取ES集群信息 | ||
| 45 | + * | ||
| 46 | + * @return ES集群信息 | ||
| 47 | + */ | ||
| 48 | + public HttpHost[] getHttpHosts() | ||
| 49 | + { | ||
| 50 | + List<ElasticSearchInfo> elasticSearchInfos = elasticSearchInfoMapper.selectList();; | ||
| 51 | + int size = elasticSearchInfos.size(); | ||
| 52 | + HttpHost[] httpHosts = new HttpHost[size]; | ||
| 53 | + for (int i = 0; i < size; i++) { | ||
| 54 | + String hostname = elasticSearchInfos.get(i).getHostname(); | ||
| 55 | + Integer port = elasticSearchInfos.get(i).getPort(); | ||
| 56 | + String scheme = elasticSearchInfos.get(i).getScheme(); | ||
| 57 | + httpHosts[i] = new HttpHost(hostname, port, scheme); | ||
| 58 | + } | ||
| 59 | + return httpHosts; | ||
| 60 | + } | ||
| 61 | + | ||
| 20 | } | 62 | } |
| @@ -2,6 +2,7 @@ package com.sunyo.wlpt.message.bus.service.config; | @@ -2,6 +2,7 @@ package com.sunyo.wlpt.message.bus.service.config; | ||
| 2 | 2 | ||
| 3 | import com.sunyo.wlpt.message.bus.service.domain.es.ElasticSearchInfo; | 3 | import com.sunyo.wlpt.message.bus.service.domain.es.ElasticSearchInfo; |
| 4 | import com.sunyo.wlpt.message.bus.service.domain.es.ElasticSearchProperties; | 4 | import com.sunyo.wlpt.message.bus.service.domain.es.ElasticSearchProperties; |
| 5 | +import com.sunyo.wlpt.message.bus.service.mapper.ElasticSearchInfoMapper; | ||
| 5 | import com.sunyo.wlpt.message.bus.service.service.ElasticSearchInfoService; | 6 | import com.sunyo.wlpt.message.bus.service.service.ElasticSearchInfoService; |
| 6 | import lombok.RequiredArgsConstructor; | 7 | import lombok.RequiredArgsConstructor; |
| 7 | import org.apache.http.HttpHost; | 8 | import org.apache.http.HttpHost; |
| @@ -32,7 +33,7 @@ import java.util.List; | @@ -32,7 +33,7 @@ import java.util.List; | ||
| 32 | public class ElasticSearchConfig extends AbstractElasticsearchConfiguration { | 33 | public class ElasticSearchConfig extends AbstractElasticsearchConfiguration { |
| 33 | 34 | ||
| 34 | @Resource | 35 | @Resource |
| 35 | - private ElasticSearchInfoService elasticSearchInfoService; | 36 | + private ElasticSearchInfoMapper elasticSearchInfoMapper; |
| 36 | 37 | ||
| 37 | private final ElasticSearchProperties elasticSearchProperties; | 38 | private final ElasticSearchProperties elasticSearchProperties; |
| 38 | 39 | ||
| @@ -43,7 +44,7 @@ public class ElasticSearchConfig extends AbstractElasticsearchConfiguration { | @@ -43,7 +44,7 @@ public class ElasticSearchConfig extends AbstractElasticsearchConfiguration { | ||
| 43 | */ | 44 | */ |
| 44 | public HttpHost[] getHttpHosts() | 45 | public HttpHost[] getHttpHosts() |
| 45 | { | 46 | { |
| 46 | - List<ElasticSearchInfo> elasticSearchInfos = elasticSearchInfoService.selectList(); | 47 | + List<ElasticSearchInfo> elasticSearchInfos = elasticSearchInfoMapper.selectList(); |
| 47 | int size = elasticSearchInfos.size(); | 48 | int size = elasticSearchInfos.size(); |
| 48 | HttpHost[] httpHosts = new HttpHost[size]; | 49 | HttpHost[] httpHosts = new HttpHost[size]; |
| 49 | for (int i = 0; i < size; i++) { | 50 | for (int i = 0; i < size; i++) { |
| @@ -57,7 +58,7 @@ public class ElasticSearchConfig extends AbstractElasticsearchConfiguration { | @@ -57,7 +58,7 @@ public class ElasticSearchConfig extends AbstractElasticsearchConfiguration { | ||
| 57 | 58 | ||
| 58 | 59 | ||
| 59 | @Override | 60 | @Override |
| 60 | -// @Bean | 61 | + @Bean |
| 61 | public RestHighLevelClient elasticsearchClient() | 62 | public RestHighLevelClient elasticsearchClient() |
| 62 | { | 63 | { |
| 63 | final CredentialsProvider credentialsProvider = new BasicCredentialsProvider(); | 64 | final CredentialsProvider credentialsProvider = new BasicCredentialsProvider(); |
| @@ -48,7 +48,7 @@ public class ElasticSearchInfoServiceImpl implements ElasticSearchInfoService { | @@ -48,7 +48,7 @@ public class ElasticSearchInfoServiceImpl implements ElasticSearchInfoService { | ||
| 48 | private ElasticSearchInfoMapper elasticSearchInfoMapper; | 48 | private ElasticSearchInfoMapper elasticSearchInfoMapper; |
| 49 | 49 | ||
| 50 | 50 | ||
| 51 | - @Qualifier("EsHighLevelClient") | 51 | +// @Qualifier("EsHighLevelClient") |
| 52 | @Resource | 52 | @Resource |
| 53 | private RestHighLevelClient restHighLevelClient; | 53 | private RestHighLevelClient restHighLevelClient; |
| 54 | 54 |
-
请 注册 或 登录 后发表评论