JFinal redis cluster集群插件

JFinal 框架到了2.1版本号,可是依旧仅仅支持redis的主从集群,没有看到Cluster集群的插件。笔者照着主从的插件方式,改了改,实现了个简单的插件,先使用起来,兴许会更新完好版本号。


插件地址:点击打开链接

附上源代码:
package com.sxt.jfinal.rediscluster;

import java.util.Set;

import org.apache.commons.pool2.impl.GenericObjectPoolConfig;

import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.JedisCluster; import com.jfinal.kit.StrKit;
import com.jfinal.plugin.IPlugin; /**
* 为JFinal框架下的 redis cluster集群提供插件方案
*
* JFinal版本号 2.1
* Jedis版本号 2.7.2
* commons-pools版本号2.3
*
* 注意:
* 须要例如以下包才干够正常使用
* jedis-2.7.2.jar
* commons-pool2-2.3.jar
*
* @author 石啸天
*
*/
public class RedisClusterPlugin implements IPlugin{ // 集群名称
String clusterName = null; // 集群对象
JedisCluster jedisCluster = null; // 超时时间
Integer timeout = null; // 连接池
GenericObjectPoolConfig poolConfig = null; // 最多重定向次数
Integer maxRedirections = null; // 集群地址集合
Set<HostAndPort> redisClusterNodes; /**
*
* 传入集群信息
*
* @param clusterName 集群名称
* @param redisClusterNodes 集群地址集合
*
*/
public RedisClusterPlugin(String clusterName, Set<HostAndPort> redisClusterNodes) { // 检查数据
this.isRightHostAndPortSet(clusterName, redisClusterNodes); // 绑定集群名称
this.clusterName = clusterName; // 绑定地址集合
this.redisClusterNodes = redisClusterNodes; } /**
*
* 传入集群信息
*
* @param clusterName 集群名称
* @param redisClusterNodes 集群地址集合
* @param timeout 超时时间
*
*/
public RedisClusterPlugin(String clusterName, Set<HostAndPort> redisClusterNodes, Integer timeout) { // 复用传入集群方法
this(clusterName, redisClusterNodes); // 超时时间绑定
this.timeout = timeout; } /**
*
* 传入集群信息
*
* @param clusterName 集群名称
* @param redisClusterNodes 集群地址集合
* @param poolConfig 连接池对象
*
*/
public RedisClusterPlugin(String clusterName, Set<HostAndPort> redisClusterNodes, GenericObjectPoolConfig poolConfig) { // 复用传入集群方法
this(clusterName, redisClusterNodes); // 连接池绑定
this.poolConfig = poolConfig; } /**
*
* 传入集群信息
*
* @param clusterName 集群名称
* @param redisClusterNodes 集群地址集合
* @param timeout 超时时间
* @param poolConfig 连接池配置
*
*/
public RedisClusterPlugin(String clusterName, Set<HostAndPort> redisClusterNodes, Integer timeout, GenericObjectPoolConfig poolConfig) { // 复用传入集群方法
this(clusterName, redisClusterNodes, timeout); // 连接池绑定
this.poolConfig = poolConfig; } /**
*
* 传入集群信息
*
* @param clusterName 集群名称
* @param redisClusterNodes 集群地址集合
* @param poolConfig 连接池对象
*
*/
public RedisClusterPlugin(String clusterName, Set<HostAndPort> redisClusterNodes, Integer timeout, Integer maxRedirections) { // 复用传入集群方法
this(clusterName, redisClusterNodes, timeout); // 连接池绑定
this.maxRedirections = maxRedirections; } /**
*
* 传入集群信息
*
* @param clusterName 集群名称
* @param redisClusterNodes 集群地址集合
* @param poolConfig 连接池对象
*
*/
public RedisClusterPlugin(String clusterName, Set<HostAndPort> redisClusterNodes, Integer timeout, Integer maxRedirections, GenericObjectPoolConfig poolConfig) { // 复用传入集群方法
this(clusterName, redisClusterNodes, timeout, maxRedirections); // 连接池绑定
this.poolConfig = poolConfig; } @Override
public boolean start() { if(timeout != null && maxRedirections != null && poolConfig != null) {
jedisCluster = new JedisCluster(redisClusterNodes, timeout, maxRedirections, poolConfig);
} else if(timeout != null && maxRedirections != null) {
jedisCluster = new JedisCluster(redisClusterNodes, timeout, maxRedirections);
} else if(timeout != null && poolConfig != null) {
jedisCluster = new JedisCluster(redisClusterNodes, timeout, poolConfig);
} else if(timeout != null) {
jedisCluster = new JedisCluster(redisClusterNodes, timeout);
} else if(poolConfig != null){
jedisCluster = new JedisCluster(redisClusterNodes, poolConfig);
} else {
jedisCluster = new JedisCluster(redisClusterNodes);
} // 增加集群集合
RedisCluster.addCache(clusterName, jedisCluster); return true;
} @Override
public boolean stop() { // 清除出集群集合
JedisCluster removeRedisCluster = RedisCluster.removeCache(clusterName); // 关闭集群链接
removeRedisCluster.close(); return false; } // 推断传入的集群位置资料是否正确
private void isRightHostAndPortSet(String clusterName, Set<HostAndPort> redisClusterNodes) { // 集群名称不能为空
if (StrKit.isBlank(clusterName)) {
throw new IllegalArgumentException("clusterName can not be blank.");
} // 检查集群详细地址和端口号是否正常
if(redisClusterNodes != null && redisClusterNodes.size()>0) {
for(HostAndPort hap : redisClusterNodes) { // 获取主机ip
String host = hap.getHost(); // 空字符串
if (StrKit.isBlank(host)) {
throw new IllegalArgumentException("host can not be blank.");
} // 获取端口
Integer port = hap.getPort(); // 空端口数据
if(port == null) {
throw new IllegalArgumentException("port can not be blank.");
} }
} else { // 集群集合数据为空
throw new IllegalArgumentException("redisClusterNodes can not be blank."); } } }


