欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

springboot结合redis实现redis订阅发布模式

程序员文章站 2022-07-05 10:35:23
...

redis订阅发布模式:

Redis提供了发布订阅功能,可以用于消息的传输,Redis的发布订阅机制包括三个部分,发布者,订阅者和Channel。 springboot结合redis实现redis订阅发布模式

发布者和订阅者都是Redis客户端,Channel则为Redis服务器端,发布者将消息发送到某个的频道,订阅了这个频道的订阅者就能接收到这条消息。Redis的这种发布订阅机制与基于主题的发布订阅类似,Channel相当于主题。

实现功能:

通过网页访问一个网址的时候实现redis在某个频道的的发布,同时订阅了该频道的redis订阅者会做出相应的反应。

要想让redis在web容器开启时就一直处于订阅状态,考虑通过listener来实现:

Listener:

@Configuration
@WebListener
public class MyListenerOnServlet implements ServletContextListener{
    @Override
    public void contextInitialized(ServletContextEvent servletContextEvent) {
        System.out.println("ServletContext容器初始化了。。。");
        Thread threadOnRedis = new TestSubscribe();
        threadOnRedis.start();

    }

    @Override
    public void contextDestroyed(ServletContextEvent servletContextEvent) {
        System.out.println("ServletContext容器销毁了。。。");
    }

}
TestSubScribe线程类:

public class TestSubscribe extends Thread {


    @Override
    public void run() {
        JedisShardInfo jedisShardInfo = new JedisShardInfo("127.0.0.1", 6379);
        jedisShardInfo.setPassword("admin");
        Jedis jedis = new Jedis(jedisShardInfo);
        RedisMsgPubSubListener listener = new RedisMsgPubSubListener();
        jedis.subscribe(listener, "redisChatTest");
        System.out.println("不阻塞");
    }
}

RedisMsgPubSubListener类:

public class RedisMsgPubSubListener extends JedisPubSub {
    @Override
    public void unsubscribe() {
        super.unsubscribe();
    }

    @Override
    public void unsubscribe(String... channels) {
        super.unsubscribe(channels);
    }

    @Override
    public void subscribe(String... channels) {
        super.subscribe(channels);
    }

    @Override
    public void psubscribe(String... patterns) {
        super.psubscribe(patterns);
    }

    @Override
    public void punsubscribe() {
        super.punsubscribe();
    }

    @Override
    public void punsubscribe(String... patterns) {
        super.punsubscribe(patterns);
    }

    @Override
    public void onMessage(String channel, String message) {
        System.out.println("channel:" + channel + "receives message :" + message);
//        this.unsubscribe();
    }

    @Override
    public void onPMessage(String pattern, String channel, String message) {

    }

    @Override
    public void onSubscribe(String channel, int subscribedChannels) {
        System.out.println("channel:" + channel + "is been subscribed:" + subscribedChannels);
    }

    @Override
    public void onPUnsubscribe(String pattern, int subscribedChannels) {

    }

    @Override
    public void onPSubscribe(String pattern, int subscribedChannels) {

    }

    @Override
    public void onUnsubscribe(String channel, int subscribedChannels) {
        System.out.println("channel:" + channel + "is been unsubscribed:" + subscribedChannels);
    }


}


通过controller的访问来触发redis的发布:

Controller:

@RequestMapping("/t07")
    public String test07(@RequestParam("msg") String s){
        testService.testRedisForMsg(s);
        return "完成";
    }
Service:

@Service
public class TestService {

    @Autowired
    private JedisPool jedisPool;

    @Autowired
    private UserMapper userMapper;

    

    public void testRedisForMsg(String msg){
        Jedis jedis = jedisPool.getResource();
        try{
            jedis.publish("redisChatTest", msg);
        }catch (Exception e){
            System.out.println("发布出错:"+e.toString());
        }finally {
            if(jedis!=null){
                jedis.close();
            }
        }
    }



}
jedispool配置就不解释了。差不多就是这样,在浏览器访问触发redis发布的时候,本例的响应是在控制台输出发布信息。

输出信息:

channel:redisChatTestreceives message :lvelvelve
相应可以自己决定,感觉又学到了好玩的东西。