{"id":201528,"date":"2025-05-10T08:59:00","date_gmt":"2025-05-10T00:59:00","guid":{"rendered":"https:\/\/server.hk\/cnblog\/201528\/"},"modified":"2025-05-10T08:59:00","modified_gmt":"2025-05-10T00:59:00","slug":"quarkus%e9%9b%86%e6%88%90redis%e6%93%8d%e4%bd%9credisson%e5%ae%9e%e7%8e%b0%e6%95%b0%e6%8d%ae%e4%ba%92%e9%80%9a","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/201528\/","title":{"rendered":"Quarkus\u96c6\u6210redis\u64cd\u4f5cRedisson\u5b9e\u73b0\u6570\u636e\u4e92\u901a"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>Quarkus\u96c6\u6210redis\u64cd\u4f5cRedisson\u5b9e\u73b0\u6570\u636e\u4e92\u901a<\/h1>\n<p><span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u4eb2\u7231\u7684\u7f16\u7a0b\u5b66\u4e60\u7231\u597d\u8005\uff0c\u5982\u679c\u4f60\u70b9\u5f00\u4e86\u8fd9\u7bc7\u6587\u7ae0\uff0c\u8bf4\u660e\u4f60\u5bf9\u300aQuarkus\u96c6\u6210redis\u64cd\u4f5cRedisson\u5b9e\u73b0\u6570\u636e\u4e92\u901a\u300b\u5f88\u611f\u5174\u8da3\u3002\u672c\u7bc7\u6587\u7ae0\u5c31\u6765\u7ed9\u5927\u5bb6\u8be6\u7ec6\u89e3\u6790\u4e00\u4e0b\uff0c\u4e3b\u8981\u4ecb\u7ecd\u4e00\u4e0bQuarkusredis\u3001Redisson\u3001\u6570\u636e\u4e92\u901a\uff0c\u5e0c\u671b\u6240\u6709\u8ba4\u771f\u8bfb\u5b8c\u7684\u7ae5\u978b\u4eec\uff0c\u90fd\u6709\u5b9e\u8d28\u6027\u7684\u63d0\u9ad8\u3002<\/p>\n<p>Quarkus\u7248\u672c\uff1a1.7.0.CR1<\/p>\n<h2>\u96c6\u6210redis<\/h2>\n<p>\u9996\u5148\u4f60\u7684quarkus\u7248\u672c\u4e00\u5b9a\u89811.7.0.CR1\u7248\u672c\u53ca\u4ee5\u4e0a\u624d\u884c\uff0c\u56e0\u4e3aredis\u7684\u6269\u5c55\u5305\u662f\u8fd9\u4e2a\u7248\u672c\u624d\u53d1\u5e03\u7684\uff0c\u6dfb\u52a0\u4f9d\u8d56\uff1a<\/p>\n<pre>\n   \n    \n     io.quarkus\n    \n    \n     quarkus-redis-client\n    \n   <\/pre>\n<p>\u65b0\u589eredis\u94fe\u63a5\u914d\u7f6e<\/p>\n<pre>quarkus.redis.hosts=127.0.0.1:6379\nquarkus.redis.database=0\nquarkus.redis.timeout=10s\nquarkus.redis.password=sasa<\/pre>\n<h2>\u590d\u5236Redisson\u5e8f\u5217\u5316<\/h2>\n<p>Redisson\u91cc\u5185\u7f6e\u4e86\u5f88\u591a\u7684\u5e8f\u5217\u5316\u65b9\u5f0f\uff0c\u6211\u4eec\u7528\u7684JsonJacksonCodec\uff0c\u8fd9\u91cc\u5c06Redisson\u4e2d\u7684\u5b9e\u73b0\u590d\u5236\u540e\uff0c\u7a0d\u52a0\u6539\u52a8\uff0c\u5982\u4e0b\uff1a<\/p>\n<pre>\/**\n * \u548cRedisson\u7684\u5e8f\u5217\u5316\u6570\u636e\u4e92\u76f8\u53cd\u5e8f\u5217\u5316\u7684\u7f16\u89e3\u7801\u5668\n * @author keking\n *\/\npublic class JsonJacksonCodec{\n    public static final JsonJacksonCodec INSTANCE = new JsonJacksonCodec();\n    @JsonIdentityInfo(generator=ObjectIdGenerators.IntSequenceGenerator.class, property=\"@id\")\n    @JsonAutoDetect(fieldVisibility = Visibility.ANY,\n                    getterVisibility = Visibility.PUBLIC_ONLY,\n                    setterVisibility = Visibility.NONE,\n                    isGetterVisibility = Visibility.NONE)\n    public static class ThrowableMixIn {\n    }\n    protected final ObjectMapper mapObjectMapper;\n    public JsonJacksonCodec() {\n        this(new ObjectMapper());\n    }\n    public JsonJacksonCodec(ObjectMapper mapObjectMapper) {\n        this.mapObjectMapper = mapObjectMapper.copy();\n        init(this.mapObjectMapper);\n        initTypeInclusion(this.mapObjectMapper);\n    }\n    protected void initTypeInclusion(ObjectMapper mapObjectMapper) {\n        TypeResolverBuilder&gt; mapTyper = new DefaultTypeResolverBuilder(DefaultTyping.NON_FINAL) {\n            @Override\n            public boolean useForType(JavaType t) {\n                switch (_appliesFor) {\n                case NON_CONCRETE_AND_ARRAYS:\n                    while (t.isArrayType()) {\n                        t = t.getContentType();\n                    }\n                    \/\/ fall through\n                case OBJECT_AND_NON_CONCRETE:\n                    return (t.getRawClass() == Object.class) || !t.isConcrete();\n                case NON_FINAL:\n                    while (t.isArrayType()) {\n                        t = t.getContentType();\n                    }\n                    \/\/ to fix problem with wrong long to int conversion\n                    if (t.getRawClass() == Long.class) {\n                        return true;\n                    }\n                    if (t.getRawClass() == XMLGregorianCalendar.class) {\n                        return false;\n                    }\n                    return !t.isFinal(); \/\/ includes Object.class\n                default:\n                    \/\/ case JAVA_LANG_OBJECT:\n                    return t.getRawClass() == Object.class;\n                }\n            }\n        };\n        mapTyper.init(JsonTypeInfo.Id.CLASS, null);\n        mapTyper.inclusion(JsonTypeInfo.As.PROPERTY);\n        mapObjectMapper.setDefaultTyping(mapTyper);\n    }\n    protected void init(ObjectMapper objectMapper) {\n        objectMapper.setSerializationInclusion(Include.NON_NULL);\n        objectMapper.setVisibility(objectMapper.getSerializationConfig()\n                                                    .getDefaultVisibilityChecker()\n                                                        .withFieldVisibility(JsonAutoDetect.Visibility.ANY)\n                                                        .withGetterVisibility(JsonAutoDetect.Visibility.NONE)\n                                                        .withSetterVisibility(JsonAutoDetect.Visibility.NONE)\n                                                        .withCreatorVisibility(JsonAutoDetect.Visibility.NONE));\n        objectMapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);\n        objectMapper.enable(Feature.WRITE_BIGDECIMAL_AS_PLAIN);\n        objectMapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);\n        objectMapper.enable(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY);\n        objectMapper.addMixIn(Throwable.class, ThrowableMixIn.class);\n    }\n    \/**\n     * \u89e3\u7801\u5668\n     * @param val\n     * @return\n     *\/\n    public Object decoder(String val){\n        try {\n            ByteBuf buf = ByteBufAllocator.DEFAULT.buffer();\n            try (ByteBufOutputStream os = new ByteBufOutputStream(buf)) {\n                os.write(val.getBytes());\n            }\n            return mapObjectMapper.readValue((InputStream) new ByteBufInputStream(buf), Object.class);\n        } catch (IOException e) {\n            e.printStackTrace();\n        }\n        return null;\n    }\n    \/**\n     * \u7f16\u7801\u5668\n     * @param obj\n     * @return\n     *\/\n    public String encoder(Object obj){\n        ByteBuf out = ByteBufAllocator.DEFAULT.buffer();\n        try {\n            ByteBufOutputStream os = new ByteBufOutputStream(out);\n            mapObjectMapper.writeValue((OutputStream) os, obj);\n            return os.buffer().toString(StandardCharsets.UTF_8);\n        } catch (IOException e) {\n            out.release();\n        }\n        return null;\n    }\n}<\/pre>\n<h2>\u4f7f\u7528<\/h2>\n<pre>@Dependent\n@Startup\npublic class Test {\n    @Inject\n    RedisClient redisClient;\n    @Inject\n    Logger logger;\n    void initializeApp(@Observes StartupEvent ev) {\n        \/\/\u4f7f\u7528JsonJacksonCodec\u7f16\u89e3\u7801\uff0c\u4fdd\u6301\u548credisson\u4e92\u901a\n        JsonJacksonCodec codec = JsonJacksonCodec.INSTANCE;\n        Map\n   \n     map = new HashMap();\n        map.put(\"key\",\"666\");\n        redisClient.set(Arrays.asList(\"AAAKEY\", codec.encoder(map)));\n        String str = redisClient.get(\"AAAKEY\").toString(StandardCharsets.UTF_8);\n        Map\n    \n      getVal = (Map\n     \n      ) codec.decoder(str); logger.info(getVal.get(\"key\")); } }\n     \n    \n   <\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Quarkus\u96c6\u6210redis\u64cd\u4f5c&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101],"tags":[],"class_list":["post-201528","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/201528","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=201528"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/201528\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=201528"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=201528"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=201528"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}