{"id":234402,"date":"2022-02-07T08:51:51","date_gmt":"2022-02-07T00:51:51","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=234402"},"modified":"2022-01-29T15:53:21","modified_gmt":"2022-01-29T07:53:21","slug":"redis-subscription-publishing","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/redis-subscription-publishing.html","title":{"rendered":"SpringBoot+Redis \u5b9e\u73b0\u6d88\u606f\u8ba2\u9605\u53d1\u5e03"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\nRedis \u901a\u5e38\u5728\u9879\u76ee\u4e2d\u7528\u7684\u6700\u591a\u7684\u529f\u80fd\u662f\u7f13\u5b58\uff0c\u7136\u800c\u4eca\u5929\u4e3a\u4f60\u4ecb\u7ecd\u5b83\u7684\u53e6\u4e00\u4e2a\u529f\u80fd\uff0c\u8f7b\u91cf\u7ea7\u7684\u6d88\u606f\u961f\u5217\u3002<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n
Redis \u53d1\u5e03\u8ba2\u9605<\/strong><\/div>\n

Redis \u63d0\u4f9b\u4e86\u53d1\u5e03\u8ba2\u9605\u529f\u80fd\uff0c\u53ef\u4ee5\u7528\u4e8e\u6d88\u606f\u7684\u4f20\u8f93\uff0cRedis \u7684\u53d1\u5e03\u8ba2\u9605\u673a\u5236\u5305\u62ec\u4e09\u4e2a\u90e8\u5206\uff0c\u53d1\u5e03\u8005\uff0c\u8ba2\u9605\u8005\u548c Channel\uff08\u79f0\u4e4b\u4e3a\u9891\u9053\u6216\u4e3b\u9898\uff09\u3002<\/p>\n

\"\"<\/p>\n

\u53d1\u5e03\u8005\u5c06\u6d88\u606f\u53d1\u5e03\u5230\u6307\u5b9a\u9891\u9053\uff0c\u8ba2\u9605\u8be5\u9891\u9053\u7684\u8ba2\u9605\u8005\u5c31\u80fd\u591f\u63a5\u53d7\u5230\u8fd9\u6761\u6d88\u606f\uff0c\u5982\u679c\u6709\u591a\u4e2a\u8ba2\u9605\u8005\uff0c\u90a3\u4e48\u4ed6\u4eec\u4f1a\u63a5\u53d7\u5230\u76f8\u540c\u7684\u6d88\u606f\u3002<\/p>\n

\u529f\u80fd\u5b9e\u73b0<\/strong><\/div>\n
\u53d1\u5e03\u6d88\u606f<\/strong><\/span><\/div>\n

Redis \u91c7\u7528 PUBLISH \u547d\u4ee4\u53d1\u9001\u6d88\u606f\uff0c\u5176\u8fd4\u56de\u503c\u4e3a\u63a5\u6536\u5230\u8be5\u6d88\u606f\u7684\u8ba2\u9605\u8005\u7684\u6570\u91cf\u3002<\/p>\n

\"\"<\/p>\n

\u8ba2\u9605\u9891\u9053<\/strong><\/span><\/div>\n

Redis \u91c7\u7528 SUBSCRIBE \u547d\u4ee4\u8ba2\u9605\u67d0\u4e2a\u9891\u9053\uff0c\u5176\u8fd4\u56de\u503c\u5305\u62ec\u5ba2\u6237\u7aef\u8ba2\u9605\u7684\u9891\u9053\uff0c\u76ee\u524d\u5df2\u8ba2\u9605\u7684\u9891\u9053\u6570\u91cf\uff0c\u4ee5\u53ca\u63a5\u6536\u5230\u7684\u6d88\u606f\uff0c\u5176\u4e2dsubscribe\u8868\u793a\u5df2\u7ecf\u6210\u529f\u8ba2\u9605\u4e86\u67d0\u4e2a\u9891\u9053\u3002<\/p>\n

\"\"<\/p>\n

\u6a21\u5f0f\u5339\u914d<\/strong><\/span><\/div>\n

\u6a21\u5f0f\u5339\u914d\u529f\u80fd\u5141\u8bb8\u5ba2\u6237\u7aef\u8ba2\u9605\u7b26\u5408\u67d0\u4e2a\u6a21\u5f0f\u7684\u9891\u9053\uff0cRedis\u91c7\u7528PSUBSCRIBE\u8ba2\u9605\u7b26\u5408\u67d0\u4e2a\u6a21\u5f0f\u6240\u6709\u9891\u9053\uff0c\u7528\u201c * \u201d\u8868\u793a\u6a21\u5f0f\uff0c\u201c * \u201d\u53ef\u4ee5\u88ab\u4efb\u610f\u503c\u4ee3\u66ff\u3002<\/p>\n

\"\"<\/p>\n

\u5047\u8bbe\u5ba2\u6237\u7aef\u540c\u65f6\u8ba2\u9605\u4e86\u67d0\u79cd\u6a21\u5f0f\u548c\u7b26\u5408\u8be5\u6a21\u5f0f\u7684\u67d0\u4e2a\u9891\u9053\uff0c\u90a3\u4e48\u53d1\u9001\u7ed9\u8fd9\u4e2a\u9891\u9053\u7684\u6d88\u606f\u5c06\u88ab\u5ba2\u6237\u7aef\u63a5\u6536\u5230\u4e24\u6b21\uff0c\u53ea\u4e0d\u8fc7\u8fd9\u4e24\u6761\u6d88\u606f\u7684\u7c7b\u578b\u4e0d\u540c\uff0c\u4e00\u4e2a\u662f message \u7c7b\u578b\uff0c\u4e00\u4e2a\u662f pmessage \u7c7b\u578b\uff0c\u4f46\u5176\u5185\u5bb9\u76f8\u540c\u3002<\/p>\n

\u53d6\u6d88\u8ba2\u9605<\/strong><\/span><\/div>\n

Redi s\u91c7\u7528 UNSUBSCRIBE \u548c PUNSUBSCRIBE \u547d\u4ee4\u53d6\u6d88\u8ba2\u9605\uff0c\u5176\u8fd4\u56de\u503c\u4e0e\u8ba2\u9605\u7c7b\u4f3c\u3002\u7531\u4e8eRedis\u7684\u8ba2\u9605\u64cd\u4f5c\u662f\u963b\u585e\u5f0f\u7684\uff0c\u56e0\u6b64\u4e00\u65e6\u5ba2\u6237\u7aef\u8ba2\u9605\u4e86\u67d0\u4e2a\u9891\u9053\u6216\u6a21\u5f0f\uff0c\u5c31\u5c06\u4f1a\u4e00\u76f4\u5904\u4e8e\u8ba2\u9605\u72b6\u6001\u76f4\u5230\u9000\u51fa\u3002\u5728 SUBSCRIBE\uff0cPSUBSCRIBE\uff0cUNSUBSCRIBE \u548c PUNSUBSCRIBE \u547d\u4ee4\u4e2d\uff0c\u5176\u8fd4\u56de\u503c\u90fd\u5305\u542b\u4e86\u8be5\u5ba2\u6237\u7aef\u5f53\u524d\u8ba2\u9605\u7684\u9891\u9053\u548c\u6a21\u5f0f\u7684\u6570\u91cf\uff0c\u5f53\u8fd9\u4e2a\u6570\u91cf\u53d8\u4e3a0\u65f6\uff0c\u8be5\u5ba2\u6237\u7aef\u4f1a\u81ea\u52a8\u9000\u51fa\u8ba2\u9605\u72b6\u6001\u3002<\/p>\n

SpringBoot+Redis \u5b9e\u73b0\u53d1\u5e03\u8ba2\u9605<\/strong><\/div>\n

pringboot \u5982\u4f55\u6574\u5408 redis \u6211\u8fd9\u91cc\u5c31\u4e0d\u8bb2\u4e86\uff0c\u76f8\u4fe1\u5bf9\u4f60\u6765\u8bf4\u4e5f\u6ca1\u6709\u4e00\u70b9\u95ee\u9898\u3002\u6211\u4eec\u76f4\u63a5\u4e0a\u4ee3\u7801<\/p>\n

\u6d88\u606f\u76d1\u542c\u914d\u7f6e<\/strong><\/span><\/div>\n
\r\n@Configuration\r\npublic class RedisSubConfig {\r\n\r\n    public static final String SUB_KEY = \"chat\";\/\/\u9891\u9053channel\r\n\r\n    \/**\r\n     * redis\u6d88\u606f\u76d1\u542c\u5668\u5bb9\u5668\r\n     * \u53ef\u4ee5\u6dfb\u52a0\u591a\u4e2a\u76d1\u542c\u4e0d\u540c\u8bdd\u9898\u7684redis\u76d1\u542c\u5668\uff0c\u53ea\u9700\u8981\u628a\u6d88\u606f\u76d1\u542c\u5668\u548c\u76f8\u5e94\u7684\u6d88\u606f\u8ba2\u9605\u5904\u7406\u5668\u7ed1\u5b9a\uff0c\u8be5\u6d88\u606f\u76d1\u542c\u5668\r\n     * \u901a\u8fc7\u53cd\u5c04\u6280\u672f\u8c03\u7528\u6d88\u606f\u8ba2\u9605\u5904\u7406\u5668\u7684\u76f8\u5173\u65b9\u6cd5\u8fdb\u884c\u4e00\u4e9b\u4e1a\u52a1\u5904\u7406\r\n     * @param connectionFactory\r\n     * @param listenerAdapter\r\n     * @return\r\n     *\/\r\n    @Bean\r\n    RedisMessageListenerContainer container(RedisConnectionFactory connectionFactory, MessageListenerAdapter listenerAdapter) {\r\n\r\n        RedisMessageListenerContainer container = new RedisMessageListenerContainer();\r\n        container.setConnectionFactory(connectionFactory);\r\n\r\n        \/\/\u8ba2\u9605\u4e86\u4e00\u4e2a\u9891\u9053\r\n        container.addMessageListener(listenerAdapter, new PatternTopic(RedisSubConfig.SUB_KEY));\r\n        return container;\r\n    }\r\n\r\n    \/**\r\n     * \u6d88\u606f\u76d1\u542c\u5668\u9002\u914d\u5668\uff0c\u7ed1\u5b9a\u6d88\u606f\u5904\u7406\u5668\uff0c\u5229\u7528\u53cd\u5c04\u6280\u672f\u8c03\u7528\u6d88\u606f\u5904\u7406\u5668\u7684\u4e1a\u52a1\u65b9\u6cd5\r\n     * @param receiver\r\n     * @return\r\n     *\/\r\n    @Bean\r\n    MessageListenerAdapter listenerAdapter(RedisReceiver receiver) {\r\n        return new MessageListenerAdapter(receiver, \"receiveMessage\");\r\n    }\r\n\r\n    \/**\r\n     * redis \u8bfb\u53d6\u5185\u5bb9\u7684template\r\n     * @param connectionFactory\r\n     * @return\r\n     *\/\r\n    @Bean\r\n    StringRedisTemplate template(RedisConnectionFactory connectionFactory) {\r\n        return new StringRedisTemplate(connectionFactory);\r\n    }\r\n}\r\n<\/pre>\n
\u63a5\u6536\u6d88\u606f<\/strong><\/span><\/div>\n
\r\n@Service\r\npublic class RedisReceiver {\r\n\r\n    public void receiveMessage(String message) {\r\n        System.out.println(\"\u63a5\u6536\u6d88\u606f\uff1a\" + message);\r\n    }\r\n}\r\n<\/pre>\n
\u91c7\u7528\u5b9a\u65f6\u5668\u53d1\u5e03\u6d88\u606f<\/strong><\/span><\/div>\n
\r\n@EnableScheduling \/\/\u5f00\u542f\u5b9a\u65f6\u5668\u529f\u80fd\r\n@Component\r\npublic class MessageSender {\r\n\r\n    @Autowired\r\n    private StringRedisTemplate stringRedisTemplate;\r\n\r\n    @Scheduled(fixedRate = 5000) \/\/\u95f4\u96945s \u901a\u8fc7StringRedisTemplate\u5bf9\u8c61\u5411redis\u6d88\u606f\u961f\u5217chat\u9891\u9053\u53d1\u5e03\u6d88\u606f\r\n    public void sendMessage(){\r\n        stringRedisTemplate.convertAndSend(\"chat\", \"hello \"+ new Date());\r\n    }\r\n}\r\n<\/pre>\n

\u8fd0\u884c\u7ed3\u679c<\/p>\n

\"\"<\/p>\n","protected":false},"excerpt":{"rendered":"

Redis \u63d0\u4f9b\u4e86\u53d1\u5e03\u8ba2\u9605\u529f\u80fd\uff0c\u53ef\u4ee5\u7528\u4e8e\u6d88\u606f\u7684\u4f20\u8f93\uff0cRedis \u7684\u53d1\u5e03\u8ba2\u9605\u673a\u5236\u5305\u62ec\u4e09\u4e2a\u90e8\u5206\uff0c\u53d1\u5e03\u8005\uff0c\u8ba2\u9605\u8005\u548c […]<\/p>\n","protected":false},"author":1903,"featured_media":235037,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-234402","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/234402","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/1903"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=234402"}],"version-history":[{"count":2,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/234402\/revisions"}],"predecessor-version":[{"id":234409,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/234402\/revisions\/234409"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/235037"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=234402"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=234402"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=234402"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}