springBoot,Idea 整合redisCluster

自定义redisCluster参数

package com.example.mq.controller;
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.JedisCluster;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
/**
 * 测试rediscluster.
 * Created by daitian on 2017/5/31.
 */
@Component
@ConfigurationProperties(prefix = "spring.redis.cluster")
public class RedisCluseterTest {
    //节点
    private List nodes;
    private int maxidle;
    private int maxTotal;
    private int minidle;
    private Long maxWaitMillis;
    private boolean testOnBorrow;
    private boolean testOnReturn;
    private boolean testWhileIdle;
    //集群配置
    private String password;
    private int connectionTimeout;
    private int soTimeout;
    private int maxAttempts;
    @Bean
    public GenericObjectPoolConfig genericObjectPoolConfig() {
        GenericObjectPoolConfig poolConfig = new GenericObjectPoolConfig();
        poolConfig.setMaxIdle(maxidle);
        poolConfig.setMaxTotal(maxTotal);
        poolConfig.setMinIdle(minidle);
        poolConfig.setMaxWaitMillis(maxWaitMillis);
        poolConfig.setTestOnBorrow(testOnBorrow);
        poolConfig.setTestOnReturn(testOnReturn);
        poolConfig.setTestWhileIdle(testWhileIdle);
        return poolConfig;
    }
    @Bean
    public JedisCluster jedisCluster() {
        Set snodes = new HashSet<>();
        for (Iterator iterator = nodes.iterator(); iterator.hasNext(); ) {
            String next = iterator.next();
            snodes.add(HostAndPort.parseString(next));
        }
//自定义集群密码    
//        return new JedisCluster(snodes, connectionTimeout, soTimeout, maxAttempts, password, genericObjectPoolConfig());
        return new JedisCluster(snodes);
    }
    public List getNodes() {
        return nodes;
    }
    public void setNodes(List nodes) {
        this.nodes = nodes;
    }
    public int getMaxidle() {
        return maxidle;
    }
    public void setMaxidle(int maxidle) {
        this.maxidle = maxidle;
    }
    public int getMaxTotal() {
        return maxTotal;
    }
    public void setMaxTotal(int maxTotal) {
        this.maxTotal = maxTotal;
    }
    public int getMinidle() {
        return minidle;
    }
    public void setMinidle(int minidle) {
        this.minidle = minidle;
    }
    public Long getMaxWaitMillis() {
        return maxWaitMillis;
    }
    public void setMaxWaitMillis(Long maxWaitMillis) {
        this.maxWaitMillis = maxWaitMillis;
    }
    public boolean isTestOnBorrow() {
        return testOnBorrow;
    }
    public void setTestOnBorrow(boolean testOnBorrow) {
        this.testOnBorrow = testOnBorrow;
    }
    public boolean isTestOnReturn() {
        return testOnReturn;
    }
    public void setTestOnReturn(boolean testOnReturn) {
        this.testOnReturn = testOnReturn;
    }
    public boolean isTestWhileIdle() {
        return testWhileIdle;
    }
    public void setTestWhileIdle(boolean testWhileIdle) {
        this.testWhileIdle = testWhileIdle;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    public int getConnectionTimeout() {
        return connectionTimeout;
    }
    public void setConnectionTimeout(int connectionTimeout) {
        this.connectionTimeout = connectionTimeout;
    }
    public int getSoTimeout() {
        return soTimeout;
    }
    public void setSoTimeout(int soTimeout) {
        this.soTimeout = soTimeout;
    }
    public int getMaxAttempts() {
        return maxAttempts;
    }
    public void setMaxAttempts(int maxAttempts) {
        this.maxAttempts = maxAttempts;
    }
}

aplication.yml中初始化redis配置

spring:
  application:
    name: coffeestore
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://localhost:3306/joinu
    username: root
    password: root
  redis:
    cluster:
     #集群配置
     connectionTimeout: 2000
     soTimeout: 1000
     maxAttempts: 5
     password: hiteam666
     maxTotal: 15
     #连接池配置
     maxidle: 10
     minidle: 10
     maxWaitMillis: 1000
     testOnBorrow: true
     testOnReturn: false
     testWhileIdle: false
     #节点
     nodes:
      - 17.92.74.41:7001
      - 17.92.74.41:7002
      - 17.92.74.41:7003
      - 17.92.74.41:7004
      - 17.92.74.41:7005
      - 17.92.74.41:7006

配置完上边就可以直接使用了.使用方法很简单

@Autowired
JedisCluster jedisCluster;

可以自定义工具方便使用

import java.util.List;
import java.util.Map;
import java.util.Set;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.hiteamtech.joinu.kit.StrKit;
import redis.clients.jedis.JedisCluster;
@Service
public class RedisKit implements IRedisKit{