package com.sxt.jfinal.rediscluster;

import java.util.concurrent.ConcurrentHashMap;

import redis.clients.jedis.JedisCluster;

import com.jfinal.kit.StrKit;

/**
* redis cluster 工具类
*
* @author 石啸天
*
*/
public class RedisCluster { // 主集群缓存
static JedisCluster mainCache = null; // 集群缓存集合
private static final ConcurrentHashMap<String, JedisCluster> cacheMap = new ConcurrentHashMap<String, JedisCluster>(); /**
* 插入新集群缓存
*
* @param cacheName 集群缓存名称
*
* @param cache 集群缓存
*/
public static void addCache(String cacheName, JedisCluster cache) { if (cache == null)
throw new IllegalArgumentException("cache can not be null");
if (cacheMap.containsKey(cacheName))
throw new IllegalArgumentException("The cache name already exists"); cacheMap.put(cacheName, cache);
if (mainCache == null)
mainCache = cache; } /**
*
* 删除集群缓存
*
* @param cacheName 集群缓存名称
*
* @return JedisCluster
*
*/
public static JedisCluster removeCache(String cacheName) { return cacheMap.remove(cacheName); } /**
* 提供一个设置设置主集群缓存 mainCache 的机会,否则第一个被初始化的 Cache 将成为 mainCache
*/
public static void setMainCache(String cacheName) { if (StrKit.isBlank(cacheName))
throw new IllegalArgumentException("cacheName can not be blank");
cacheName = cacheName.trim();
JedisCluster cache = cacheMap.get(cacheName);
if (cache == null)
throw new IllegalArgumentException("the cache not exists: " + cacheName); RedisCluster.mainCache = cache; } /**
*
* 使用主集群缓存
*
* @return JedisCluster
*/
public static JedisCluster use() {
return mainCache;
} /**
*
* 使用指定名称集群缓存
*
* @param cacheName 集群缓存名称
*
* @return JedisCluster
*/
public static JedisCluster use(String cacheName) {
return cacheMap.get(cacheName);
} }

JFinal插件加载方式:

       /**
* 配置插件
*/
public void configPlugin(Plugins me) { // redis cluster集群节点
Set<HostAndPort> jedisClusterNodes = new HashSet<HostAndPort>();
jedisClusterNodes.add(new HostAndPort("192.168.1.200", 7000));
jedisClusterNodes.add(new HostAndPort("192.168.1.200", 7001));
jedisClusterNodes.add(new HostAndPort("192.168.1.200", 7002)); // 创建插件对象
RedisClusterPlugin redisClusterPlugin = new RedisClusterPlugin("sxt", jedisClusterNodes); // 加载插件
me.add(redisClusterPlugin); }

使用方式:

// 获取redis使用对象
JedisCluster redis = RedisCluster.use("sxt");
// 设置值
redis.set("f", "起飞");
// 获取值
String result = redis.get("f");
// 输出
System.out.println(result);</span>

插件地址:点击打开链接

