redis主从集群搭建eclipse_Springboot2.X集成redis集群(Lettuce)连接

前提:搭建好redis集群环境,搭建方式请看:https://www.cnblogs.com/xymBlog/p/9300574.html

1. 新建工程,pom.xml文件中添加redis支持

org.springframework.boot

spring-boot-starter-data-redis

2.      配置application.properties

1 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

2

3 spring.redis.cluster.timeout=1000

4

5 spring.redis.cluster.max-redirects=3

3.      新建下面的两个类

@Configuration

public class RedisConfiguration {

@Resource

private LettuceConnectionFactory myLettuceConnectionFactory;

@Bean

public RedisTemplate redisTemplate() {

RedisTemplate 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 source = new HashMap();

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,有如下显示,证明成功。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值