SpringDataElasticsearch與SpEL表達(dá)式實(shí)現(xiàn)ES動態(tài)索引
前言
一般情況下,當(dāng)我們使用 SpringDataElasticsearch 去操作 ES 時,索引名稱都會在 @Document 注解中寫死,每次都是對這個固定的索引進(jìn)行操作。
假如我們現(xiàn)在處于一個多租戶系統(tǒng)中,每個租戶都有自己所對應(yīng)的用戶數(shù)據(jù),而這些用戶數(shù)據(jù)都會被導(dǎo)入到 ES 中,那怎么實(shí)現(xiàn)各個租戶的用戶數(shù)據(jù)索引隔離呢?
換言之,在同一個索引結(jié)構(gòu)的情況下怎么實(shí)現(xiàn)一個租戶一個索引?
解決方案:使用 SpEL 表達(dá)式動態(tài)獲取索引。
實(shí)現(xiàn)
動態(tài)獲取索引類
DynamicIndex.java
package cn.xeblog.userprovider.es;
import cn.hutool.core.util.StrUtil;
import org.springframework.stereotype.Component;
/**
* 動態(tài)索引
*
* @author anlingyi
* @date 2022/2/19 6:52 下午
*/
@Component
public class DynamicIndex {
private static final ThreadLocal<String> THREAD_LOCAL = new ThreadLocal<>();
/**
* 獲取索引名稱后綴
*
* @return
*/
public String getSuffix() {
return THREAD_LOCAL.get();
}
/**
* 設(shè)置索引名稱后綴
*
* @param suffix
*/
public void setSuffix(String suffix) {
THREAD_LOCAL.set(suffix);
}
/**
* 移除當(dāng)前索引
*/
public void remove() {
THREAD_LOCAL.remove();
}
/**
* 獲取當(dāng)前索引
*
* @return
*/
public String getIndex() {
if (StrUtil.isBlank(getSuffix())) {
return null;
}
return "user_" + getSuffix();
}
}原理:一般在請求后臺接口的時候,我們會根據(jù)前端傳過來的 Token ,解析出當(dāng)前的用戶信息,然后放置在當(dāng)前請求線程的 ThreadLocal 中,當(dāng)調(diào)用 getIndex() 方法時,會從當(dāng)前線程的 ThreadLocal 中獲取出用戶的編號(索引后綴),然后拼接為一個完整的索引返回。
我這里為了方便測試,提供了 setSuffix()、remove() 等方法,用于手動設(shè)置或移除當(dāng)前索引后綴。
索引數(shù)據(jù)模型
EsUserInfo.java
package cn.xeblog.userprovider.es.model;
import lombok.Data;
import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.Document;
/**
* 用戶信息
*
* @author anlingyi
* @date 2022/2/19 6:47 下午
*/
@Data
@Document(indexName = "#{@dynamicIndex.getIndex()}", type = "_doc", createIndex = false)
public class EsUserInfo {
@Id
private Long id;
/**
* 用戶名
*/
private String username;
/**
* 性別
*/
private String gender;
/**
* 年齡
*/
private Integer age;
}將indexName 設(shè)置為 #{@dynamicIndex.getIndex()} ,這是一個 SpEL 表達(dá)式,dynamicIndex 就是我們上面創(chuàng)建的動態(tài)獲取索引類的對象,當(dāng)需要獲取索引名稱的時候,getIndex() 方法就會被調(diào)用。
createIndex 一定要設(shè)置為 false,避免當(dāng)項(xiàng)目啟動時索引被自動創(chuàng)建。
ES存儲庫實(shí)現(xiàn)
EsUserInfoRepository.java
無需定義任何方法
package cn.xeblog.userprovider.es;
import cn.xeblog.userprovider.es.model.EsUserInfo;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
/**
* @author anlingyi
* @date 2022/2/19 6:55 下午
*/
public interface EsUserInfoRepository extends ElasticsearchRepository<EsUserInfo, Long> {
}測試
package cn.xeblog.userprovider.es;
import cn.xeblog.userprovider.es.model.EsUserInfo;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import javax.annotation.Resource;
import static org.junit.jupiter.api.Assertions.*;
/**
* @author anlingyi
* @date 2022/2/19 6:57 下午
*/
@SpringBootTest
class EsUserInfoRepositoryTest {
@Resource
private EsUserInfoRepository esUserInfoRepository;
@Resource
private DynamicIndex dynamicIndex;
@Test
public void addUserInfo() {
EsUserInfo userInfo = new EsUserInfo();
userInfo.setId(1L);
userInfo.setUsername("張三");
userInfo.setGender("男");
userInfo.setAge(18);
// 索引后綴為當(dāng)前租戶ID:10001
dynamicIndex.setSuffix("10001");
// 為租戶10001添加用戶
esUserInfoRepository.save(userInfo);
// 移除后綴
dynamicIndex.remove();
EsUserInfo userInfo2 = new EsUserInfo();
userInfo2.setId(2L);
userInfo2.setUsername("李四");
userInfo2.setGender("男");
userInfo2.setAge(21);
// 索引后綴為當(dāng)前租戶ID:10002
dynamicIndex.setSuffix("10002");
// 為租戶10002添加用戶
esUserInfoRepository.save(userInfo2);
// 移除后綴
dynamicIndex.remove();
}
}我這里分別為 租戶10001 和 租戶10002 各創(chuàng)建了一個用戶。

