Springboot + Redis (Standalone)

God cattle 003

  • The cloud community
  • A programming language
  • redis
  • java
  • spring
  • configuration
  • string
  • jedis

Abstract: This time, I will share with you the integration of Redis in SpringBoot, where the use of Redis jedis client, Clients

jedis

This time, we will share with you the integration of Redis in SpringBoot, where the use of Redis jedis client, adding dependencies as follows

<dependency>
  <groupId>redis.clients</groupId>
  <artifactId>jedis</artifactId>
</dependency>Copy the code

Then I need the redis configuration (here my redis password is empty)

Spring: redis: single: 192.168.146.28:6378 jedis: pool: max-idle: 8 max-active: 8 max-wait: 3000 timeout: 3000 password:Copy the code

This is the general configuration of Redis, and tuning can set these parameters, which are read in the JedisConfig class below

    @Value("${spring.redis.single}")
    private String strSingleNode;

    @Value("${spring.redis.jedis.pool.max-idle}")
    private Integer maxIdle;

    @Value("${spring.redis.jedis.pool.max-active}")
    private Integer maxActive;

    @Value("${spring.redis.jedis.pool.max-wait}")
    private Integer maxAWait;

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

    @Value("${spring.redis.password}")
    private String password;Copy the code

With the above configuration, you need to create a method that returns JedisPoolConfig in the code

Public JedisPoolConfig getJedisPoolConfig() {JedisPoolConfig config = new JedisPoolConfig(); config.setMaxIdle(maxIdle); # config.setMaxWaitmillis (maxAWait); Config.setmaxtotal (maxActive); Return config; }Copy the code

Now that you have the configuration, create JedisPool, which is hosted in Spring

@bean public jedispool getJedisPool() {JedisPoolConfig config = getJedisPoolConfig(); System.out.println("strSingleNode:" + this.strSingleNode); String[] nodeArr = this.strSingleNode.split(":"); JedisPool jedisPool = null; if (this.password.isEmpty()) { jedisPool = new JedisPool( config, nodeArr[0], Integer.valueOf(nodeArr[1]), this.timeout); } else { jedisPool = new JedisPool( config, nodeArr[0], Integer.valueOf(nodeArr[1]), this.timeout, this.password); } return jedisPool; }Copy the code

In this case, jedis configuration and connection pool are basically set up. The following is the method used for encapsulation. Here, set and GET are used as examples. First create a JedisComponent 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); }}Copy the code

With the invocation of Jedis wrapped, we test at the Controller layer for example

    @Autowired
    JedisComponent jedis;

    @GetMapping("/setJedis/{val}")
    public boolean setJedis(@PathVariable String val) {
        return jedis.set("token", val);
    }

    @GetMapping("/getJedis")
    public String getJedis() {
        return jedis.get("token");
    }Copy the code








Use the cloud habitat community APP, comfortable ~

For details, please click

To:

Related articles

The net friend comment on