最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

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

前言

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

Quarkus版本:1.7.0.CR1

集成redis

首先你的quarkus版本一定要1.7.0.CR1版本及以上才行,因?yàn)閞edis的擴(kuò)展包是這個(gè)版本才發(fā)布的,添加依賴(lài):

<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

復(fù)制Redisson序列化

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

/**
 * 和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ù)實(shí)現(xiàn)互通的詳細(xì)內(nèi)容,更多關(guān)于Quarkus集成redis操作Redisson數(shù)據(jù)的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • 在redis中存儲(chǔ)ndarray的示例代碼

    在redis中存儲(chǔ)ndarray的示例代碼

    在Redis中存儲(chǔ)NumPy數(shù)組(ndarray)通常需要將數(shù)組轉(zhuǎn)換為二進(jìn)制格式,然后將其存儲(chǔ)為字符串,這篇文章給大家介紹了在redis中存儲(chǔ)ndarray的示例代碼,感興趣的朋友一起看看吧
    2024-02-02
  • Redis Key的數(shù)量上限及優(yōu)化策略分享

    Redis Key的數(shù)量上限及優(yōu)化策略分享

    Redis 作為高性能的鍵值存儲(chǔ)數(shù)據(jù)庫(kù),廣泛應(yīng)用于緩存、會(huì)話存儲(chǔ)、排行榜等場(chǎng)景,但在實(shí)際使用中,開(kāi)發(fā)者常常會(huì)關(guān)心一個(gè)問(wèn)題:Redis 的 Key 數(shù)量是否有上限?本文將從 Redis Key 的理論上限 出發(fā),深入探討 Redis Key 的管理策略,需要的朋友可以參考下
    2025-03-03
  • 16個(gè)Redis的常見(jiàn)使用場(chǎng)景

    16個(gè)Redis的常見(jiàn)使用場(chǎng)景

    這篇文章主要介紹了Redis 常見(jiàn)使用場(chǎng)景的相關(guān)資料,需要的朋友可以參考下文
    2021-08-08
  • Redis慢查詢(xún)的實(shí)現(xiàn)

    Redis慢查詢(xún)的實(shí)現(xiàn)

    本文主要介紹了Redis慢查詢(xún)的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2023-07-07
  • Redis中的連接命令與鍵命令操作詳解

    Redis中的連接命令與鍵命令操作詳解

    Redis連接命令主要是用于客戶(hù)端與服務(wù)器建立連接的,Redis是一種流行的內(nèi)存數(shù)據(jù)庫(kù),支持多種數(shù)據(jù)結(jié)構(gòu),其中鍵命令是核心操作之一,在Redis中,鍵(Key)是用來(lái)存儲(chǔ)數(shù)據(jù)的主要元素,每個(gè)鍵都有一個(gè)唯一的名稱(chēng),本文給大家介紹了Redis中的連接命令與鍵命令操作
    2024-09-09
  • Python交互Redis的實(shí)現(xiàn)

    Python交互Redis的實(shí)現(xiàn)

    本文主要介紹了Python交互Redis的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2022-08-08
  • 使用Redis防止重復(fù)發(fā)送RabbitMQ消息的方法詳解

    使用Redis防止重復(fù)發(fā)送RabbitMQ消息的方法詳解

    今天遇到一個(gè)問(wèn)題,發(fā)送MQ消息的時(shí)候需要保證不會(huì)重復(fù)發(fā)送,注意不是可靠到達(dá),這里保證的是不會(huì)生產(chǎn)多條一樣的消息,所以本文主要介紹了使用Redis防止重復(fù)發(fā)送RabbitMQ消息的方法,需要的朋友可以參考下
    2025-01-01
  • redis中hash表內(nèi)容刪除的方法代碼

    redis中hash表內(nèi)容刪除的方法代碼

    在本篇文章里小編給各位整理了關(guān)于redis中hash表內(nèi)容怎么刪除的方法以及技巧代碼,需要的朋友們分享下。
    2019-07-07
  • 淺談一下Redis的緩存穿透、擊穿和雪崩

    淺談一下Redis的緩存穿透、擊穿和雪崩

    這篇文章主要介紹了淺談一下Redis緩存穿透、擊穿和雪崩,緩存穿透是指在使用緩存系統(tǒng)時(shí),頻繁查詢(xún)一個(gè)不存在于緩存中的數(shù)據(jù),導(dǎo)致這個(gè)查詢(xún)每次都要通過(guò)緩存層去查詢(xún)數(shù)據(jù)源,無(wú)法從緩存中獲得結(jié)果,需要的朋友可以參考下
    2023-08-08
  • 淺談Redis 緩存的三大問(wèn)題及其解決方案

    淺談Redis 緩存的三大問(wèn)題及其解決方案

    Redis 經(jīng)常用于系統(tǒng)中的緩存,這樣可以解決目前 IO 設(shè)備無(wú)法滿(mǎn)足互聯(lián)網(wǎng)應(yīng)用海量的讀寫(xiě)請(qǐng)求的問(wèn)題。本文主要介紹了淺談Redis 緩存的三大問(wèn)題及其解決方案,感興趣的可以了解一下
    2021-07-07

最新評(píng)論

巴中市| 内江市| 连云港市| 怀柔区| 炎陵县| 林甸县| 梅河口市| 息烽县| 陈巴尔虎旗| 克什克腾旗| 噶尔县| 铜鼓县| 南华县| 辉南县| 大石桥市| 洛川县| 夏邑县| 南陵县| 泗洪县| 顺平县| 上栗县| 东明县| 四会市| 北川| 新兴县| 昂仁县| 辰溪县| 开化县| 湟中县| 康平县| 越西县| 巴中市| 两当县| 峨山| 黄山市| 永定县| 理塘县| 永新县| 榆林市| 云和县| 荣昌县|