JFinal redis cluster集群插件的更多相关文章

  1. redis cluster集群中键的分布算法

    Redis Cluster Redis Cluster是Redis的作者 Antirez 提供的 Redis 集群方案 —— 官方多机部署方案,每组Redis Cluster是由多个Redis实例组成 ...

  2. Redis Cluster集群搭建与配置

    Redis Cluster是一种服务器sharding分片技术,关于Redis的集群方案应该怎么做,请参考我的另一篇博客http://www.cnblogs.com/xckk/p/6134655.ht ...

  3. jedis处理redis cluster集群的密码问题

    环境介绍:jedis:2.8.0 redis版本:3.2 首先说一下redis集群的方式,一种是cluster的 一种是sentinel的,cluster的是redis 3.0之后出来新的集群方式 本 ...

  4. 深入分析redis cluster 集群

    深入分析redis cluster 集群安装配置详解 下面小编来为各位介绍一篇深入分析redis cluster 集群安装配置详解,如果你希望做数据库集群就可以来看看此文章的哦. http://rub ...

  5. Redis Cluster集群搭建与应用

    1.redis-cluster设计 Redis集群搭建的方式有多种,例如使用zookeeper,但从redis 3.0之后版本支持redis-cluster集群,redis-cluster采用无中心结 ...

  6. Redis Cluster集群主从方案

    本文介绍一种通过Jedis和Cluster实现Redis集群(主从)的高可用方案,该方案需要使用Jedis2.8.0(推荐),Redis3.0及以上版本(强制). 附:Redis Cluster集群主 ...

  7. CentOS7 安装Redis Cluster集群

    上一篇中已经讲到了如何安装单击版Redis,这一篇我们来说下如何安装Cluster,关于哨兵模式这里我就不写文章安装了,有兴趣的同学可以自己去研究,哨兵模式可以在主从模式下在创建三台机器的哨兵集群监控 ...

  8. Redis Cluster集群架构实现(四)--技术流ken

    Redis集群简介 通过前面三篇博客的介绍<Redis基础认识及常用命令使用(一)--技术流ken>,<Redis基础知识补充及持久化.备份介绍(二)--技术流ken>,< ...

  9. 【精】搭建redis cluster集群,JedisCluster带密码访问【解决当中各种坑】!

    转: [精]搭建redis cluster集群,JedisCluster带密码访问[解决当中各种坑]! 2017年05月09日 00:13:18 冉椿林博客 阅读数:18208  版权声明:本文为博主 ...

随机推荐

  1. 洛谷 P1334 瑞瑞的木板

    P1334 瑞瑞的木板 题目描述 瑞瑞想要亲自修复在他的一个小牧场周围的围栏.他测量栅栏并发现他需要N(1≤N≤20,000)根木板,每根的长度为整数Li(1≤Li≤50,000).于是,他神奇地买了 ...

  2. Jenkins学习总结(1)——Jenkins详细安装与构建部署使用教程

    Jenkins是一个开源软件项目,旨在提供一个开放易用的软件平台,使软件的持续集成变成可能.Jenkins是基于Java开发的一种持续集成工具,用于监控持续重复的工作,功能包括: 1.持续的软件版本发 ...

  3. js进阶 14-2 如何用ajax验证登陆状态(这里用load方法)

    js进阶 14-2 如何用ajax验证登陆状态(这里用load方法) 一.总结 一句话总结:$('#test').load('test.php?password=1234560'),这样就get方式提 ...

  4. 1、移动端 2、后台 3、 移动端,Web 端 4、 PC端

    移动端: 1.公众号:停开心 住总物业 2.app:  iso Android 停开心,住总停开心 后台:停开心智慧停车管理平台(所有的停车场) 移动端,Web端: 海投OA,公司OA PC端:收费软 ...

  5. PB导出数据excel格式dw2xls

    PB导出数据excel格式dw2xls 使用DW2XLS控件 语法 uf_save_dw_as_excel ( dw, filename ) 參数 dw A reference to the data ...

  6. [Android 4.4.2] 泛泰A870 Mokee4.4.2 20140531 RC1.0 by syhost

    欢迎关注泛泰非盈利专业第三方开发团队 VegaDevTeam  (本team 由 syhost suky zhaochengw(z大) xuefy(大星星) tenfar(R大师) loogeo cr ...

  7. UVA 11609 - Teams 组合、快速幂取模

    看题传送门 题目大意: 有n个人,选一个或者多个人参加比赛,其中一名当队长,如果参赛者相同,队长不同,也算一种方案.求一共有多少种方案. 思路: 排列组合问题. 先选队长有C(n , 1)种 然后从n ...

  8. 6.4 Android硬件访问服务编写HAL代码

    JNI向上提供本地函数,向下加载HAL文件,并调用HAL的函数: HAL负责访问驱动程序执行硬件操作 JNI和HAL都是用c语言或者C++语言编写的,JNI加载HAL的实质就是使用dlopen加载动态 ...

  9. 【BZOJ 4516】生成魔咒

    [链接]h在这里写链接 [题意]     [Description]         给你n(n<=10^9)个数字,把它们依次,一个一个地添加在空串S的后面.         要求每添加一次之 ...

  10. java项目中VO和DTO以及Entity,各自是在什么情况下应用的

    j2ee中,经常提到几种对象(object),理解他们的含义有助于我们更好的理解面向对象的设计思维.     POJO(plain old java object):普通的java对象,有别于特殊的j ...