哨兵搭建
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"));
}
}