springboot配置redis

在springboot中,默認繼承好了一套無缺的redis包,能夠直接使用,可是若是使用中出了錯不容易找到錯誤的緣由,所以這裏使用本身配置的redis;java

須要使用的三個主要jar包:redis

<dependency>
            <groupId>redis.clients</groupId>
            <artifactId>jedis</artifactId>
            <version>2.9.0</version>
        </dependency>
<dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-redis</artifactId>
        </dependency>
<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>
View Code

使用spring-boot-configuration-processor包主要是用來配置加載文件spring

package com.zs.springboot.config.redis;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

/**
 * @Company
 * @Author Zs
 * 將這個類做爲spring的一個組件,添加@ConfigurationProperties(prefix = "spring.redis")註解,就會默認從application.properties
 * 文件中加載前綴爲spring.redis的配置信息,配置文件中的配置字段與該類中的屬性一致,經過setter方法來設值
 * @Date Create in 2019/8/30
 **/
@Component
@ConfigurationProperties(prefix = "spring.redis")
public class RedisProperties {

    private String ip;
    private Integer[] ports;
    private Integer maxActive;
    private Integer maxWait;
    private Integer expire;

    public String getIp() {
        return ip;
    }

    public void setIp(String ip) {
        this.ip = ip;
    }

    public Integer[] getPorts() {
        return ports;
    }

    public void setPorts(Integer[] ports) {
        this.ports = ports;
    }

    public Integer getMaxActive() {
        return maxActive;
    }

    public void setMaxActive(Integer maxActive) {
        this.maxActive = maxActive;
    }

    public Integer getMaxWait() {
        return maxWait;
    }

    public void setMaxWait(Integer maxWait) {
        this.maxWait = maxWait;
    }

    public Integer getExpire() {
        return expire;
    }

    public void setExpire(Integer expire) {
        this.expire = expire;
    }
}

在application中配置redis:json

 

 而後配置redis的配置類,使用jdisCluster來操做redis:springboot

package com.zs.springboot.config.redis;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.JedisCluster;

import java.util.HashSet;
import java.util.Set;

/**
 * @Company
 * @Author Zs
 * @Date Create in 2019/8/30
 **/
@Configuration
public class RedisConfiguration {
    private RedisProperties redisProperties;

    public RedisConfiguration(RedisProperties redisProperties) {
        this.redisProperties = redisProperties;
    }

    @Bean
    public JedisCluster jedisCluster() {
        Integer[] ports = redisProperties.getPorts();
        String host = redisProperties.getIp();
        Set<HostAndPort> hostAndPortSet = new HashSet<>();
        for (Integer port : ports) {
            hostAndPortSet.add(new HostAndPort(host, port));
        }
        return new JedisCluster(hostAndPortSet, redisProperties.getMaxActive(), redisProperties.getMaxWait());
    }
}

編輯redis的增刪該方法:app

/**
 * @Company
 * @Author Zs
 * @Date Create in 2019/8/28
 **/
@Service
public class RedisService {

    @Autowired
    private JedisCluster jedisCluster;
    private static final String SET_SUCCESS = "OK";

    /**
     * 添加string數據,成功返回code:200,失敗code:404
     * @param key
     * @param value
     * @return
     */
    public Map<String, Object> set(String key, Object value) {
        Map<String, Object> map = new HashMap<>();
        String result = jedisCluster.set(key, JsonUtil.toJsonString(value));
        if (SET_SUCCESS.equals(result)) {
            map.put(Status.SUCCESS.getCodeName(), Status.SUCCESS.getCode());
        } else {
            map.put(Status.FILED.getCodeName(), Status.FILED.getCode());
        }
        return map;
    }

    /**
     * 從redis根據key獲取string數據
     * @param key
     * @return
     */
    public String get(String key) {
        String jsonString = jedisCluster.get(key);
        if (jsonString==null || jsonString.equals("")) {
            return null;
        }
        return jsonString;
    }

    /**
     * 刪除string數據
     * @param key
     * @return
     */
    public Map<String, Object> del(String key) {
        Map<String, Object> map = new HashMap<>();
        Long del = jedisCluster.del(key);
        if (del>0) {
            map.put("code", 200);
        } else {
            map.put("code", 404);
        }
        return map;
    }

    /**
     * 設置失效時間
     * @param key
     * @param seconds
     * @return
     */
    public Long expire(String key,Integer seconds) {
        return jedisCluster.expire(key, seconds);
    }

}

注意不能在service層中注入service,若是須要能夠在controller層將redisService作爲參數傳遞進去,若是在service層中注入其餘的service對象,可能形成事務的串聯,讀到髒數據。ide

該方法須要使用到jsonUtil類,將數據轉爲json字符串存儲spring-boot

相關文章
相關標籤/搜索