当前位置:   article > 正文

SpringBoot整合Redis【一】Jedis配置_springboot jedis连接池配置

springboot jedis连接池配置

说明

Jedis有着丰富的操作Redis数据库的指令,下面来看看SpringBoot整合Jedis配置

配置

yml

redis:
  # redis种第几个库
  DB: 0
  # ip
  host: ip
  # 端口
  port: 6379
  # 密码
  password: password
  # 连接超时时间
  timeout: 3000
  # 最大连接数
  max-active: 100
  # 最多维持空闲连接
  max-idle: 10
  # 最少维持空闲连接
  min-idle: 10
  # 连接池出借连接的最长期限
  max-wait: 20000

server:
  port: 80

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23

pom

<!--jedis-->
<dependency>
    <groupId>redis.clients</groupId>
    <artifactId>jedis</artifactId>
</dependency>
<dependency>
   <groupId>org.apache.commons</groupId>
    <artifactId>commons-collections4</artifactId>
    <version>4.2</version>
</dependency>
<dependency>
    <groupId>cn.hutool</groupId>
    <artifactId>hutool-core</artifactId>
    <version>5.3.10</version>
</dependency>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

JedisPool工厂类

这里我们可以整合Nacos配置Yml文件,具体可参考:

SpringBoot整合Nacos

import org.junit.platform.commons.util.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

/**
 * 创建一个生成JedisPool的工厂
 */
@Configuration
public class JedisPoolFactory {

    @Value("${redis.DB:0}")
    private Integer dbNum;

    @Value("${redis.host:localhost}")
    private String host;

    @Value("${redis.port:6379}")
    private Integer port;

    @Value("${redis.password}")
    private String password;

    @Value("${redis.timeout:3000}")
    private Integer timeout;

    @Value("${redis.max-active:20}")
    private Integer maxActive;

    @Value("${redis.max-idle:8}")
    private Integer maxIdle;

    @Value("${redis.min-idle:0}")
    private Integer minIdle;

    @Value("${redis.max-wait:10000}")
    private Long maxWaitMillis;


    @Bean
    public JedisPool generateJedisPoolFactory() {
        if (StringUtils.isBlank(password)){
            password = null;
        }
        JedisPoolConfig poolConfig = new JedisPoolConfig();
        poolConfig.setMaxTotal(maxActive);
        poolConfig.setMaxIdle(maxIdle);
        poolConfig.setMinIdle(minIdle);
        poolConfig.setMaxWaitMillis(maxWaitMillis);
        JedisPool jedisPool = new JedisPool(poolConfig, host, port, timeout, password, dbNum);
        return jedisPool;
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Cpp五条/article/detail/159966
推荐阅读
相关标签
  

闽ICP备14008679号