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

image

esrepository

1 public interface RawMessageRepository extends ElasticsearchRepository<RawMessageDto, String> {
2 
3 }

Dto,使用@Document注解進行實體映射,相當於Mysql的@Table注解

image


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM