日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

java集成redis集群_spring集成redis cluster详解

發(fā)布時(shí)間:2025/4/5 编程问答 15 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java集成redis集群_spring集成redis cluster详解 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

客戶端采用最新的jedis 2.7

1.maven依賴:

redis.clients

jedis

2.7.3

2.增加spring 配置

classpath:connect-redis.properties

3.增加connect-redis.properties 配置文件

這里配置了6個(gè)節(jié)點(diǎn)

address1=*:*

address2=*:*

address3=*:*

address4=*:*

address5=*:*

address6=*:*

4.增加java類:

import java.util.HashSet;

import java.util.Properties;

import java.util.Set;

import java.util.regex.Pattern;

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

import org.springframework.beans.factory.FactoryBean;

import org.springframework.beans.factory.InitializingBean;

import org.springframework.core.io.Resource;

import redis.clients.jedis.HostAndPort;

import redis.clients.jedis.JedisCluster;

public class JedisClusterFactory implements FactoryBean, InitializingBean {

private Resource addressConfig;

private String addressKeyPrefix ;

private JedisCluster jedisCluster;

private Integer timeout;

private Integer maxRedirections;

private GenericObjectPoolConfig genericObjectPoolConfig;

private Pattern p = Pattern.compile("^.+[:]\\d{1,5}\\s*$");

@Override

public JedisCluster getObject() throws Exception {

return jedisCluster;

}

@Override

public Class extends JedisCluster> getObjectType() {

return (this.jedisCluster != null ? this.jedisCluster.getClass() : JedisCluster.class);

}

@Override

public Boolean isSingleton() {

return true;

}

private Set parseHostAndPort() throws Exception {

try {

Properties prop = new Properties();

prop.load(this.addressConfig.getInputStream());

Set haps = new HashSet();

for (Object key : prop.keySet()) {

if (!((String) key).startsWith(addressKeyPrefix)) {

continue;

}

String val = (String) prop.get(key);

Boolean isIpPort = p.matcher(val).matches();

if (!isIpPort) {

throw new IllegalArgumentException("ip 或 port 不合法");

}

String[] ipAndPort = val.split(":");

HostAndPort hap = new HostAndPort(ipAndPort[0], Integer.parseint(ipAndPort[1]));

haps.add(hap);

}

return haps;

}

catch (IllegalArgumentException ex) {

throw ex;

}

catch (Exception ex) {

throw new Exception("解析 jedis 配置文件失敗", ex);

}

}

@Override

public void afterPropertiesSet() throws Exception {

Set haps = this.parseHostAndPort();

jedisCluster = new JedisCluster(haps, timeout, maxRedirections,genericObjectPoolConfig);

}

public void setAddressConfig(Resource addressConfig) {

this.addressConfig = addressConfig;

}

public void setTimeout(int timeout) {

this.timeout = timeout;

}

public void setMaxRedirections(int maxRedirections) {

this.maxRedirections = maxRedirections;

}

public void setAddressKeyPrefix(String addressKeyPrefix) {

this.addressKeyPrefix = addressKeyPrefix;

}

public void setGenericObjectPoolConfig(GenericObjectPoolConfig genericObjectPoolConfig) {

this.genericObjectPoolConfig = genericObjectPoolConfig;

}

}

5.到此配置完成

使用時(shí),直接注入即可, 如下所示:

@Autowired

JedisCluster jedisCluster;

總結(jié)

以上就是本文關(guān)于spring集成redis cluster詳解的全部?jī)?nèi)容,希望對(duì)大家有所幫助。如有不足之處,歡迎留言指出。感謝朋友們對(duì)本站的支持!

總結(jié)

以上是生活随笔為你收集整理的java集成redis集群_spring集成redis cluster详解的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。