java哨铺,redis集群之哨兵模式

该博客介绍了如何在SpringBoot中配置Redis哨兵模式,以实现故障自动转移和主从复制。配置包括数据库设置、密码、连接池参数以及哨兵的服务器节点列表。通过StringRedisTemplate和RedisTemplate操作Redis,并展示了set、get和删除键的方法。哨兵模式目前不支持负载均衡,但能确保高可用性。
摘要由CSDN通过智能技术生成

redis集群之哨兵模式

2、与springboot集成 这里哨兵模式暂时只提供了故障自动转移等,暂时不提供负载均衡功能,自动提供了故障转移和主从复制功能

配置

spring.redis.database=0

spring.redis.password=123456

# pool settings ...池配置

spring.redis.pool.max-idle=8

spring.redis.pool.min-idle=0

spring.redis.pool.max-active=8

spring.redis.pool.max-wait=-1

#哨兵监听redis server名称

spring.redis.sentinel.master=mymaster

#哨兵的配置列表

spring.redis.sentinel.nodes=192.168.12.194:26379,192.168.12.194:36379,192.168.12.194:4637

调用封装

[@Component](https://my.oschina.net/u/3907912)

public class RedisComponent {

@Autowired

//操作字符串的template,StringRedisTemplate是RedisTemplate的一个子集

private StringRedisTemplate stringRedisTemplate;

@Autowired

// RedisTemplate,可以进行所有的操作

private RedisTemplate redisTemplate;

public void set(String key, String value){

ValueOperations ops = this.stringRedisTemplate.opsForValue();

boolean bExistent = this.stringRedisTemplate.hasKey(key);

if (bExistent) {

System.out.println("this key is bExistent!");

}else{

ops.set(key, value);

}

}

public String get(String key){

return this.stringRedisTemplate.opsForValue().get(key);

}

public void del(String key){

this.stringRedisTemplate.delete(key);

}

public void sentinelSet(User user){

String key = null;

try {

key = new String(user.getId().getBytes("gbk"),"utf-8");

} catch (UnsupportedEncodingException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

System.out.println(key);

redisTemplate.opsForValue().set(key, user.toString());

}

public String sentinelGet(String key){

return stringRedisTemplate.opsForValue().get(key);

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值