java中的接口是类吗
529
2023-01-25
Springboot2.X集成redis集群(Lettuce)连接的方法
前提:搭建好redis集群环境,搭建方式请看:https://jb51.net/article/143749.htm
1. 新建工程,pom.xml文件中添加redis支持
2.配置application.properties
spring.redis.cluster.nodes=127.0.0.1:6380,127.0.0.1:6381,127.0.0.1:6382,127.0.0.1:6383,127.0.0.1:6384,127.0.0.1:6385
spring.redis.cluster.timeout=1000
spring.redis.cluster.max-redirects=3
3. 新建下面的两个类
@Configuration
public class RedisConfiguration {
@Resource
private LettuceConnectionFactory myLettuceConnectionFactory;
@Bean
public RedisTemplate
RedisTemplate
template.setKeySerializer(new StringRedisSerializer());
template.setValueSerializer(new GenericJackson2jsonRedisSerializer());
template.setConnectionFactory(myLettuceConnectionFactory);
return template;
}
}
@Configuration
public class RedisFactoryConfig {
@Autowired
private Environment environment;
@Bean
public RedisConnectionFactory myLettuceConnectionFactory() {
Map
source.put("spring.redis.cluster.nodes", environment.getProperty("spring.redis.cluster.nodes"));
source.put("spring.redis.cluster.timeout", environment.getProperty("spring.redis.cluster.timeout"));
source.put("spring.redis.cluster.max-redirects", environment.getProperty("spring.redis.cluster.max-redirects"));
RedisClusterConfiguration redisClusterConfiguration;
redisClusterConfiguration = new RedisClusterConfiguration(new MapPropertySource("RedisClusterConfiguration", source));
return new LettuceConnectionFactory(redisClusterConfiguration);
}
}
4. 执行测试
@SpringBootTest
@RunWith(SpringRunner.class)
public class RedisConfigurationTest {
@Autowired
private RedisTemplate redisTemplate;
@Test
public void redisTemplate() throws Exception {
redisTemplate.opsForValue().set("author", "Damein_xym");
}
}
5. 验证,使用Redis Desktop Manager 连接redis节点,查看里面的数据是否存在author,有如下显示,证明成功。
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~