欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

SpringDataElasticsearch與SpEL表達(dá)式實(shí)現(xiàn)ES動(dòng)態(tài)索引

 更新時(shí)間:2022年09月09日 08:44:18   作者:AnLingYi???????  
這篇文章主要介紹了SpringDataElasticsearch與SpEL表達(dá)式實(shí)現(xiàn)ES動(dòng)態(tài)索引,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的朋友可以參考一下

前言

一般情況下,當(dāng)我們使用 SpringDataElasticsearch 去操作 ES 時(shí),索引名稱都會(huì)在 @Document 注解中寫死,每次都是對(duì)這個(gè)固定的索引進(jìn)行操作。

假如我們現(xiàn)在處于一個(gè)多租戶系統(tǒng)中,每個(gè)租戶都有自己所對(duì)應(yīng)的用戶數(shù)據(jù),而這些用戶數(shù)據(jù)都會(huì)被導(dǎo)入到 ES 中,那怎么實(shí)現(xiàn)各個(gè)租戶的用戶數(shù)據(jù)索引隔離呢?

換言之,在同一個(gè)索引結(jié)構(gòu)的情況下怎么實(shí)現(xiàn)一個(gè)租戶一個(gè)索引?

解決方案:使用 SpEL 表達(dá)式動(dòng)態(tài)獲取索引。

實(shí)現(xiàn)

動(dòng)態(tài)獲取索引類

DynamicIndex.java

package cn.xeblog.userprovider.es;

import cn.hutool.core.util.StrUtil;
import org.springframework.stereotype.Component;

/**
 * 動(dòng)態(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();
    }

}

原理:一般在請(qǐng)求后臺(tái)接口的時(shí)候,我們會(huì)根據(jù)前端傳過來的 Token ,解析出當(dāng)前的用戶信息,然后放置在當(dāng)前請(qǐng)求線程的 ThreadLocal 中,當(dāng)調(diào)用 getIndex() 方法時(shí),會(huì)從當(dāng)前線程的 ThreadLocal 中獲取出用戶的編號(hào)(索引后綴),然后拼接為一個(gè)完整的索引返回。

我這里為了方便測試,提供了 setSuffix()、remove() 等方法,用于手動(dòng)設(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()} ,這是一個(gè) SpEL 表達(dá)式,dynamicIndex 就是我們上面創(chuàng)建的動(dòng)態(tài)獲取索引類的對(duì)象,當(dāng)需要獲取索引名稱的時(shí)候,getIndex() 方法就會(huì)被調(diào)用。

createIndex 一定要設(shè)置為 false,避免當(dāng)項(xiàng)目啟動(dòng)時(shí)索引被自動(dòng)創(chuàng)建。

ES存儲(chǔ)庫實(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)建了一個(gè)用戶。

注意

除了 createIndex 一定要設(shè)置為 false 之外,還有一個(gè)需要特別注意的地方:

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)類,我們看一下這個(gè)類的 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()); 這行代碼時(shí),獲取到的索引后綴可能為空。

原因在于 entityInformation.getIndexName()

MappingElasticsearchEntityInformation.java

	@Override
	public String getIndexName() {
		return indexName != null ? indexName : entityMetadata.getIndexName();
	}

在項(xiàng)目啟動(dòng)時(shí),SpringDataElasticsearch 會(huì)去解析一次 @Document 注解獲取出索引名稱,并將索引名稱保存到 MappingElasticsearchEntityInformation.java 類的 indexName 字段中,后續(xù)調(diào)用 entityInformation.getIndexName() 時(shí),indexName 字段值不為 null 時(shí)會(huì)直接返回,不會(huì)再去解析 @Document 注解。

這樣就存在一個(gè)問題,當(dāng)項(xiàng)目啟動(dòng)的時(shí)候 getSuffix() 返回的肯定是 null,如果在 getIndex() 方法中去掉判空代碼,第一次調(diào)用時(shí),返回的索引名稱肯定會(huì)是 user_null,這樣就會(huì)出現(xiàn)索引不存在的問題。

到此這篇關(guān)于SpringDataElasticsearch與SpEL表達(dá)式實(shí)現(xiàn)ES動(dòng)態(tài)索引的文章就介紹到這了,更多相關(guān)SpringDataElasticsearch實(shí)現(xiàn)ES動(dòng)態(tài)索引內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Spring配置文件使用占位符配置方式

    Spring配置文件使用占位符配置方式

    這篇文章主要介紹了Spring配置文件使用占位符配置方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-11-11
  • IDEA如何實(shí)現(xiàn)批量修改變量名

    IDEA如何實(shí)現(xiàn)批量修改變量名

    這篇文章主要介紹了IDEA如何實(shí)現(xiàn)批量修改變量名問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-11-11
  • 詳解MyBatis自定義Plugin插件

    詳解MyBatis自定義Plugin插件

    這篇文章主要介紹了MyBatis自定義Plugin插件的相關(guān)知識(shí),實(shí)現(xiàn)方法也很簡單,只需實(shí)現(xiàn) Interceptor 接口,并指定想要攔截的方法簽名即可,需要的朋友可以參考下
    2018-06-06
  • JPA?@ManyToMany?報(bào)錯(cuò)StackOverflowError的解決

    JPA?@ManyToMany?報(bào)錯(cuò)StackOverflowError的解決

    這篇文章主要介紹了JPA?@ManyToMany?報(bào)錯(cuò)StackOverflowError的解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-12-12
  • IntelliJ IDEA設(shè)置代碼的快捷編輯模板Live Templates

    IntelliJ IDEA設(shè)置代碼的快捷編輯模板Live Templates

    今天小編就為大家分享一篇關(guān)于IntelliJ IDEA設(shè)置代碼的快捷編輯模板Live Templates,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧
    2018-10-10
  • Java實(shí)現(xiàn)貪吃蛇游戲的示例代碼

    Java實(shí)現(xiàn)貪吃蛇游戲的示例代碼

    這篇文章主要為大家詳細(xì)介紹了如何利用Java實(shí)現(xiàn)簡單的貪吃蛇游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-09-09
  • java金錢處理方法實(shí)例詳解

    java金錢處理方法實(shí)例詳解

    這篇文章主要介紹了java金錢處理方法實(shí)例詳解的相關(guān)資料,這里提供實(shí)現(xiàn)方法分轉(zhuǎn)化成元的實(shí)現(xiàn)方法,需要的朋友可以參考下
    2017-08-08
  • SpringBoot@DeleteMapping(/xxx/{id})請(qǐng)求報(bào)405的解決

    SpringBoot@DeleteMapping(/xxx/{id})請(qǐng)求報(bào)405的解決

    這篇文章主要介紹了SpringBoot@DeleteMapping(/xxx/{id})請(qǐng)求報(bào)405的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-01-01
  • Java實(shí)現(xiàn)單例設(shè)計(jì)模式方法解析

    Java實(shí)現(xiàn)單例設(shè)計(jì)模式方法解析

    這篇文章主要介紹了Java實(shí)現(xiàn)單例設(shè)計(jì)模式方法解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-04-04
  • Java獲取文件夾下所有文件名稱的方法示例

    Java獲取文件夾下所有文件名稱的方法示例

    這篇文章主要介紹了Java獲取文件夾下所有文件名稱的方法,涉及java針對(duì)文件與目錄相關(guān)操作技巧,需要的朋友可以參考下
    2017-06-06

最新評(píng)論