注意
除了 createIndex 一定要設(shè)置為 false 之外,還有一個需要特別注意的地方:
DynamicIndex 的 getIndex() 方法在獲取不到當(dāng)前的索引后綴的情況下,一定要返回null ?。?!
/**
* 獲取當(dāng)前索引
*
* @return
*/
public String getIndex() {
if (StrUtil.isBlank(getSuffix())) {
// 一定要返回null
return null;
}
return "user_" + getSuffix();
}為什么呢?
淺看一下 ElasticsearchRepository.java 源碼你就懂了。
AbstractElasticsearchRepository.java 是 ElasticsearchRepository.java 的具體實(shí)現(xiàn)類,我們看一下這個類的 save() 方法的實(shí)現(xiàn)代碼
@Override
public <S extends T> S save(S entity) {
Assert.notNull(entity, "Cannot save 'null' entity.");
elasticsearchOperations.index(createIndexQuery(entity));
elasticsearchOperations.refresh(entityInformation.getIndexName());
return entity;
}當(dāng)執(zhí)行到 elasticsearchOperations.refresh(entityInformation.getIndexName()); 這行代碼時,獲取到的索引后綴可能為空。
原因在于 entityInformation.getIndexName()
MappingElasticsearchEntityInformation.java
@Override
public String getIndexName() {
return indexName != null ? indexName : entityMetadata.getIndexName();
}在項(xiàng)目啟動時,SpringDataElasticsearch 會去解析一次 @Document 注解獲取出索引名稱,并將索引名稱保存到 MappingElasticsearchEntityInformation.java 類的 indexName 字段中,后續(xù)調(diào)用 entityInformation.getIndexName() 時,indexName 字段值不為 null 時會直接返回,不會再去解析 @Document 注解。
這樣就存在一個問題,當(dāng)項(xiàng)目啟動的時候 getSuffix() 返回的肯定是 null,如果在 getIndex() 方法中去掉判空代碼,第一次調(diào)用時,返回的索引名稱肯定會是 user_null,這樣就會出現(xiàn)索引不存在的問題。
到此這篇關(guān)于SpringDataElasticsearch與SpEL表達(dá)式實(shí)現(xiàn)ES動態(tài)索引的文章就介紹到這了,更多相關(guān)SpringDataElasticsearch實(shí)現(xiàn)ES動態(tài)索引內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
JPA?@ManyToMany?報(bào)錯StackOverflowError的解決
這篇文章主要介紹了JPA?@ManyToMany?報(bào)錯StackOverflowError的解決,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-12-12
IntelliJ IDEA設(shè)置代碼的快捷編輯模板Live Templates
今天小編就為大家分享一篇關(guān)于IntelliJ IDEA設(shè)置代碼的快捷編輯模板Live Templates,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧2018-10-10
SpringBoot@DeleteMapping(/xxx/{id})請求報(bào)405的解決
這篇文章主要介紹了SpringBoot@DeleteMapping(/xxx/{id})請求報(bào)405的解決方案,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-01-01
Java實(shí)現(xiàn)單例設(shè)計(jì)模式方法解析
這篇文章主要介紹了Java實(shí)現(xiàn)單例設(shè)計(jì)模式方法解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-04-04

