Spring boot长时间不操作链接超时问题

1.现象描述:在生产环境時调用接口会偶尔出现连接超时现象,经过测试发现,连接超时现象统一出现在对系统长时间没有操作后第一次操作开始15-20分钟左右,在之后会表现为系统稳定不会出现链接超时现象,再次一到两个小时无操作,问题重复出现.
2.问题原因:经过问题定位确定为数据库链接导致的连接超时现象,我们用的是druid
链接池的配置为自己定义的如图:
在这里插入图片描述
红框已经注释掉的为出问题是的链接池配置.
3解决:如上图链接池配置使用Druid自带的配置即可.使用改配置時要导入Druid包
-
com.alibaba
druid
1.0.19

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Boot 中,我们可以通过添加 `spring-boot-starter-data-redis` 依赖来使用 Redis。然后,我们可以通过在 `application.properties` 或 `application.yml` 文件中添加以下属性来配置 Redis RDB 持久化: **application.properties:** ``` # Redis RDB 持久化 spring.redis.database=0 spring.redis.host=localhost spring.redis.port=6379 spring.redis.password= spring.redis.timeout=3000 spring.redis.jedis.pool.max-active=8 spring.redis.jedis.pool.max-idle=8 spring.redis.jedis.pool.min-idle=0 spring.redis.jedis.pool.max-wait=-1 spring.redis.redis-cluster=false spring.redis.sentinel.master= spring.redis.sentinel.nodes= spring.redis.sentinel.password= spring.redis.sentinel.pool.max-active=8 spring.redis.sentinel.pool.max-idle=8 spring.redis.sentinel.pool.min-idle=0 spring.redis.sentinel.pool.max-wait=-1 spring.redis.sentinel.pool.master-name= spring.redis.sentinel.pool.nodes= spring.redis.sentinel.pool.password= spring.redis.cluster.max-redirects=3 spring.redis.cluster.nodes=redis://localhost:6379 spring.redis.cluster.password= spring.redis.cluster.timeout=3000 spring.redis.cluster.max-redirects=3 spring.redis.lettuce.pool.max-active=8 spring.redis.lettuce.pool.max-idle=8 spring.redis.lettuce.pool.min-idle=0 spring.redis.lettuce.pool.max-wait=-1 spring.redis.lettuce.shutdown-timeout=100 spring.redis.lettuce.shutdown-timeout=100 ``` **application.yml:** ``` # Redis RDB 持久化 spring: redis: database: 0 host: localhost port: 6379 password: timeout: 3000 jedis: pool: max-active: 8 max-idle: 8 min-idle: 0 max-wait: -1 redis-cluster: false sentinel: master: nodes: password: pool: max-active: 8 max-idle: 8 min-idle: 0 max-wait: -1 pool: master-name: nodes: password: cluster: max-redirects: 3 nodes: redis://localhost:6379 password: timeout: 3000 max-redirects: 3 lettuce: pool: max-active: 8 max-idle: 8 min-idle: 0 max-wait: -1 shutdown-timeout: 100 shutdown-timeout: 100 ``` 其中,`spring.redis.database` 表示要使用的 Redis 数据库编号,默认为 0;`spring.redis.host` 和 `spring.redis.port` 表示 Redis 服务器的地址和端口号,默认为 `localhost` 和 `6379`;`spring.redis.password` 表示 Redis 认证密码,默认为空;`spring.redis.timeout` 表示连接 Redis 服务器的超时时间,默认为 3000 毫秒;`spring.redis.jedis.pool.max-active`、`spring.redis.jedis.pool.max-idle`、`spring.redis.jedis.pool.min-idle` 和 `spring.redis.jedis.pool.max-wait` 表示 Jedis 连接池的配置参数,可以根据实际情况进行调整。其他参数的含义可以参考官方文档。 配置完成后,我们可以通过在代码中注入 `RedisTemplate` 或 `StringRedisTemplate` 来进行 Redis 操作。例如: ``` @Autowired private RedisTemplate<String, Object> redisTemplate; public void set(String key, Object value) { redisTemplate.opsForValue().set(key, value); } public Object get(String key) { return redisTemplate.opsForValue().get(key); } ``` 注意,在使用 Redis RDB 持久化时,我们需要在 Redis 服务器的配置文件中开启 RDB 持久化功能。可以通过在配置文件中添加以下内容来开启 RDB 持久化: ``` save 900 1 save 300 10 save 60 10000 ``` 其中,`save` 表示 RDB 持久化的配置项,后面的三个数字分别表示多长时间内有多少次修改操作就会触发一次 RDB 持久化。例如,上面的配置表示如果在 900 秒内有至少一次修改操作、或者在 300 秒内有至少 10 次修改操作、或者在 60 秒内有至少 10000 次修改操作,就会触发一次 RDB 持久化。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值