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

Quarkus集成redis操作Redisson實現(xiàn)數(shù)據(jù)互通

 更新時間:2022年02月23日 09:40:21   作者:kl  
這篇文章主要為大家介紹了Quarkus集成redis操作Redisson實現(xiàn)數(shù)據(jù)互通的示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步

前言

博主所在公司大量使用了redis緩存,redis客戶端用的Redisson。在Quarkus集成redis時,博主嘗試使用Redisson客戶端直接集成,發(fā)現(xiàn),在jvm模式下運行quarkus沒點問題,但是在打native image時,就報錯了,嘗試了很多方式都是莫名其妙的異常。最后決定采用quarkus官方的redis客戶端,但是Redisson客戶端數(shù)據(jù)序列化方式是特有的,不是簡單的String,所以quarkus中的redis需要操作Redisson的數(shù)據(jù),就要保持序列化方式一致,本文就是為了解決這個問題。

Quarkus版本:1.7.0.CR1

集成redis

首先你的quarkus版本一定要1.7.0.CR1版本及以上才行,因為redis的擴展包是這個版本才發(fā)布的,添加依賴:

<dependency>
            <groupId>io.quarkus</groupId>
            <artifactId>quarkus-redis-client</artifactId>
</dependency>

新增redis鏈接配置

quarkus.redis.hosts=127.0.0.1:6379
quarkus.redis.database=0
quarkus.redis.timeout=10s
quarkus.redis.password=sasa

復制Redisson序列化

Redisson里內置了很多的序列化方式,我們用的JsonJacksonCodec,這里將Redisson中的實現(xiàn)復制后,稍加改動,如下:

/**
 * 和Redisson的序列化數(shù)據(jù)互相反序列化的編解碼器
 * @author keking
 */
