{"id":874,"date":"2020-04-14T15:57:55","date_gmt":"2020-04-14T07:57:55","guid":{"rendered":"http:\/\/toradora.xyz\/?p=874"},"modified":"2020-04-14T15:57:55","modified_gmt":"2020-04-14T07:57:55","slug":"spring-data-redis%e5%8a%a8%e6%80%81%e8%ae%a2%e9%98%85%e5%8f%91%e5%b8%83","status":"publish","type":"post","link":"https:\/\/diuut.com\/?p=874","title":{"rendered":"Spring-Data-Redis\u52a8\u6001\u8ba2\u9605\u53d1\u5e03"},"content":{"rendered":"\n<p>\u666e\u901aredis\u8ba2\u9605\uff0c\u662f\u4ee5\u7528container\u505a\u5bb9\u5668\uff0c\u914d\u7f6e\u7c7b\u914d\u7f6e\u6587\u4ef6\u65b9\u5f0f\u76f4\u63a5\u5728spring init\u7684\u65f6\u5019\u8fdb\u884c\u52a0\u8f7d\uff0c\u4e0d\u80fd\u8fdb\u884c\u52a8\u6001\u6dfb\u52a0\u3002\u5728\u7a0b\u5e8f\u8fd0\u884c\u65f6\u4fee\u6539\u4e0d\u8d77\u4f5c\u7528\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><pre class=\"brush: java; title: ; notranslate\" title=\"\">\n@Configuration\npublic class RedisChannelConfig {\n    @Bean\n    RedisMessageListenerContainer container(RedisConnectionFactory connectionFactory,\n                                            MessageListenerAdapter listenerAdapter) {\n        RedisMessageListenerContainer container = new RedisMessageListenerContainer();\n        container.setConnectionFactory(connectionFactory);\n        \/\/\u8ba2\u9605\u4e3b\u9898messagepush\u548cmessagepush3\n        container.addMessageListener(listenerAdapter, new PatternTopic(&quot;messagepush&quot;));\n        container.addMessageListener(listenerAdapter, new PatternTopic(&quot;messagepush3&quot;));\n        \/\/\u8fd9\u4e2acontainer \u53ef\u4ee5\u6dfb\u52a0\u591a\u4e2a messageListener\n        return container;\n    }\n\n    @Bean\n    MessageListenerAdapter listenerAdapter(MessageReceive receiver) {\n        \/\/\u8fd9\u4e2a\u5730\u65b9 \u662f\u7ed9messageListenerAdapter \u4f20\u5165\u4e00\u4e2a\u6d88\u606f\u63a5\u53d7\u7684\u5904\u7406\u5668\uff0c\u5229\u7528\u53cd\u5c04\u7684\u65b9\u6cd5\u8c03\u7528\u201creceiveMessage\u201d\n        \/\/\u4e5f\u6709\u597d\u51e0\u4e2a\u91cd\u8f7d\u65b9\u6cd5\uff0c\u8fd9\u8fb9\u9ed8\u8ba4\u8c03\u7528\u5904\u7406\u5668\u7684\u65b9\u6cd5 \u53ebhandleMessage \u53ef\u4ee5\u81ea\u5df1\u5230\u6e90\u7801\u91cc\u9762\u770b\n        return new MessageListenerAdapter(receiver, &quot;receiveMessage&quot;);\n    }\n\n    @Bean \/\/\u6ce8\u5165\u64cd\u4f5c\u6570\u636e\u7684template(\u8fd9\u91cc\u4e0d\u9700\u8981\u64cd\u4f5credis\u6570\u636e,\u548c\u6d88\u606f\u961f\u5217\u529f\u80fd\u65e0\u5173)\n    StringRedisTemplate template(RedisConnectionFactory connectionFactory) {\n        return new StringRedisTemplate(connectionFactory);\n    }\n}\n\/\/\u6b64\u6bb5\u4ee3\u7801\u6458\u81ea: https:\/\/blog.csdn.net\/zhang18024666607\/article\/details\/84392335\n<\/pre><\/code><\/pre>\n\n\n\n<p>\u5982\u679c\u53ea\u662f\u5f53\u505a\u56fa\u5b9a\u7684\u6d88\u606f\u961f\u5217\u8fdb\u884c\u8ba2\u9605\u53d1\u5e03\uff0c\u8db3\u591f\uff0c\u4f46\u662f\u5982\u679c\u9700\u6c42\u662f\u6839\u636e\u524d\u53f0\u4f20\u5165\u7684\u5b57\u6bb5\uff0c\u52a8\u6001\u7684\u8ba2\u9605\u7684\u8bdd\u5c31\u65e0\u6cd5\u6ee1\u8db3\u4e86\uff0c\u60f3\u8981\u5b9e\u73b0\u5c31\u4e0d\u80fd\u7528 container \u7684\u65b9\u5f0f\u8fdb\u884c\u8ba2\u9605\uff0c\u4f46\u662f\u53ef\u4ee5\u5229\u7528Lettuce\u5ba2\u6237\u7aef\u8fdb\u884c\u8ba2\u9605\uff0c\u65e7\u7248\u672c\u4e2d\u7684spring-data-redis\u4e2d\u7684\u81ea\u5e26\u5ba2\u6237\u7aef\u90fd\u662fjedis\uff0c\u65b0\u7248\u672c\u540e\u90fd\u6362\u6210\u4e86Lettuce\uff0c\u8fd8\u81ea\u5e26\u4e86\u5f02\u6b65\u65b9\u6cd5\uff0c\u4e0d\u4f1a\u5bf9\u7cfb\u7edf\u963b\u585e\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><pre class=\"brush: java; title: ; notranslate\" title=\"\">\npackage com.miracle.im.service;\n\nimport com.miracle.im.pojo.Msg;\nimport io.lettuce.core.RedisClient;\nimport io.lettuce.core.RedisFuture;\nimport io.lettuce.core.RedisURI;\nimport io.lettuce.core.pubsub.StatefulRedisPubSubConnection;\nimport io.lettuce.core.pubsub.api.async.RedisPubSubAsyncCommands;\nimport lombok.extern.slf4j.Slf4j;\nimport org.springframework.beans.factory.annotation.Autowired;\nimport org.springframework.stereotype.Service;\n\n\/**\n * @Author Diuut\n * @Date 2020\/4\/13  9:50\n *\/\n@Slf4j\n@Service\npublic class ImService {\n    @Autowired\n    private MyRedisPubSubListener myRedisPubSubListener;\n\n    public String publish(String consumer, String msg) {\n        RedisURI redisUri = RedisURI.Builder.redis(&quot;xxxxxx&quot;)\n                .withPassword(&quot;xxxxxx&quot;)\n                .withDatabase(2)\n                .withPort(6379)\n                .build();\n\/\/        RedisURI redisUri = RedisURI.Builder.redis(&quot;127.0.0.1&quot;).build();\n        RedisClient redisClient = RedisClient.create(redisUri);\n        StatefulRedisPubSubConnection&amp;lt;String, String&gt; connection = redisClient.connectPubSub();\n        RedisPubSubAsyncCommands&amp;lt;String, String&gt; async = connection.async();\n        RedisFuture&amp;lt;Long&gt; publish = async.publish(consumer, msg);\n        return &quot;\u6267\u884c\u5b8c\u6bd5&quot;;\n    }\n\n    public String subscribe(String username) {\n        RedisURI redisUri = RedisURI.Builder.redis(&quot;xxxxxx&quot;)\n                .withPassword(&quot;xxxxxx&quot;)\n                .withDatabase(2)\n                .withPort(6379)\n                .build();\n\/\/        RedisURI redisUri = RedisURI.Builder.redis(&quot;127.0.0.1&quot;).build();\n        RedisClient client = RedisClient.create(redisUri);\n        StatefulRedisPubSubConnection&amp;lt;String, String&gt; connection = client.connectPubSub();\n        connection.addListener(myRedisPubSubListener);\n        RedisPubSubAsyncCommands&amp;lt;String, String&gt; async = connection.async();\n        async.subscribe(&quot;Topic_&quot; + username);\n        async.subscribe(&quot;Topic_server&quot;);\n\/\/        log.info(&quot;future: {}&quot;,future);\n        return &quot;\u6267\u884c\u5b8c\u6bd5&quot;;\n    }\n}\n<\/pre><\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code><pre class=\"brush: java; title: ; notranslate\" title=\"\">\npackage com.miracle.im.service;\n\n\nimport lombok.extern.slf4j.Slf4j;\nimport org.springframework.stereotype.Component;\n\n\nimport io.lettuce.core.pubsub.RedisPubSubListener;\n\n\/**\n * Simple to Introduction\n * className: MyRedisPubSubListener\n *\n *\/\n@Slf4j\n@Component\npublic class MyRedisPubSubListener implements RedisPubSubListener&amp;lt;String, String&gt; {\n\n    @Override\n    public void message(String channel, String message) {\n        log.info(&quot;msg1={} on channel {}&quot;, message, channel);\n    }\n\n    @Override\n    public void message(String pattern, String channel, String message) {\n        log.info(&quot;msg2={} in channel={}&quot;, message, channel);\n    }\n\n    @Override\n    public void subscribed(String channel, long count) {\n        log.info(&quot;sub channel={}, count={}&quot;, channel, count);\n    }\n\n    @Override\n    public void psubscribed(String pattern, long count) {\n        log.info(&quot;psub pattern={}, count={}&quot;, pattern, count);\n    }\n\n    @Override\n    public void unsubscribed(String channel, long count) {\n        log.info(&quot;unsub channel={}, count={}&quot;, channel, count);\n    }\n\n    @Override\n    public void punsubscribed(String pattern, long count) {\n        log.info(&quot;punsub channel={}, count={}&quot;, pattern, count);\n    }\n}\n<\/pre><\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u666e\u901aredis\u8ba2\u9605\uff0c\u662f\u4ee5\u7528container\u505a\u5bb9\u5668\uff0c\u914d\u7f6e\u7c7b\u914d\u7f6e\u6587\u4ef6\u65b9\u5f0f\u76f4\u63a5\u5728spring init\u7684\u65f6\u5019\u8fdb\u884c\u52a0<span class=\"more-button\"><a href=\"https:\/\/diuut.com\/?p=874\" class=\"more-link\">view all . . .<span class=\"screen-reader-text\">Spring-Data-Redis\u52a8\u6001\u8ba2\u9605\u53d1\u5e03<\/span><\/a><\/span><\/p>\n","protected":false},"author":1,"featured_media":875,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[2],"tags":[],"_links":{"self":[{"href":"https:\/\/diuut.com\/index.php?rest_route=\/wp\/v2\/posts\/874"}],"collection":[{"href":"https:\/\/diuut.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/diuut.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/diuut.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/diuut.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=874"}],"version-history":[{"count":0,"href":"https:\/\/diuut.com\/index.php?rest_route=\/wp\/v2\/posts\/874\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/diuut.com\/index.php?rest_route=\/wp\/v2\/media\/875"}],"wp:attachment":[{"href":"https:\/\/diuut.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=874"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/diuut.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=874"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/diuut.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=874"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}