Java 单个API,多个Elasticsearch实例
我们有一个Spring Boot Restful API,它需要从2个不同的Elasticsearch实例(在不同的服务器上)获取数据,1个用于“共享”数据(上面有大约5个不同的索引),1个用于“私有”数据(大约有3个不同的索引)。目前只针对“私有”数据实例运行,一切都很好。但我们现在需要获取“共享”数据 在我们的Spring Boot应用程序中,我们启用了如下Elasticsearch存储库Java 单个API,多个Elasticsearch实例,java,spring,
elasticsearch,Java,Spring,
elasticsearch,我们有一个Spring Boot Restful API,它需要从2个不同的Elasticsearch实例(在不同的服务器上)获取数据,1个用于“共享”数据(上面有大约5个不同的索引),1个用于“私有”数据(大约有3个不同的索引)。目前只针对“私有”数据实例运行,一切都很好。但我们现在需要获取“共享”数据 在我们的Spring Boot应用程序中,我们启用了如下Elasticsearch存储库 @SpringBootApplication @EnableElasticsearchReposito
@SpringBootApplication
@EnableElasticsearchRepositories(basePackages = {
"com.company.core.repositories", //<- private repos here...
"com.company.api.repositories" //<-- shared repos here...
})
public class Application { //... }
现在我们要添加另一个存储库,如:
package com.company.core.repositories
public interface DocRepository extends ElasticsearchRepository<Doc, Integer> { ... }
package com.company.api.repositories
public interface LookupRepository extends ElasticsearchRepository<Lookup, Integer> { ... }
我们认为可以用不同的名称定义多个bean,但如何将每个“elasticsearchTemplate”bean与需要它们的不同ElasticsearchRepository实例相关联?此外,我们如何将“私有”bean/配置与
@Resource
private ElasticsearchTemplate template;
我们需要在什么地方本地使用它?可能有多种方法可以做到这一点。这里是一个利用
@Bean
名称和@Resource
名称的程序
@Configuration
public class MyElasticConfig{
@Bean //this is your private template
public ElasticsearchTemplate template(){
//construct your template
return template;
}
@Bean //this is your public template
public ElasticsearchTemplate publicTemplate(){
//construct your template
return template;
}
}
然后你可以像这样得到它们
@Resource
private ElasticsearchTemplate template;
@Resource
private ElasticsearchTemplate publicTemplate;
或
您还可以直接命名@Bean
,而不依赖@Bean
的方法名
@Bean (name="template")
public ElasticsearchTemplate myPrivateTemplate(){
//construct your template
return template;
}
@Bean (name="publicTemplate")
public ElasticsearchTemplate myPubTemplate(){
//construct your template
return template;
}
查看这些关于该主题的精彩资源
您可以使用两个独特的Elasticsearch配置bean和一个用于StatusEndpoint控制器中模板注入的
@Resource(name=“XXX”)
注释来解决此问题
如果根据应使用的Elasticsearch集群将存储库分隔为不同的包,则可以使用@EnableElasticsearchRepositories注释将它们与不同的配置相关联
例如:
如果您有这些包和类:
com.company.data.repositories.private.YourPrivateRepository
com.company.data.repositories.shared.YourSharedRepository
然后是这些配置:
@配置
@EnableElasticsearchRepositories(
basePackages={“com.company.data.repositories.private”},
elasticsearchTemplateRef=“privateElasticsearchTemplate”)
公共类PrivateElasticsearchConfiguration{
@Bean(name=“privateElasticsearchTemplate”)
公共ElasticsearchTemplate privateTemplate(){
//用于创建到专用ES群集的连接的代码
}
}
由于
@EnableElasticsearchRepositories
注释中的elasticsearchTemplateRef
参数,实现存储库的JPA代码将使用basePackages
列表中存储库的指定模板
对于StatusEndpoint
部分,只需为@Resource
注释提供正确的模板bean名称。您的StatusEndpoint
如下所示:
@RestController
@CrossOrigin
@RequestMapping("/v2/statuses/")
public class StatusEndpoint {
@Resource(name="privateElasticsearchTemplate")
private ElasticsearchTemplate template;
@Autowired
private DocRepository docRepository;
@Autowired
private Validator validator;
//...
}
这正好适合这个场景。允许我使用存储库和ElasticSearchTemplate。
@Bean (name="template")
public ElasticsearchTemplate myPrivateTemplate(){
//construct your template
return template;
}
@Bean (name="publicTemplate")
public ElasticsearchTemplate myPubTemplate(){
//construct your template
return template;
}
com.company.data.repositories.private.YourPrivateRepository
com.company.data.repositories.shared.YourSharedRepository
@Configuration
@EnableElasticsearchRepositories(
basePackages = {"com.company.data.repositories.shared"},
elasticsearchTemplateRef = "sharedElasticsearchTemplate")
public class SharedElasticsearchConfiguration {
@Bean(name="sharedElasticsearchTemplate")
public ElasticsearchTemplate sharedTemplate() {
//code to create connection to shared ES cluster
}
}
@RestController
@CrossOrigin
@RequestMapping("/v2/statuses/")
public class StatusEndpoint {
@Resource(name="privateElasticsearchTemplate")
private ElasticsearchTemplate template;
@Autowired
private DocRepository docRepository;
@Autowired
private Validator validator;
//...
}