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