    /**日志记录器*/
    Logger log = Logger.getLogger(RedisKit.class);

    @Autowired
    private JedisCluster jedisCluster;

    //是否成功
    private boolean isOK(String s){
        return StrKit.notBlank(s)&&"OK".equals(s);
    }
    @Override
    public String getStrByKey(String key) {
        return StrKit.isBlank(key)?"":jedisCluster.get(key);
    }
    @Override
    public boolean putStrTime(String key, String value, int time) {
        return isOK(jedisCluster.setex(key, time, value));
    }
    @Override
    public boolean putStr(String key, String value) {
        return isOK(jedisCluster.set(key, value));
    }
    @Override
    public long delete(String key) {
        return StrKit.isBlank(key)?0:jedisCluster.del(key);
    }
    @Override
    public boolean saveMap(String key, Map map) {
        return (map!=null&&map.size()>0)&&isOK(jedisCluster.hmset(key,map));
    }
    @Override
    public boolean mapKeyRise(String key, String mapKey, int num) {
        try {
            if(StrKit.isBlank(key)){
                return false;   
            }
            jedisCluster.hincrBy(key,mapKey,num);
        } catch (Exception e) {
            return false;
        }
        return true;
    }
    @Override
    public String getValueByMapKey(String mapRedisKey, String mapKey) {
        if(StrKit.isBlank(mapKey)||StrKit.isBlank(mapRedisKey)){
            return "";
        }
        return jedisCluster.hget(mapRedisKey, mapKey);
    }
    @Override
    public List getMap(String key) {
        if(StrKit.isBlank(key)){
            return null;
        }
        return jedisCluster.hvals(key);
    }
    @Override
    public boolean delMap(String key, String... value) {
        try {
            if(StrKit.isBlank(key)){
                return false;
            }
            jedisCluster.hdel(key, value);
        } catch (Exception e) {
            return false;
        }
        return true;
    }

    @Override
    public boolean addToSet(String key, String... members) {
        try {
            if(StrKit.isBlank(key)){
                return false;
            }
            jedisCluster.sadd(key, members);
        } catch (Exception e) {
            return false;
        }
        return true;
    }
    @Override
    public boolean addToZSets(String key, String members, int score) {
        try {
            if(StrKit.isBlank(key)){
                return false;
            }
            jedisCluster.zadd(key,score,members);
        } catch (Exception e) {
            return false;
        }
        return true;
    }
    @Override
    public long getSetObjNum(String key) {
        return StrKit.isBlank(key)?0:jedisCluster.scard(key);
    }
    @Override
    public Set getSet(String key) {
        return StrKit.isBlank(key)?null:jedisCluster.smembers(key);
    }
    @Override
    public Set getZSet(String key) {
        return StrKit.isBlank(key)?null:jedisCluster.zrange(key, 0, -1);
    }
    @Override
    public List getRandomSet(String key, int count) {
        return StrKit.isBlank(key)?null:jedisCluster.srandmember(key, count);
    }
    @Override
    public boolean getKeySet(String key, String value) {
        return StrKit.notBlank(key)&&jedisCluster.sismember(key, value);
    }
    @Override
    public boolean Srem(String key, String... value) {
        return StrKit.notBlank(key)&&(jedisCluster.srem(key, value)>0);
    }
    @Override
    public boolean ZSrem(String key, String... value) {
        return StrKit.notBlank(key)&&(jedisCluster.zrem(key, value)>0);
    }
    @Override
    public boolean expire(String key, int seconds) {
        return StrKit.notBlank(key)&&(jedisCluster.expire(key, seconds)==1);
    }
    @Override
    public Map getMapHgetall(String mapRedisKey) {
        return StrKit.isBlank(mapRedisKey)?null:jedisCluster.hgetAll(mapRedisKey);
    }
}

你可能感兴趣的:(springboot)