SpringBoot2.0使用JedisCluster管理 配置redis集群
1.pom文件
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> </dependency>
2.配置文件redis.properties
#客戶端超時時間單位是毫秒 默認是2000 redis.timeout=10000 #最大空閑數 redis.maxIdle=300 #連接池的最大數據庫連接數。設為0表示無限制,如果是jedis 2.4以后用redis.maxTotal #redis.maxActive=600 #控制一個pool可分配多少個jedis實例,用來替換上面的redis.maxActive,如果是jedis 2.4以后用該屬性 redis.maxTotal=2000 #最大建立連接等待時間。如果超過此時間將接到異常。設為-1表示無限制。 redis.maxWaitMillis=1000 redis.nodes=127.0.0.1:6479,127.0.0.1:6480,127.0.0.1:6481,127.0.0.1:6482,127.0.0.1:6483,127.0.0.1:6484 redis.expireSeconds=120 redis.commandTimeout=10000 redis.password=redis
3.通過配置文件注入JedisCluster
package top.xzhand.config; import org.apache.commons.pool2.impl.GenericObjectPoolConfig; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.PropertySource; import redis.clients.jedis.HostAndPort; import redis.clients.jedis.JedisCluster; import java.util.HashSet; import java.util.Set; @Configuration @PropertySource("classpath:redis.properties") //指定redis配置文件路徑 public class JedisClusterConfig { @Value("${redis.maxIdle}") private Integer maxIdle; @Value("${redis.timeout}") private Integer timeout; @Value("${redis.maxTotal}") private Integer maxTotal; @Value("${redis.maxWaitMillis}") private Integer maxWaitMillis; @Value("${redis.nodes}") private String clusterNodes; @Value("${redis.password}") private String password; @Value("${redis.expireSeconds}") private int expireSeconds; @Value("${redis.commandTimeout}") private int commandTimeout; @Bean public JedisCluster getJedisCluster() { String[] serverArray = clusterNodes.split(",");//獲取服務器數組 Set<HostAndPort> nodes = new HashSet<>(); for (String ipPort : serverArray) { String[] ipPortPair = ipPort.split(":"); nodes.add(new HostAndPort(ipPortPair[0].trim(), Integer.valueOf(ipPortPair[1].trim()))); } return new JedisCluster(nodes,commandTimeout,1000,1,password ,new GenericObjectPoolConfig());//需要密碼連接的創建對象方式 } }
4.redis工具類
package top.xzhand.util; import com.alibaba.fastjson.JSON; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import org.springframework.stereotype.Service; import redis.clients.jedis.JedisCluster; import java.util.concurrent.TimeUnit; @Component public class RedisClientTemplate { @Autowired private JedisCluster jedisCluster; /** * 設置緩存 * @param key 緩存key * @param value 緩存value */ public void set(String key, String value) { jedisCluster.set(key, value); } /* * 設置緩存字符串 * @param key 緩存key * @param obj 緩存value */ public boolean set(String key, String obj , int expireTime) { try { jedisCluster.setex(key, expireTime, obj); return true; } catch (Exception var6) { var6.printStackTrace(); return false; } } /** * 設置緩存對象 * @param key 緩存key * @param obj 緩存value */ public <T> void setObject(String key, T obj , int expireTime) { jedisCluster.setex(key, expireTime, JSON.toJSONString(obj)); } /** * 獲取指定key的緩存 * @param key---JSON.parseObject(value, User.class); */ public String getObject(String key) { return jedisCluster.get(key); } /** * 判斷當前key值 是否存在 * * @param key */ public boolean hasKey(String key) { return jedisCluster.exists(key); } /** * 設置緩存,並且自己指定過期時間 * @param key * @param value * @param expireTime 過期時間 */ public void setWithExpireTime( String key, String value, int expireTime) { jedisCluster.setex(key, expireTime, value); } /** * 獲取指定key的緩存 * @param key */ public String get(String key) { String value = jedisCluster.get(key); return value; } /** * 刪除指定key的緩存 * @param key */ public void delete(String key) { jedisCluster.del(key); } }
5.測試redis
@Autowired
RedisClientTemplate redisClientTemplate;
@Test public void testSet(){ redisClientTemplate.set("june","june測試redis"); System.out.println(redisClientTemplate.get("june")); }