Jedis整合单机、Sentinel和Cluster模式
程序员文章站
2022-06-24 07:54:43
配置文件和配置类 一、定义jedis接口 二、定义IJedisService的实现类 三、单机模式的具体实现 四、Sentinel模式的具体实现 五、Cluster模式的具体实现 六、注入Bean ......
配置文件和配置类
@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
一、定义jedis接口
public interface ijedisservice {
/**
* 获取jedis连接
* @return jediscommands
*/
jediscommands getjedis();
/**
* 释放连接
* @param jedis
*/
void jedisclose(jediscommands jedis);
/**
* 关闭连接池
*/
void jedispoolclose();
}
二、定义ijedisservice的实现类
@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();
}
}
三、单机模式的具体实现
public class jedissingleservice extends abstractredisservice {
@postconstruct
private void loadjedispool() {
super.initjedispool(jedispool.class);
}
}
四、sentinel模式的具体实现
public class jedissentinelservice extends abstractredisservice {
@postconstruct
private void loadjedispool() {
super.initjedispool(jedissentinelpool.class);
}
}
五、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());
}
}
}
六、注入bean
@bean
public ijedisservice redissinglejedisservice() { return new jedissingleservice(); }
@bean
ijedisservice redissentinelservice() { return new jedissentinelservice(); }
@bean
ijedisservice redisclusterservice() { return new jedisclusterservice(); }
上一篇: Node初识
下一篇: python-day01