超完整!Springboot整合redis集群(Sentine),spring boot自动配置集成redis集群(Sentine)

3 篇文章 0 订阅
3 篇文章 0 订阅

1、添加maven依赖

注意maven版本依赖,版本要相互匹配,如不知道如何查看相对应的版本时,可进入博主主页查看博主上一篇博文。

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.0.5.RELEASE</version>
    <relativePath /> <!-- lookup parent from repository -->
</parent>

<dependencies>
 <!-- 这里还需要加入spring boot基本依赖如spring-boot-starter-web,就不一一阐述了 -->
	<dependency>
	    <groupId>org.springframework.boot</groupId>
	    <artifactId>spring-boot-starter-data-redis</artifactId>
	</dependency>
	<dependency>
	    <groupId>org.apache.commons</groupId>
	    <artifactId>commons-pool2</artifactId>
	    <version>2.5.0</version>
	</dependency>
</dependencies>

2、在appliction.properties添加配置

# 服务器
spring.redis.sentinel.nodes=ip1:port1,ip2:port2,ip3:port3
spring.redis.sentinel.master=mymaster
spring.redis.password=password
# 连接池最大连接数(使用负值表示没有限制)
spring.redis.jedis.pool.max-active=200
spring.redis.lettuce.pool.max-wait=1500
# Redis数据库索引(默认为0)
spring.redis.database=0
# 连接池最大阻塞等待时间(使用负值表示没有限制)
spring.redis.jedis.pool.max-wait=1500
# 连接池中的最大空闲连接
spring.redis.jedis.pool.max-idle=100
# 连接池中的最小空闲连接
spring.redis.jedis.pool.min-idle=100000
# 连接超时时间(毫秒)
spring.redis.timeout=60000

3、测试效果

spring boot 有自动配置,非常棒的功能,什么事情都不需要你做,只需要配置基本参数即可,非常简单!!!

package test;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest
public class DemoApplicationTests {

    @Autowired
    private SerialNoServiceImpl service;

    @Test
    public void test() {
       // set key
       redisTemplate.opsForValue().set("keyname", "value", "100000", TimeUnit.MILLISECONDS);
       // get key
       System.out.println("redis,key:" + redisTemplate.opsForValue().get("keyname"));
    }
}

成功输出!可以看出spring boot集成redis简直太简单了。关于传统spring项目集成redis集群,可以进博主主页查看博主上一篇博文,超完整!传统spring项目集成redis集群(Sentine),springMVC整合redis集群(Sentine),含全部配置.

如有问题,可留言,加博主qq:244156219,微信:zuo995518,可以拉你进技术交流群,大家一起进步。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值