天天看點

springboot + redis(單機版)

本次和大家分享的是在springboot內建使用redis,這裡使用的是redis的jedis用戶端,如下添加依賴

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

然後需要redis的相關配置(這裡我的redis密碼是空)

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

這是redis的一般配置,具體調優可以設定這些參數,下面在JedisConfig類中讀取這些設定

@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;           

有上面的配置,就需要有代碼裡面設定下,這裡建立一個傳回JedisPoolConfig的方法

/**
     * jedis配置
     *
     * @return
     */
    public JedisPoolConfig getJedisPoolConfig() {
        JedisPoolConfig config = new JedisPoolConfig();
        config.setMaxIdle(maxIdle); #最大空閑數
        config.setMaxWaitMillis(maxAWait); #最大等待時間
        config.setMaxTotal(maxActive); #最大連接配接數
        return config;
    }           

有了配置,接下來就建立JedisPool,這裡把JedisPool托管到spring中

/**
     * 擷取jedispool
     *
     * @return
     */
    @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;
    }           

上面簡單區分了無密碼的情況,到此jedis的配置和連接配接池就基本搭建完了,下面就是封裝使用的方法,這裡以set和get為例;首先建立個JedisComponent元件,代碼如下

/**
 * 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);
    }
}           

有了對jedis的調用封裝,我們在Controller層的測試用例如下

@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");
    }           

運作set和get的接口效果如

springboot + redis(單機版)
springboot + redis(單機版)