redis publish/subscribe命令
subscribe channel [channel ...] : 订阅频道信息
publish channel message : 将信息发布到指定的频道
unsubscribe [channel [channel ...]] : 退订指定的频道
以下演示在redis的操作
- 客户端1订阅CCTV1和CCTV2频道
- 客户端2订阅CCTV1频道
- 客户端3向CCTV1发布消息
Springboot代码实现
1) pox.xml
<!-- redis模块 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<!--测试模块-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
2) 创建RedisReceiver类
//类名可以随便起
public class RedisReceiver {
//方法可以随便起 主要是用来处理消息的方法
public void receiveMessage(String message){
System.out.println(message);
}
}
3) RedisConfig配置类
@Configuration
@EnableCaching //开启注解
public class RedisConfig extends CachingConfigurerSupport {
/**
* retemplate相关配置
* @param factory
* @return
*/
@Bean("redisTemplate")
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
RedisTemplate<String, Object> template = new RedisTemplate<>();
// 配置连接工厂
template.setConnectionFactory(factory);
//使用Jackson2JsonRedisSerializer来序列化和反序列化redis的value值(默认使用JDK的序列化方式)
Jackson2JsonRedisSerializer jacksonSeial = new Jackson2JsonRedisSerializer(Object.class);
ObjectMapper om = new ObjectMapper();
// 指定要序列化的域,field,get和set,以及修饰符范围,ANY是都有包括private和public
om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
// 指定序列化输入的类型,类必须是非final修饰的,final修饰的类,比如String,Integer等会跑出异常
om.activateDefaultTyping(LaissezFaireSubTypeValidator.instance, ObjectMapper.DefaultTyping.NON_FINAL);
jacksonSeial.setObjectMapper(om);
// 值采用json序列化
//template.setValueSerializer(jacksonSeial);
//使用StringRedisSerializer来序列化和反序列化redis的key值
template.setKeySerializer(stringRedisSerializer());
// 设置hash key 和value序列化模式
template.setHashKeySerializer(stringRedisSerializer());
//template.setHashValueSerializer(jacksonSeial);
template.afterPropertiesSet();
return template;
}
/**
* redis序列化
* @return
*/
@Bean
public StringRedisSerializer stringRedisSerializer(){
return new StringRedisSerializer();
}
/**
* redis实现消息队列
* @param connectionFactory
* @return
*/
@Bean
public RedisMessageListenerContainer redisMessageListenerContainer(RedisConnectionFactory connectionFactory){
RedisMessageListenerContainer container = new RedisMessageListenerContainer();
container.setConnectionFactory(connectionFactory);
//订阅不同的频道
container.addMessageListener(messageListenerAdapter(), new PatternTopic("topic"));
container.addMessageListener(messageListenerAdapter(), new PatternTopic("topic1"));
container.addMessageListener(messageListenerAdapter2(), new PatternTopic("topic"));
return container;
}
@Bean
//频道2
public MessageListenerAdapter messageListenerAdapter(){
return new MessageListenerAdapter(new RedisReceiver(), "receiveMessage");
}
@Bean
//频道2
public MessageListenerAdapter messageListenerAdapter2(){
return new MessageListenerAdapter(new RedisReceiver(), "receiveMessage");
}
}
4) RedisUtils工具类
@Component
@Slf4j
public class RedisUtils {
@Autowired
@Qualifier("redisTemplate")
private RedisTemplate<String, Object> redisTemplate;
//发送消息
public void convertAndSend(String channel, Object message){
redisTemplate.convertAndSend(channel,message);
}
}
5) RedisMessageQueue测试类
@SpringBootTest(classes = SmartConstrolCostApplication.class)
@RunWith(SpringRunner.class)
public class RedisMessageQueue {
@Autowired
private RedisUtils redisUtils;
/**
* redis实现消息队列
*/
@Test
public void sendMessage(){
redisUtils.convertAndSend("topic","1232141312");
}
}