jedis的使用,spring整合redis,springboot整合redis
2020-12-22 21:29
标签:区别 wired val 取消 自定义 isa cto 转换 idle 很简单,看我的github的例子就行 2.1导包 2.2配置applicationContext.xml Spring提供了很多序列化器 Jackson2JsonRedisSerializer JdkSerializationRedisSerializer OxmSerializer StringRedisSerializer GenericToStringRedisSerializer GenericJackson2JsonRedisSerializer 2.3测试 ApplicationContext
applicationContext = new ClassPathXmlApplicationContext( 2.4
StringRedisTemplete和RedisTemplete的区别 StringRedisTemplete继承RedisTemplete,内部使用string序列化器,所以适合操作key和value都是string的数据 3 Spring中发布订阅 3.1首先提供接收消息的类,它将实现 org.springframework.data.redis.connection.MessageListener 接口,并实现接口定义的方法 public void onMessage(Message
message,byte[]pattern) /*** imports ***/ 23.
} 3.2.在spring中配置自定义的监听器 3.4 测试,像chat通道发送消息 4 springboot整合redis 4.1新建springboot项目,勾选上redis 4.2配置application.yml 4.2 配置redis的配置类 @Configuration 4.3 测试 @SpringBootTest 4.4 配置订阅发布 4.4.1新建类实现MessageListener 4.4.2 配置redisConfig,把messageListener和containner放入容器中 @Bean 4.4.3测试 @Test jedis的使用,spring整合redis,springboot整合redis 标签:区别 wired val 取消 自定义 isa cto 转换 idle 原文地址:https://www.cnblogs.com/zhouyanger/p/13215534.html
spring-data-redis
jedis
"applicationContext.xml");
RedisTemplate
redisTemplate = applicationContext
.getBean(RedisTemplate.class);
3.3 要给一个监听容器,在 Spring 中已有类 org.springframework.data.redis.listener.RedisMessageListenerContainer。它可以用于监听 Redis 的发布订阅消息,下面的配置就是为了实现这个功能,读者可以通过注释来了解它的配置要点。
spring:
redis:
host: 127.0.0.1
port: 6379 jedis:
pool:
max-active: 100
max-idle: 10
max-wait: 20000
lettuce:
shutdown-timeout: 1000
public class RedisConfig {
@Bean
public RedisTemplate
RedisTemplate
redisTemplate.setConnectionFactory(redisConnectionFactory);
redisTemplate.setKeySerializer(stringRedisSerializer());
redisTemplate.setValueSerializer(jdkSerializationRedisSerializer());
return redisTemplate;
}
@Bean
public StringRedisSerializer stringRedisSerializer(){
return new StringRedisSerializer();
}
@Bean
public JdkSerializationRedisSerializer jdkSerializationRedisSerializer(){
return new JdkSerializationRedisSerializer();
}
}
class SpringbootRedisApplicationTests {
@Autowired
private RedisTemplate
@Test
void contextLoads() {
redisTemplate.opsForValue().set("myname","zhouyang");
String myname = (String)redisTemplate.opsForValue().get("myname");
System.out.println(myname);
}
} public class MyRedisMessageListener implements MessageListener {
@Autowired
private RedisTemplate
@Override
public void onMessage(Message message, byte[] bytes) {
// 获取消息
byte[] body = message.getBody();
// 使用值序列化器转换
String msgBody = (String) redisTemplate.getValueSerializer()
.deserialize(body);
System.err.println(msgBody);
// 获取 channel
byte[] channel = message.getChannel();
// 使用字符串序列化器转换
String channelStr = (String) redisTemplate.getStringSerializer()
.deserialize(channel);
System.err.println(channelStr);
// 渠道名称转换
String bytesStr = new String(bytes);
System.err.println(bytesStr);
}
}
public MyRedisMessageListener myRedisMessageListener(){
return new MyRedisMessageListener();
}
@Bean
public RedisMessageListenerContainer redisMessageListenerContainer (RedisConnectionFactory redisConnectionFactory){
RedisMessageListenerContainer
redisMessageListenerContainer = new RedisMessageListenerContainer();
redisMessageListenerContainer.setConnectionFactory(redisConnectionFactory);
redisMessageListenerContainer.addMessageListener(myRedisMessageListener(),new PatternTopic("chat"));
return redisMessageListenerContainer;
}
void testPublish() {
redisTemplate.convertAndSend("chat","我是周扬");
}
文章标题:jedis的使用,spring整合redis,springboot整合redis
文章链接:http://soscw.com/index.php/essay/37652.html