Redis之Pub/Sub和在springboot2中的使用

**

一、Redis之Pub/Sub

**
Sub(订阅者):
打开一个redis客户端名为R1,执行如下命令(意思是订阅名为news的频道)

127.0.0.1:6379> subscribe news

如图:
在这里插入图片描述
Pub(发布者):
再次打开一个redis客户端名为R2,执行如下命令(意思是在news频道里发布消息)

127.0.0.1:6379> publish news hello

如图:
在这里插入图片描述
此时,redis客户端R1就会收到来自客户端R2发布过来的消息,如图:
在这里插入图片描述



**

二、springboot2中redis订阅者和发布者的使用:

**

package com.example.shopgoods.controller.redisTest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.connection.MessageListener;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.listener.PatternTopic;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.data.redis.listener.adapter.MessageListenerAdapter;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.io.UnsupportedEncodingException;

/**
 * @Author: zp
 * @Date: 2019/4/28 13:31
 * @Description:
 */
@RestController
@RequestMapping("/redis")
public class RedisPub_Sub_TestController {

    @Autowired
    private StringRedisTemplate redisTemplate;

    /**
     * 发布者
     * @param message
     */
    @PostMapping("/publish")
    public void publish(@RequestParam(value = "message") String message) {
        redisTemplate.convertAndSend("news", message);

    }


    /**
     * subscribe 订阅者
     */
    class MyRedisChannelListener implements MessageListener {
        @Override
        public void onMessage(Message message, byte[] pattern) {
            byte[] channel = message.getChannel();
            byte[] body = message.getBody();

            try {
                String content = new String(body,"UTF-8");
                String address = new String(channel,"UTF-8");
                System.out.println("get " + content + " from " + address);
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
        }
    }


    @Bean
    MessageListenerAdapter messageListenerAdapter() {
        return new MessageListenerAdapter(new MyRedisChannelListener());
    }

    @Bean
    RedisMessageListenerContainer container(RedisConnectionFactory connectionFactory, MessageListenerAdapter listenerAdapter) {
        RedisMessageListenerContainer container = new RedisMessageListenerContainer();
        container.setConnectionFactory(connectionFactory);
        // news 频道名称
        container.addMessageListener(listenerAdapter, new PatternTopic("news"));
        return container;
    }

}

利用postMan工具访问如下地址:

 http://localhost:8082/redis/publish?message=小学生放假

idea控制台下会打印出发布者发出的消息如下图:
在这里插入图片描述



或者创建一个类,注入spring容器中,等待发布者消息的发出,代码如下:

package com.example.shopgoods.controller.redisTest;

import org.springframework.context.annotation.Bean;
import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.connection.MessageListener;
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;
import org.springframework.stereotype.Component;

import java.io.UnsupportedEncodingException;

/**
 * @Author: zp
 * @Date: 2019/4/28 11:53
 * @Description:
 */
@Component
public class MyRedisChannelListener implements MessageListener {
    @Override
    public void onMessage(Message message, byte[] pattern) {
        byte[] channel = message.getChannel();
        byte[] body = message.getBody();
        try {
            String content = new String(body,"UTF-8");
            String address = new String(channel,"UTF-8");
            System.out.println("get " + content + " from " + address);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
    }

    @Bean
    MessageListenerAdapter messageListenerAdapter() {
        return new MessageListenerAdapter(new MyRedisChannelListener());
    }

    @Bean
    RedisMessageListenerContainer container(RedisConnectionFactory connectionFactory, MessageListenerAdapter listenerAdapter) {
        RedisMessageListenerContainer container = new RedisMessageListenerContainer();
        container.setConnectionFactory(connectionFactory);
        container.addMessageListener(listenerAdapter, new PatternTopic("news"));
        return container;

    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值