Springboot + redis (stand-alone)

This time, I want to share with you that redis is used in spring boot integration. Here, the jedis client of redis is used (here, redis running by docker, please refer to docker quickly builds several common third-party services ), add dependencies as follows:

<dependency>
  <groupId>redis.clients</groupId>
  <artifactId>jedis</artifactId>
</dependency>

Then you need to configure redis (my redis password is empty here). In application.yml, set it as follows:

spring:
  redis:
    single: 192.168.146.28:6378
    jedis:
      pool:
        max-idle: 8
        max-active: 8
        max-wait: 3000
    timeout: 3000
    password:

This is the general configuration of redis. You can set these parameters for specific tuning. Read these settings in the JedisConfig class as follows:

 1  @Value("${spring.redis.single}")
 2     private String strSingleNode;
 3 
 4     @Value("${spring.redis.jedis.pool.max-idle}")
 5     private Integer maxIdle;
 6 
 7     @Value("${spring.redis.jedis.pool.max-active}")
 8     private Integer maxActive;
 9 
10     @Value("${spring.redis.jedis.pool.max-wait}")
11     private Integer maxAWait;
12 
13     @Value("${spring.redis.timeout}")
14     private Integer timeout;
15 
16     @Value("${spring.redis.password}")
17     private String password;

With the above configuration, you need to set it in the code. Here, create a method to return to JedisPoolConfig

 1     /**
 2      * jedis To configure
 3      *
 4      * @return
 5      */
 6     public JedisPoolConfig getJedisPoolConfig() {
 7         JedisPoolConfig config = new JedisPoolConfig();
 8         config.setMaxIdle(maxIdle); #Maximum number of idle
 9         config.setMaxWaitMillis(maxAWait); #Maximum waiting time
10         config.setMaxTotal(maxActive); #maximum connection
11         return config;
12     }

With the configuration, the next step is to create a JedisPool, which is hosted in spring

 1   /**
 2      * Get jedispool
 3      *
 4      * @return
 5      */
 6     @Bean
 7     public JedisPool getJedisPool() {
 8         JedisPoolConfig config = getJedisPoolConfig();
 9         System.out.println("strSingleNode:" + this.strSingleNode);
10         String[] nodeArr = this.strSingleNode.split(":");
11 
12         JedisPool jedisPool = null;
13         if (this.password.isEmpty()) {
14             jedisPool = new JedisPool(
15                     config,
16                     nodeArr[0],
17                     Integer.valueOf(nodeArr[1]),
18                     this.timeout);
19         } else {
20             jedisPool = new JedisPool(
21                     config,
22                     nodeArr[0],
23                     Integer.valueOf(nodeArr[1]),
24                     this.timeout,
25                     this.password);
26         }
27         return jedisPool;
28     }

The above simply distinguishes the case of no password. The configuration and connection pool of jedis are basically set up. The following is the encapsulation method. Here, take set and get as examples. First, create a JedisComponent component, and the code is as follows:

/**
 * Created by Administrator on 2018/8/18.
 */
@Component
public class JedisComponent {

    @Autowired
    JedisPool jedisPool;

    public boolean set(String key, String val) {
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            return jedis.set(key, val).equalsIgnoreCase("OK");
        } finally {
            if (jedis != null) {
                jedis.close();
            }
        }
    }

    public <T> boolean set(String key, T t) {
        String strJson = JacksonConvert.serilize(t);
        if (strJson.isEmpty()) {
            return false;
        }
        return this.set(key, strJson);
    }

    public String get(String key) {
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            return jedis.get(key);
        } finally {
            if (jedis != null) {
                jedis.close();
            }
        }
    }

    public <T> T get(String key, Class<T> tClass) {
        String strJson = this.get(key);
        return JacksonConvert.deserilize(strJson, tClass);
    }
}

With the call encapsulation of jedis, our test cases at the Controller layer are as follows:

 1   @Autowired
 2     JedisComponent jedis;
 3 
 4     @GetMapping("/setJedis/{val}")
 5     public boolean setJedis(@PathVariable String val) {
 6         return jedis.set("token", val);
 7     }
 8 
 9     @GetMapping("/getJedis")
10     public String getJedis() {
11         return jedis.get("token");
12     }

The interface effect of running set and get is as follows:

Keywords: Java Jedis Redis Spring Docker

Added by kishanprasad on Thu, 02 Jan 2020 15:07:01 +0200