SpringBoo整合Redis实现简单发布/订阅
作者:互联网
一、添加pom
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
二、配置RedisConfig
package com.example.redismqdemo;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import java.net.UnknownHostException;
@Configuration
public class RedisConfig {
@Bean
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory)
throws UnknownHostException {
RedisTemplate<String, Object> template = new RedisTemplate<>();
template.setConnectionFactory(factory);
// Json序列化配置
Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = 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.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL); //@Deprecated 已过时
jackson2JsonRedisSerializer.setObjectMapper(om);
//配置具体的序列化方式
StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
//key采用String的序列化方式
template.setKeySerializer(stringRedisSerializer);
//hash的key也采用String的序列化方式
template.setHashKeySerializer(stringRedisSerializer);
//value序列化采用jackson
template.setValueSerializer(jackson2JsonRedisSerializer);
template.setHashValueSerializer(jackson2JsonRedisSerializer);
template.afterPropertiesSet();
return template;
}
}
三、配置消费端
package com.example.redismqdemo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
@Component
public class Receiver {
@Autowired
private RedisTemplate<String,Object> redisTemplate;
public void onMsg(String msg){
System.out.println("hhhhhhhhhhh");
System.out.println("get msg:::"+msg.toString());
}
}
四、配置RedisMessageListenerContainer
package com.example.redismqdemo;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
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;
@Configuration
@AutoConfigureAfter(Receiver.class)
public class SubscribeConfig {
@Bean
public MessageListenerAdapter myMsgAdpater(Receiver receiver) {
return new MessageListenerAdapter(receiver, "onMsg");
}
@Bean
public RedisMessageListenerContainer myRedisMsgListenerContainer(RedisConnectionFactory redisConnectionFactory,MessageListenerAdapter myMsgAdpater){
RedisMessageListenerContainer redisMessageListenerContainer = new RedisMessageListenerContainer();
redisMessageListenerContainer.setConnectionFactory(redisConnectionFactory);
redisMessageListenerContainer.addMessageListener(myMsgAdpater,new PatternTopic("topic"));
return redisMessageListenerContainer;
}
}
PS:可以订阅多个topic,创建不同的MessageListenerAdapter即可,通过Adapter绑定消费方法。
五、测试(生产端)
@SpringBootTest
class RedisMqDemoApplicationTests {
@Autowired
RedisTemplate<String,Object> redisTemplate;
@Test
void contextLoads() {
String channel = "topic";
redisTemplate.convertAndSend(channel, "hello world");
}
}
标签:SpringBoo,订阅,redis,Redis,springframework,template,import,org,data 来源: https://blog.csdn.net/u014681799/article/details/118905855