Multiple data sources support
# Introduction
We provide the same multi-data source support as MP, with basically no additional learning and understanding costs. If you have been exposed to MP's multi-data sources before, you can simply understand and use it here. What needs special attention is that, We have adopted a more lightweight solution, which does not require the introduction of multi-data source plug-ins before use like MP. It can be directly configured and used, which is more convenient to use. In addition, considering that MP uses the @DS (*Silk) annotation, in order to distinguish it from MP, adjust the annotation to @EsDS (Russian is *Silk) in EasyEs.
# Use posture
Step 1: Add multiple data source related configurations to the configuration file
easy-es:
dynamic:
datasource:
ds1:
address: 10.20.64.228:9200 #Connection address of data source 1
#username: 'Remove this line if there is nothing'
#password: 'Remove this line if there is nothing'
ds2:
address: 49.234.28.111:9200 #Connection address of data source 2
#username: 'Remove this line if there is nothing'
#password: 'Remove this line if there is nothing'
#dsn…
2
3
4
5
6
7
8
9
10
11
12
Step 2: Add the @EsDS annotation to the corresponding Mapper
Add the @EsDS annotation to the Mapper involved, and specify the data source name in the value. The data source name is ds1, ds2... added in the configuration file above. Of course, you can also call it dds, xds... In short, you The desired connection address and the value in the annotation can match.
@EsDS("ds1")
public interface DocumentMapper extends BaseEsMapper<Document> {
}
@EsDS("ds2")
public interface CommentMapper extends BaseEsMapper<Comment> {
}
2
3
4
5
6
7
Step 3: Inject the corresponding mapper for query
This step is relatively simple. Just use it as an ordinary mapper. However, compared with the previous mapper, each mapper is bound to a specific data source. After that, a series of CRUD operations performed through the mapper are specified by annotations. data source.
# Expand
Since the current version of EE does not provide an xml method similar to MP (dsl may not be suitable for expression in xml, and is still being explored), it uses a multi-dimensional hybrid query mode out of the box to meet the needs of all scenarios. Therefore, the @EsDS annotation cannot be added to the specific method, but it does not affect the user's use at all. Even if a very flexible scenario occurs, the user can still use a hybrid query to solve it. Entrust the mapper to complete CRUD. The data source specified by @EsDS on the mapper is the corresponding data source for hybrid query. Of course, you can also have other extremely flexible options, such as the following, directly get the framework assembled Use RestHighLevelClient.
public class DsTest{
@Autowired
private RestHighLevelClientUtils restHighLevelClientUtils;
@Test
public void testDs(){
// Get the corresponding RestHighLevelClient by passing in restHighLevelClientId. RestHighLevelClientId is the data source name ds1, ds2... configured in the configuration file.
RestHighLevelClient restHighLevelClient = restHighLevelClientUtils.getClient(String restHighLevelClientId);
// After getting the corresponding RestHighLevelClient, I don’t need to tell you how to do the rest. It is the usage of ES native RestHighLevelClient. I don’t know how to use Baidu or look at the es official website.
}
}
2
3
4
5
6
7
8
9
10
11
12
13
Compared with the above method, the out-of-box queries and hybrid queries provided by EE are obviously the best solutions and can help you minimize the amount of code. If you need to specify a specified index to query a certain data source, You can move to the chapter to see dynamic index support!