SpringBoot+ElasticSearch7.x+JPA配置多数据源
Posted 胡玉坤
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SpringBoot+ElasticSearch7.x+JPA配置多数据源相关的知识,希望对你有一定的参考价值。
SpringBoot+ElasticSearch7.x+JPA配置多数据源
这里分为测试环境配置和正式环境配置,大体相差无几
ESTestConfig
1 @Configuration
2 @ConfigurationProperties(prefix = "es-config")
3 @EnableElasticsearchRepositories(basePackages = {"com.tingyu.cdssdatatest.modules.test.*.esrepository"},elasticsearchTemplateRef = "testTemplate")
4 public class ESTestConfig extends AbstractElasticsearchConfiguration {
5
6 @Value("${es-config.hostAndPortTest}")
7 private String hostAndPortTest;
8
9 /**
10 * @description: 测试环境ES
11 */
12 @Bean("elasticsearchClientTest")
13 @Override
14 public RestHighLevelClient elasticsearchClient() {
15 ClientConfiguration configuration = ClientConfiguration.builder()
16 .connectedTo(hostAndPortTest).build();
17 return RestClients.create(configuration).rest();
18 }
19
20 @Override
21 @Bean(name = { "testOperations", "testTemplate" })
22 public ElasticsearchOperations elasticsearchOperations(ElasticsearchConverter elasticsearchConverter,
23 @Qualifier("elasticsearchClientTest") RestHighLevelClient elasticsearchClient) {
24 return new ElasticsearchRestTemplate(elasticsearchClient, elasticsearchConverter);
25 }
26 }
ESOfficialConfig,这里使用了@Primary注解
1 @Configuration
2 @ConfigurationProperties(prefix = "es-config")
3 @EnableElasticsearchRepositories(basePackages = {"com.tingyu.cdssdatatest.modules.official.*.esrepository"},elasticsearchTemplateRef="officialTemplate")
4 public class ESOfficialConfig extends AbstractElasticsearchConfiguration {
5 @Value("${es-config.hostAndPortOfficial}")
6 private String hostAndPortOfficial;
7
8 /**
9 * @description: 正式环境ES
10 */
11 @Bean("elasticsearchClientOfficial")
12 @Primary
13 @Override
14 public RestHighLevelClient elasticsearchClient() {
15 ClientConfiguration configuration = ClientConfiguration.builder()
16 .connectedTo(hostAndPortOfficial).build();
17 return RestClients.create(configuration).rest();
18 }
19
20 @Override
21 @Bean(name = { "officialOperations", "officialTemplate" })
22 public ElasticsearchOperations elasticsearchOperations(ElasticsearchConverter elasticsearchConverter,
23 @Qualifier("elasticsearchClientOfficial") RestHighLevelClient elasticsearchClient) {
24 return new ElasticsearchRestTemplate(elasticsearchClient, elasticsearchConverter);
25 }
26 }
application.yml
esrepository
1 public interface RawMessageRepository extends ElasticsearchRepository<RawMessageDto, String> {
2
3 }
以上是关于SpringBoot+ElasticSearch7.x+JPA配置多数据源的主要内容,如果未能解决你的问题,请参考以下文章
SpringBoot整合ElasticSearch7.x及实战
SpringBoot+ElasticSearch7.x+JPA配置多数据源