当前位置:   article > 正文

SpringBoot 集成Jedis操作Redis缓存_spring.redis.pool testonborrow

spring.redis.pool testonborrow

  在使用SpringBoot构建SpringCloud微服务时,需要用到Redis做数据缓存,提高业务逻辑的处理。所以就不得不让SpringBoot集成Redis,但如果使用官方的Redis去操作的话,你叫麻烦,所以就使用Jedis去操作Reids,这样操作简便,编码效率打打提高。这篇就介绍SpringBoot如何集成Jedis去操作Redis。

首先在application.properties文件中加入redis的基本配置:

 

#多redis连接配置
spring.redis.shard.1.host = 127.0.0.1
spring.redis.shard.1.password = 
spring.redis.shard.1.port = 6379

#spring.redis.shard.2.host = 127.0.0.1
#spring.redis.shard.2.password = 
#spring.redis.shard.2.port = 6379

spring.redis.pool.maxIdle = 20
spring.redis.pool.maxTotal = 500
spring.redis.pool.numTestsPerEvictionRun = 3
spring.redis.pool.testOnBorrow = true
spring.redis.pool.blockWhenExhausted = false
spring.redis.pool.testOnReturn = false
 
然后配置JedisConfig配置类,注入配置文件中的值:
  1. import java.io.Serializable;
  2. import org.springframework.boot.context.properties.ConfigurationProperties;
  3. import org.springframework.stereotype.Component;
  4. @Component
  5. @ConfigurationProperties(prefix = "spring.redis.pool")
  6. public class JedisConfig implements Serializable{
  7. /**
  8. *
  9. */
  10. private static final long serialVersionUID = 1L;
  11. private String host;
  12. private int port;
  13. private String password;
  14. private Integer maxTotal;
  15. private Integer maxIdle;
  16. private Integer minIdle;
  17. private Long maxWaitMillis;
  18. private boolean testOnBorrow;
  19. private boolean testOnReturn;
  20. private boolean testWhileIdle;
  21. public String getHost() {
  22. return host;
  23. }
  24. public void setHost(String host) {
  25. this.host = host;
  26. }
  27. public int getPort() {
  28. return port;
  29. }
  30. public void setPort(int port) {
  31. this.port = port;
  32. }
  33. public String getPassword() {
  34. return password;
  35. }
  36. public void setPassword(String password) {
  37. this.password = password;
  38. }
  39. public Integer getMaxTotal() {
  40. return maxTotal;
  41. }
  42. public void setMaxTotal(Integer maxTotal) {
  43. this.maxTotal = maxTotal;
  44. }
  45. public Integer getMaxIdle() {
  46. return maxIdle;
  47. }
  48. public void setMaxIdle(Integer maxIdle) {
  49. this.maxIdle = maxIdle;
  50. }
  51. public Integer getMinIdle() {
  52. return minIdle;
  53. }
  54. public void setMinIdle(Integer minIdle) {
  55. this.minIdle = minIdle;
  56. }
  57. public Long getMaxWaitMillis() {
  58. return maxWaitMillis;
  59. }
  60. public void setMaxWaitMillis(Long maxWaitMillis) {
  61. this.maxWaitMillis = maxWaitMillis;
  62. }
  63. public boolean isTestOnBorrow() {
  64. return testOnBorrow;
  65. }
  66. public void setTestOnBorrow(boolean testOnBorrow) {
  67. this.testOnBorrow = testOnBorrow;
  68. }
  69. public boolean isTestOnReturn() {
  70. return testOnReturn;
  71. }
  72. public void setTestOnReturn(boolean testOnReturn) {
  73. this.testOnReturn = testOnReturn;
  74. }
  75. public boolean isTestWhileIdle() {
  76. return testWhileIdle;
  77. }
  78. public void setTestWhileIdle(boolean testWhileIdle) {
  79. this.testWhileIdle = testWhileIdle;
  80. }
  81. }
最后配置JedisConfiguration类,生成2个工具Bean,redisTemplate和forecastRedisTemplate,在需要使用jedis的地方注入bean,可以对redis进行操作。
  1. import org.springframework.beans.factory.annotation.Autowired;
  2. import org.springframework.context.annotation.Bean;
  3. import org.springframework.context.annotation.Configuration;
  4. import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
  5. import org.springframework.data.redis.core.StringRedisTemplate;
  6. import redis.clients.jedis.JedisPoolConfig;
  7. @Configuration
  8. public class JedisConfiguration {
  9. @Autowired
  10. JedisConfig redisConfig;
  11. public JedisConnectionFactory convertJedisConnectionFactory() {
  12. JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory();
  13. jedisConnectionFactory.setHostName(redisConfig.getHost());
  14. jedisConnectionFactory.setPort(redisConfig.getPort());
  15. jedisConnectionFactory.setPassword(redisConfig.getPassword());
  16. JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
  17. jedisPoolConfig.setMaxTotal(redisConfig.getMaxTotal());
  18. jedisPoolConfig.setMaxIdle(redisConfig.getMaxIdle());
  19. jedisPoolConfig.setMinIdle(redisConfig.getMinIdle());
  20. jedisPoolConfig.setMaxWaitMillis(redisConfig.getMaxWaitMillis());
  21. jedisPoolConfig.setTestOnBorrow(redisConfig.isTestOnBorrow());
  22. jedisPoolConfig.setTestOnReturn(redisConfig.isTestOnReturn());
  23. jedisPoolConfig.setTestWhileIdle(redisConfig.isTestWhileIdle());
  24. jedisConnectionFactory.setPoolConfig(jedisPoolConfig);
  25. return jedisConnectionFactory;
  26. }
  27. @Bean(name = "redisTemplate")
  28. public StringRedisTemplate convertStringRedisTemplate() {
  29. StringRedisTemplate stringRedisTemplate = new StringRedisTemplate(convertJedisConnectionFactory());
  30. return stringRedisTemplate;
  31. }
  32. @Bean(name = "forecastRedisTemplate")
  33. public StringRedisTemplate convertStringRedisTemplate1() {
  34. StringRedisTemplate stringRedisTemplate = new StringRedisTemplate(convertJedisConnectionFactory());
  35. return stringRedisTemplate;
  36. }
  37. }
 

使用的地方可以注入bean(redisTemplate,forecastRedisTemplate),名字在JedisConfiguration类的bean(“name”)中自行更改。如图所示:

 

 

 

本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号