Jedis整合單機、Sentinel和Cluster模式

配置文件和配置類java

@Data
@Configuration
@ConfigurationProperties("jedis-config")
public class JedisConfigure {

    @Value("maxtotal")
    private String maxTotal;
    @Value("maxidle")
    private String maxIdle;
    @Value("minidle")
    private String minIdle;
    @Value("host")
    private String host;
    @Value("port")
    private String port;
    @Value("nodes")
    private Set<String> nodes;
    @Value("master-name")
    private String masterName;
    @Value("timeout")
    private String timeout;
}
//配置文件 放在application.yml
jedis-config:
  maxtotal: 100
  host: 127.0.0.1
  port: 6379
  maxidle: 100
  minidle: 0
  master-name: mymaster
  nodes:
    - 127.0.0.1:7001
    - 127.0.0.1:7002
    - 192.168.1.98:7003
    - 192.168.1.98:7004
    - 192.168.1.20:7005
    - 192.168.1.20:7006

  1、定義jedis接口node

public interface IJedisService {

    /**
     * 獲取Jedis鏈接
     * @return JedisCommands
     */
    JedisCommands getJedis();

    /**
     * 釋放鏈接
     * @param jedis
     */
    void jedisClose(JedisCommands jedis);

    /**
     * 關閉鏈接池
     */
    void jedisPoolClose();
}

  2、定義IJedisService的實現類redis

@EnableConfigurationProperties(JedisConfigure.class)
public class AbstractRedisService implements IJedisService {

    Pool<Jedis> jedisPool;

    @Autowired
    private JedisConfigure jedisConfigure;

    /**
     * jedis鏈接池配置類
     */
    public GenericObjectPoolConfig jedisPoolConfig = new GenericObjectPoolConfig();

    /**
     * 初始化Jedis鏈接池配置
     */
    @PostConstruct
    public void initJedisPoolConfig() {

        jedisPoolConfig.setMaxTotal(Integer.parseInt(jedisConfigure.getMaxTotal()));
        jedisPoolConfig.setMaxIdle(Integer.parseInt(jedisConfigure.getMinIdle()));
        jedisPoolConfig.setMinIdle(Integer.parseInt(jedisConfigure.getMinIdle()));
        jedisPoolConfig.setJmxEnabled(true);
        jedisPoolConfig.setBlockWhenExhausted(true);
        jedisPoolConfig.setTestOnBorrow(true);
        jedisPoolConfig.setTestOnReturn(true);
        jedisPoolConfig.setMaxWaitMillis(-1);
    }

    /**
     *  初始化線程池
     *  根據Class類型初始化
     * @param clazz
     * @return
     */
    public void initJedisPool(Class clazz) {

        //Single
        if (JedisPool.class.getName().equals(clazz.getName()))
            jedisPool = new JedisPool(jedisPoolConfig, jedisConfigure.getHost(),
                    Integer.parseInt(jedisConfigure.getPort()), 100000);
        //Sentinel
        if (JedisSentinelPool.class.getName().equals(clazz.getName()))
            jedisPool = new JedisSentinelPool(jedisConfigure.getMasterName(),
                    jedisConfigure.getNodes(), jedisPoolConfig, 100000);

    }

    @Override
    public JedisCommands getJedis() {
        Jedis jedis = jedisPool.getResource();
        return jedis;
    }

    @Override
    public void jedisClose(JedisCommands jedis) {

        jedisPool.returnResource((Jedis) jedis);
    }

    @Override
    public void jedisPoolClose() {
        if (!jedisPool.isClosed()) jedisPool.close();
    }
}

  3、單機模式的具體實現app

public class JedisSingleService extends AbstractRedisService {

    @PostConstruct
    private void loadJedisPool() {
        super.initJedisPool(JedisPool.class);
    }
}

  4、Sentinel模式的具體實現ide

public class JedisSentinelService extends AbstractRedisService {

    @PostConstruct
    private void loadJedisPool() {
        super.initJedisPool(JedisSentinelPool.class);
    }
}

  5、Cluster模式的具體實現線程

@Slf4j
@EnableConfigurationProperties(JedisConfigure.class)
public class JedisClusterService extends AbstractRedisService {

    @Autowired
    private JedisConfigure jedisConfigure;

    JedisCluster jedis;

    /**
     * 初始化配置
     */
    @PostConstruct
    private void initJedisCluster() {

        //初始化通常配置
        super.initJedisPoolConfig();

        //初始化JedisCluster
        Set<HostAndPort> clusterHostMap = Sets.newHashSet();
        initHostMap(clusterHostMap, jedisConfigure.getNodes());
        jedis = new JedisCluster(clusterHostMap, jedisPoolConfig);
    }

    /**
     * 節點轉換
     *
     * @param clusterHostMap
     * @param nodes
     */
    private void initHostMap(Set<HostAndPort> clusterHostMap, Set<String> nodes) {
        if (clusterHostMap == null)
            return;
        try {
            for (String node : nodes) {

                log.info("初始化Redis-Cluster集羣 --- [{}]", node);
                String[] nodeInfo = node.split(":");

                String ip = nodeInfo[0];
                Integer port = Integer.parseInt(nodeInfo[1]);

                HostAndPort hostAndPort = new HostAndPort(ip, port);
                clusterHostMap.add(hostAndPort);
            }
        } catch (Exception e) {
            throw new RuntimeException("redis-->Cluster集羣節點配置異常");
        }
    }

    @Override
    public JedisCommands getJedis() {
        return jedis;
    }

    @Override
    public void jedisClose(JedisCommands jedis) {

        try {
            ((JedisCluster) jedis).close();
        } catch (IOException e) {
            e.printStackTrace();
            log.error("Redis-Cluster Close失敗 --- [{}]", e.getMessage());
        }
    }
}

  6、注入Beanblog

@Bean
public IJedisService redisSingleJedisService() { return new JedisSingleService(); }

@Bean
IJedisService redisSentinelService() { return new JedisSentinelService(); }

@Bean
IJedisService redisClusterService() { return new JedisClusterService(); }

  

相關文章
相關標籤/搜索