public class JsonJacksonCodec{
    public static final JsonJacksonCodec INSTANCE = new JsonJacksonCodec();
    @JsonIdentityInfo(generator=ObjectIdGenerators.IntSequenceGenerator.class, property="@id")
    @JsonAutoDetect(fieldVisibility = Visibility.ANY,
                    getterVisibility = Visibility.PUBLIC_ONLY,
                    setterVisibility = Visibility.NONE,
                    isGetterVisibility = Visibility.NONE)
    public static class ThrowableMixIn {
    }
    protected final ObjectMapper mapObjectMapper;
    public JsonJacksonCodec() {
        this(new ObjectMapper());
    }
    public JsonJacksonCodec(ObjectMapper mapObjectMapper) {
        this.mapObjectMapper = mapObjectMapper.copy();
        init(this.mapObjectMapper);
        initTypeInclusion(this.mapObjectMapper);
    }
    protected void initTypeInclusion(ObjectMapper mapObjectMapper) {
        TypeResolverBuilder<?> mapTyper = new DefaultTypeResolverBuilder(DefaultTyping.NON_FINAL) {
            @Override
            public boolean useForType(JavaType t) {
                switch (_appliesFor) {
                case NON_CONCRETE_AND_ARRAYS:
                    while (t.isArrayType()) {
                        t = t.getContentType();
                    }
                    // fall through
                case OBJECT_AND_NON_CONCRETE:
                    return (t.getRawClass() == Object.class) || !t.isConcrete();
                case NON_FINAL:
                    while (t.isArrayType()) {
                        t = t.getContentType();
                    }
                    // to fix problem with wrong long to int conversion
                    if (t.getRawClass() == Long.class) {
                        return true;
                    }
                    if (t.getRawClass() == XMLGregorianCalendar.class) {
                        return false;
                    }
                    return !t.isFinal(); // includes Object.class
                default:
                    // case JAVA_LANG_OBJECT:
                    return t.getRawClass() == Object.class;
                }
            }
        };
        mapTyper.init(JsonTypeInfo.Id.CLASS, null);
        mapTyper.inclusion(JsonTypeInfo.As.PROPERTY);
        mapObjectMapper.setDefaultTyping(mapTyper);
    }
    protected void init(ObjectMapper objectMapper) {
        objectMapper.setSerializationInclusion(Include.NON_NULL);
        objectMapper.setVisibility(objectMapper.getSerializationConfig()
                                                    .getDefaultVisibilityChecker()
                                                        .withFieldVisibility(JsonAutoDetect.Visibility.ANY)
                                                        .withGetterVisibility(JsonAutoDetect.Visibility.NONE)
                                                        .withSetterVisibility(JsonAutoDetect.Visibility.NONE)
                                                        .withCreatorVisibility(JsonAutoDetect.Visibility.NONE));
        objectMapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
        objectMapper.enable(Feature.WRITE_BIGDECIMAL_AS_PLAIN);
        objectMapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);
        objectMapper.enable(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY);
        objectMapper.addMixIn(Throwable.class, ThrowableMixIn.class);
    }
    /**
     * 解碼器
     * @param val
     * @return
     */
    public Object decoder(String val){
        try {
            ByteBuf buf = ByteBufAllocator.DEFAULT.buffer();
            try (ByteBufOutputStream os = new ByteBufOutputStream(buf)) {
                os.write(val.getBytes());
            }
            return mapObjectMapper.readValue((InputStream) new ByteBufInputStream(buf), Object.class);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
    /**
     * 編碼器
     * @param obj
     * @return
     */
    public String encoder(Object obj){
        ByteBuf out = ByteBufAllocator.DEFAULT.buffer();
        try {
            ByteBufOutputStream os = new ByteBufOutputStream(out);
            mapObjectMapper.writeValue((OutputStream) os, obj);
            return os.buffer().toString(StandardCharsets.UTF_8);
        } catch (IOException e) {
            out.release();
        }
        return null;
    }
}

使用

@Dependent
@Startup
public class Test {
    @Inject
    RedisClient redisClient;
    @Inject
    Logger logger;
    void initializeApp(@Observes StartupEvent ev) {
        //使用JsonJacksonCodec編解碼,保持和redisson互通
        JsonJacksonCodec codec = JsonJacksonCodec.INSTANCE;
        Map<String, String> map = new HashMap<>();
        map.put("key","666");
        redisClient.set(Arrays.asList("AAAKEY", codec.encoder(map)));
        String str = redisClient.get("AAAKEY").toString(StandardCharsets.UTF_8);
        Map<String,String> getVal = (Map<String, String>) codec.decoder(str);
        logger.info(getVal.get("key"));
    }

}

以上就是Quarkus集成redis操作Redisson數(shù)據(jù)實現(xiàn)互通的詳細內容,更多關于Quarkus集成redis操作Redisson數(shù)據(jù)的資料請關注腳本之家其它相關文章!

相關文章

  • 如何使用Redis鎖處理并發(fā)問題詳解

    如何使用Redis鎖處理并發(fā)問題詳解

    這篇文章主要給大家介紹了關于如何使用Redis鎖處理并發(fā)問題的相關資料,文中通過示例代碼介紹的非常詳細,對大家學習或者使用Redis具有一定的參考學習價值,需要的朋友們下面來一起學習學習吧
    2019-07-07
  • Redis5之后版本的高可用集群搭建的實現(xiàn)

    Redis5之后版本的高可用集群搭建的實現(xiàn)

    這篇文章主要介紹了Redis5之后版本的高可用集群搭建的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2021-04-04
  • Redis Stat的安裝指南

    Redis Stat的安裝指南

    這篇文章主要介紹了Redis Stat的安裝指南的相關資料,需要的朋友可以參考下
    2016-04-04
  • Redis中一些最常見的面試問題總結

    Redis中一些最常見的面試問題總結

    Redis在互聯(lián)網(wǎng)技術存儲方面使用如此廣泛,幾乎所有的后端技術面試官都要在Redis的使用和原理方面對小伙伴們進行各種刁難。下面這篇文章主要給大家總結介紹了關于Redis中一些最常見的面試問題,需要的朋友可以參考下
    2018-09-09
  • Redis與本地緩存的結合實現(xiàn)

    Redis與本地緩存的結合實現(xiàn)

    我們開發(fā)中經(jīng)常用到Redis作為緩存,本文主要介紹了Redis與本地緩存的結合實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2022-07-07
  • Redis不僅僅是緩存,還是……

    Redis不僅僅是緩存,還是……

    Redis是一個開源的(BSD協(xié)議),內存中的數(shù)據(jù)結構存儲,它可以用作數(shù)據(jù)庫,緩存,消息代理。這篇文章主要介紹了Redis不僅僅是緩存,還是……,需要的朋友可以參考下
    2020-12-12
  • Redis安裝教程圖解

    Redis安裝教程圖解

    Redis是一個開源的使用ANSI C語言編寫、支持網(wǎng)絡、可基于內存亦可持久化的日志型、Key-Value數(shù)據(jù)庫,并提供多種語言的API。本文就教大家如何安裝Redis,需要的朋友可以參考下
    2015-10-10
  • Redis操作相關命令之查看、停止、啟動命令

    Redis操作相關命令之查看、停止、啟動命令

    這篇文章主要介紹了Redis操作相關命令之查看、停止、啟動命令,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-09-09
  • 一文弄懂Redis單線程和多線程

    一文弄懂Redis單線程和多線程

    本文主要介紹了一文弄懂Redis單線程和多線程,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2023-07-07
  • Redis類型type與編碼encoding原理及使用示例

    Redis類型type與編碼encoding原理及使用示例

    這篇文章主要為大家介紹了Redis類型type與編碼encoding原理及使用示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-03-03

最新評論