SpringBoot使用Redis发布订阅pubsub

1、pom.xml


    org.springframework.boot
    spring-boot-starter-web


    org.springframework.boot
    spring-boot-starter-data-redis


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 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 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上,然后公开地址在这里,希望能够帮助到你!

你可能感兴趣的:(SpringBoot使用Redis发布订阅pubsub)