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"));
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Boot中使用Redis哨兵模式可以提高Redis的高可用性和可靠性。下面是使用Spring Boot集成Redis哨兵模式的基本步骤: 1. 首先,在`pom.xml`文件中添加Redis和Spring Data Redis的依赖: ```xml <dependencies> <!-- Redis --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> </dependencies> ``` 2. 在`application.properties`(或`application.yml`)文件中配置Redis哨兵模式的相关参数: ```properties spring.redis.sentinel.master=your-master-name spring.redis.sentinel.nodes=host1:port1,host2:port2,host3:port3 ``` 其中,`your-master-name`是你的Redis主节点的名称,`host1:port1,host2:port2,host3:port3`是你的Redis哨兵节点的主机和端口列表。 3. 创建一个Redis配置类,用于配置Redis连接工厂和Redis模板: ```java import org.springframework.context.annotation.Configuration; import org.springframework.data.redis.connection.RedisConnectionFactory; import org.springframework.data.redis.connection.RedisSentinelConfiguration; import org.springframework.data.redis.connection.jedis.JedisConnectionFactory; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.serializer.StringRedisSerializer; @Configuration public class RedisConfig { // 配置Redis连接工厂 public RedisConnectionFactory redisConnectionFactory() { RedisSentinelConfiguration sentinelConfig = new RedisSentinelConfiguration() .master("your-master-name") .sentinel("host1", port1) .sentinel("host2", port2) .sentinel("host3", port3); return new JedisConnectionFactory(sentinelConfig); } // 配置Redis模板 public RedisTemplate<String, String> redisTemplate() { RedisTemplate<String, String> redisTemplate = new RedisTemplate<>(); redisTemplate.setConnectionFactory(redisConnectionFactory()); redisTemplate.setDefaultSerializer(new StringRedisSerializer()); return redisTemplate; } } ``` 在以上示例中,需要根据你的实际配置修改`your-master-name`、`host1`、`port1`等参数。 4. 在需要使用Redis的地方注入`RedisTemplate`并使用它来操作Redis数据: ```java import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.stereotype.Service; @Service public class MyService { @Autowired private RedisTemplate<String, String> redisTemplate; public void saveData(String key, String value) { redisTemplate.opsForValue().set(key, value); } public String getData(String key) { return redisTemplate.opsForValue().get(key); } } ``` 在上述示例中,我们使用`redisTemplate`来执行一些常见的Redis操作,比如设置值和获取值。 这样,就完成了Spring Boot中使用Redis哨兵模式的集成配置和使用。你可以根据自己的需求进一步扩展和优化。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值