**
一、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;
}
}