springboot2 redis配置/哨兵sentinel配置和使用

1.新建一个springboot项目
在这里插入图片描述

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>

2.在application.yml配置redis

  • 使用jedis连接池
spring:
  redis:
    #单个redis配置host和prot
    #host: 192.168.74.83
    #port: 6379
    #password:
    timeout: 6000ms
    jedis:
      pool:
        max-active: 8
        max-wait: -1ms
        max-idle: 8
        min-idle: 0
    #sentinel哨兵配置
    sentinel:
      master: mymaster
      nodes: 127.0.0.1:26379,127.0.0.1:26380,127.0.0.1:26381
  • 使用lettuce连接池
        <!-- lettuce pool 缓存连接池 -->
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-pool2</artifactId>
            <version>2.5.0</version>
        </dependency>
spring:
  redis:
    timeout: 6000ms
    lettuce:
      pool:
        max-active: 8
        max-wait: -1ms
        max-idle: 8
        min-idle: 0
    #sentinel哨兵配置
    sentinel:
      master: mymaster
      nodes: 127.0.0.1:26379,127.0.0.1:26380,127.0.0.1:26381

3.新建一个RedisClient封装redisTemplate

/**
 * redis 工具类类
 * @author zhangxinlin
 *
 */
public class RedisClient {

	private StringRedisTemplate redisTemplate;
	
	public void setRedisTemplate(StringRedisTemplate redisTemplate) {
		this.redisTemplate = redisTemplate;
	}

	public RedisTemplate<String, String> getRedisTemplate() {
		return redisTemplate;
	}

	public void set(String key, String val) throws Exception {
		 ValueOperations<String, String> ops = redisTemplate.opsForValue();
		 ops.set(key, val);
	}

	public Boolean set(String key, String val, long expireSecond) throws Exception {
		 ValueOperations<String, String> ops = redisTemplate.opsForValue();
		 ops.set(key, val);
		 return redisTemplate.expire(key, expireSecond, TimeUnit.SECONDS);
	}
	
	public String get(String key) throws Exception {
		 ValueOperations<String, String> ops = redisTemplate.opsForValue();
		 return ops.get(key);
	}
	
	public Boolean exists(String key) throws Exception {
		return redisTemplate.hasKey(key);
	}
}

在spring中配置RedisClient,使用redisTemplate

/**
 * redis配置
 * @author zhangxinlin
 * @date
 *
 */
@Configuration
public class RedisAutoConfig {
	/**
	 * 初始化Redis
	 * @return
	 */
	@Bean
	public RedisClient redisCache(StringRedisTemplate redisTemplate) {
		RedisClient redisCache = new RedisClient();
		redisCache.setRedisTemplate(redisTemplate);
		return redisCache;
	}
}

4.新建测试类测试

@RunWith(SpringRunner.class)
@SpringBootTest
public class RedisClientTest {
    @Autowired
    RedisClient redisClient;

    @Test
    public void testRedis() {
        try{
            redisClient.set("testKey","redis测试值");
            String testValue = redisClient.get("testKey");
            System.out.println("从redis获取的key值:"+testValue);
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}

运行结果 代码中执行redis读写正常,springboot配置sentinel哨兵模式就成功了
在这里插入图片描述

注:

  1. springboot2之前redis的连接池为jedis,2.0以后redis的连接池改为了lettuce,lettuce能够支持redis4,需要java8及以上。lettuce是基于netty实现的与redis进行同步和异步的通信
  2. Jedis:Redis的Java实现客户端,比较全面的提供了Redis的操作特性。在多个线程间共享一个 Jedis 实例时是线程不安全的,需要使用连接池
  3. Lettuce:高级Redis客户端,用于线程安全同步,异步和响应使用,支持集群,主要在一些分布式缓存框架上使用比较多

项目源码
https://gitee.com/zhangxinlin/spring-datasource-demo

  • 7
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
在Spring Boot配置Redis哨兵模式需要进行以下步骤: 1. 首先,在Redis服务器上创建一个主从复制的配置文件redis.windows.conf,并修改配置如下: ``` port 6379 bind 127.0.0.1 dir "C:\\redis-master" replicaof no one ``` 2. 拷贝redis.windows.conf文件,将其复制并重命名为slave1.conf和slave2.conf。然后,分别修改这两个配置文件的端口号和目录路径,例如: ``` slave1.conf: port 6380 dir "C:\\redis-slave1" slave2.conf: port 6381 dir "C:\\redis-slave2" ``` 3. 接下来,创建哨兵配置文件sentinel1.conf、sentinel2.conf和sentinel3.conf,配置如下: ``` sentinel1.conf: bind 0.0.0.0 port 26379 dir "C:\\redis-sentinel1" sentinel monitor mymaster 127.0.0.1 6379 2 sentinel2.conf: bind 0.0.0.0 port 36379 dir "C:\\redis-sentinel2" sentinel monitor mymaster 127.0.0.1 6379 2 sentinel3.conf: bind 0.0.0.0 port 46379 dir "C:\\redis-sentinel3" sentinel monitor mymaster 127.0.0.1 6379 2 ``` 4. 在Spring Boot的配置文件配置Redis哨兵模式。例如,application.properties文件配置如下: ``` spring.redis.sentinel.master=mymaster spring.redis.sentinel.nodes=127.0.0.1:26379,127.0.0.1:36379,127.0.0.1:46379 ``` 通过以上步骤,你就成功地在Spring Boot配置Redis哨兵模式。配置文件的端口号和目录路径需要根据实际情况进行修改。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [SpringBoot Redis 哨兵配置(一主两从三哨兵配置)](https://blog.csdn.net/qq_34125349/article/details/89175908)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值