SpingBoot整合哨兵

哨兵搭建

1:SpringBoot整合redis哨兵

redis.properties文件
	redis.sentinel.masterName=mymaster
	redis.sentinels=192.168.17.136:26379   #ip地址改成自己的

配置类
	/**
 * @author Administrator
 * @date 2019-07-19 上午 10:58
 * //标识我是一个配置类
 */
@Configuration
@PropertySource("classpath:/properties/redis.properties")
public class RedisConfig {
	//注入属性
    @Value("${redis.sentinel.masterName}")
    private String masterName;
    @Value("${redis.sentinels}")
    private String nodes;


    /**
     * @return 返回单例对象
     */
    @Bean(name = "jedisSentinelPool")    //该对象是单例的
    public JedisSentinelPool jedisSentinelPool() {
        //添加到Set里面,id地址和端口
        Set<String> sentinels = new HashSet<>();
        sentinels.add(nodes);
        JedisSentinelPool pool = new JedisSentinelPool(masterName, sentinels);
        return pool;
    }

    @Bean(name = "jedis")
    @Scope("prototype")    //多例对象
    public Jedis jedis(@Autowired @Qualifier("jedisSentinelPool") JedisSentinelPool pool) {
    	//获取上面JedisSentinelPool 里面的jedis对象,
        Jedis jedis = pool.getResource();
        return jedis;
    }
}

测试类
@RunWith(SpringRunner.class)
@SpringBootTest
public class RedisApplicationTests {

    @Test
    public void contextLoads() {
    }

    @Qualifier("jedis")
    @Autowired
    private Jedis jedis;
    @Test
    public void testSentinel() {
        jedis.set("a", "HELLO");
        //输出结果HELLO,哨兵整合完毕
        System.out.println(jedis.get("a"));
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值