redis在项目中的基本配置文件

启动redis服务之后如何在spring项目中使用redis ;只需要配置好基本的连接属性,设置连接相关的信息就可以了;

properties:

redis:
#Redis数据库索引(默认为0)
host: 127.0.0.1
port: 6379
password: 123
#连接超时时间(毫秒)
timeout: 5000
# 自定义redis默认过期时间(单位:时)
expire-time: 1
jedis:
pool:
# 连接池最大连接数(使用负值表示没有限制)
max-active: -1
# 连接池最大阻塞等待时间(使用负值表示没有限制)
max-wait: -1
#连接池中的最大空闲连接
max-idle=10000
#连接池中的最小空闲连接
min-idle=0

配置好连接就可以在项目中使用了;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
哨兵模式是Redis高可用解决方案的一部分,它通过一组哨兵服务器监控主服务器,并在主节点故障时自动切换到从节点成为新的主节点。在Spring Boot应用集成Redis哨兵,可以提供可靠的数据持久化和故障恢复。 以下是配置Spring Boot应用使用Redis哨兵的基本步骤: 1. 添加依赖:首先,在项目的pom.xml或build.gradle文件添加Spring Data Redis和 lettuce-cluster(Redis客户端支持哨兵)的依赖: Maven: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> <dependency> <groupId>io.lettuce</groupId> <artifactId>lettuce-core</artifactId> </dependency> ``` Gradle: ```groovy implementation 'org.springframework.boot:spring-boot-starter-data-redis' implementation 'io.lettuce:lettuce-core' ``` 2. 配置RedisSentinelConfiguration:创建一个@Configuration类并注入`@EnableCaching`和`@EnableRedisHttpSession`,然后覆盖`redisTemplate` bean以指向哨兵集群: ```java import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Bean; import org.springframework.data.redis.connection.RedisConnectionFactory; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.sentinel.RedisSentinelConfiguration; import org.springframework.data.redis.sentinel.SentinelConnectionProvider; @Configuration @EnableCaching @EnableRedisHttpSession public class RedisConfig { @Autowired private SentinelConnectionProvider connectionProvider; @Bean(name = "sentinelRedisConnectionFactory") public RedisConnectionFactory sentinelConnectionFactory() { RedisSentinelConfiguration config = new RedisSentinelConfiguration() .master("yourMasterName") // 根据实际情况替换为主机名 .sentinels(connectionProvider.getSentinels()); // 获取所有哨兵实例 return new LettuceConnectionFactory(config); } @Bean(name = "redisTemplate") public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) { RedisTemplate<String, Object> template = new RedisTemplate<>(); template.setConnectionFactory(factory); return template; } } ``` 记得将`yourMasterName`替换为你实际的主节点名称。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Ariliya

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值