Springboot2.X集成redis集(Lettuce)连接的⽅法1. 新建⼯程,l⽂件中添加redis⽀持
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
2.配置application.properties
3.      新建下⾯的两个类
@Configuration
public class RedisConfiguration {
@Resource
private LettuceConnectionFactory myLettuceConnectionFactory;
@Bean
public RedisTemplate<String, Serializable> redisTemplate() {
RedisTemplate<String, Serializable> template = new 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<String, Object> source = new HashMap<String, Object>();
source.put("des", Property("des"));
source.put("dis.cluster.timeout", Property("dis.cluster.timeout"));
spring framework jar包
source.put("dis.cluster.max-redirects", Property("dis.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,有如下显⽰,证明成功。
以上就是本⽂的全部内容,希望对⼤家的学习有所帮助,也希望⼤家多多⽀持。

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。