SpringBoot使用Redis发布订阅pubsub

1、pom.xml

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>

2、application.yml

spring:
    redis:
        database: 0   # Redis数据库索引(默认为0)
        host: localhost  # Redis服务器地址
        port: 6379  # Redis服务器连接端口
        password:    # Redis服务器连接密码(默认为空)
        timeout: 0  # 连接超时时间(毫秒)
        pool:
          max-active: -1 # 连接池最大连接数(使用负值表示没有限制)
          max-wait: -1  # 连接池最大阻塞等待时间(使用负值表示没有限制)
          max-idle: 8  # 连接池中的最大空闲连接
          min-idle: 0  # 连接池中的最小空闲连接
server:
    port: 8989

3、 启动类+java配置

Application.java

import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.listener.PatternTopic;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.data.redis.listener.adapter.MessageListenerAdapter;

@Bean
RedisMessageListenerContainer container(RedisConnectionFactory connectionFactory,
                                        MessageListenerAdapter listenerAdapter) {
    RedisMessageListenerContainer container = new RedisMessageListenerContainer();
    container.setConnectionFactory(connectionFactory);
    container.addMessageListener(listenerAdapter, new PatternTopic("testkafka"));
    container.addMessageListener(listenerAdapter, new PatternTopic("testkafka1"));//配置要订阅的订阅项
    return container;
}

4、消息监听类

RedisSubscriber.java

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.listener.adapter.MessageListenerAdapter;
import org.springframework.stereotype.Component;

@Component
public class RedisSubscriber extends MessageListenerAdapter {

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    @Override
    public void onMessage(Message message, byte[] bytes) {
        System.out.println(message);
        //byte[] body = message.getBody();
        //byte[] channel = message.getChannel();
        //String msg = redisTemplate.getStringSerializer().deserialize(body);
        //String topic = redisTemplate.getStringSerializer().deserialize(channel);
       // System.out.println("监听到topic为" + topic + "的消息:" + msg);
    }

}

5、模拟生产者

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class SampleController {

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    /**
     * redis生产者测试
     * @param data
     * @return
     */
    @GetMapping("/send1")
    String send1(String data) {
        redisTemplate.convertAndSend("testkafka", data);
        return "success";
    }
    /**
     * redis生产者测试
     * @param data
     * @return
     */
    @GetMapping("/send2")
    String send2(String data) {
        redisTemplate.convertAndSend("testkafka1", data);
        return "success";
    }

}

6、试用

打开浏览器

localhost:8989/send1?data=test1
localhost:8989/send2?data=test2

随后我会将自己写的一些Demo开源到Github上,然后公开地址在这里,希望能够帮助到你!

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 136,665评论 19 139
  • Spring Boot 参考指南 介绍 转载自:https://www.gitbook.com/book/qbgb...
    毛宇鹏阅读 47,282评论 6 342
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 179,263评论 25 708
  • 今天我们村健康查体,我们村分在东王家村,早晨7点我就到东王家村去做准备,不到7点半我们村的育龄妇女就有过来的...
    王煜妈妈阅读 228评论 0 0
  • 儿子今年五岁,当然不在合法生育内,五年前属超生,五年前为了要这个宝贝,我辞去工作,东奔西藏担惊受怕,在外躲避八个月...
    沐源工作室阅读 325评论 4 2

友情链接更多精彩内容