最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

spring集成redis cluster詳解

 更新時(shí)間:2017年11月23日 11:32:05   作者:cenmin  
這篇文章主要介紹了spring集成redis cluster詳解,分享了maven依賴,Spring配置,增加connect-redis.properties 配置文件等相關(guān)內(nèi)容,具有一定參考價(jià)值,需要的朋友可以了解下。

客戶端采用最新的jedis 2.7

1.maven依賴:

<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.7.3</version>
</dependency>

2.增加spring 配置

<bean name="genericObjectPoolConfig" class="org.apache.commons.pool2.impl.GenericObjectPoolConfig" >
			<property name="maxWaitMillis" value="-1" />
			<property name="maxTotal" value="1000" />
			<property name="minIdle" value="8" />
			<property name="maxIdle" value="100" />
	</bean>

	<bean id="jedisCluster" class="xxx.JedisClusterFactory">
		<property name="addressConfig">
			<value>classpath:connect-redis.properties</value>
		</property>
		<property name="addressKeyPrefix" value="address" />  <!-- 屬性文件里 key的前綴 -->
		
		<property name="timeout" value="300000" />
		<property name="maxRedirections" value="6" />
		<property name="genericObjectPoolConfig" ref="genericObjectPoolConfig" />
	</bean>

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<JedisCluster>, 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<HostAndPort> parseHostAndPort() throws Exception {
		try {
			Properties prop = new Properties();
			prop.load(this.addressConfig.getInputStream());
			Set<HostAndPort> haps = new HashSet<HostAndPort>();
			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<HostAndPort> 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ì)本站的支持!

相關(guān)文章

  • 詳解SpringBoot異常處理流程及原理

    詳解SpringBoot異常處理流程及原理

    今天給大家?guī)淼氖顷P(guān)于Java的相關(guān)知識(shí),文章圍繞著SpringBoot異常處理流程及原理展開,文中有非常詳細(xì)的介紹及代碼示例,需要的朋友可以參考下
    2021-06-06
  • 解決SpringBoot整合MybatisPlus分模塊管理遇到的bug

    解決SpringBoot整合MybatisPlus分模塊管理遇到的bug

    這篇文章主要介紹了解決SpringBoot整合MybatisPlus分模塊管理遇到的bug,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-07-07
  • Java原子變量類常見問題解決

    Java原子變量類常見問題解決

    這篇文章主要介紹了Java原子變量類常見問題解決,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-03-03
  • SpringBoot實(shí)現(xiàn)項(xiàng)目健康檢查與監(jiān)控

    SpringBoot實(shí)現(xiàn)項(xiàng)目健康檢查與監(jiān)控

    這篇文章主要介紹了SpringBoot實(shí)現(xiàn)項(xiàng)目健康檢查與監(jiān)控,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2018-06-06
  • IDEA如何開啟并配置services窗口

    IDEA如何開啟并配置services窗口

    在使用IntelliJ IDEA時(shí),可能會(huì)遇到Services窗口不自動(dòng)彈出的情況,本文介紹了如何手動(dòng)開啟Services窗口的簡(jiǎn)單步驟,首先,通過點(diǎn)擊菜單欄中的“視圖”->“工具窗口”->“服務(wù)”,或使用快捷鍵Alt+F8(注意快捷鍵可能存在沖突)來打開Services窗口
    2024-10-10
  • Java多線程-線程的同步與鎖的問題

    Java多線程-線程的同步與鎖的問題

    線程的同步是為了防止多個(gè)線程訪問一個(gè)數(shù)據(jù)對(duì)象時(shí),對(duì)數(shù)據(jù)造成的破壞。本篇文章主要介紹了Java多線程-線程的同步與鎖的問題,有興趣的可以了解一下。
    2016-11-11
  • Spring Security OAuth2 實(shí)現(xiàn)登錄互踢的示例代碼

    Spring Security OAuth2 實(shí)現(xiàn)登錄互踢的示例代碼

    這篇文章主要介紹了Spring Security OAuth2實(shí)現(xiàn)登錄互踢的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-04-04
  • Java中Calendar類用法實(shí)例詳解

    Java中Calendar類用法實(shí)例詳解

    這篇文章主要給大家介紹了關(guān)于Java中Calendar類用法的相關(guān)資料,Calendar類是Java.util包中提供的一個(gè)抽象類,該類從JDK1.1開始出現(xiàn),作為Date類的替代方案,Calendar類中包含了對(duì)不同國(guó)家地區(qū)日歷的處理,需要的朋友可以參考下
    2023-09-09
  • Java源碼解析ThreadLocal及使用場(chǎng)景

    Java源碼解析ThreadLocal及使用場(chǎng)景

    今天小編就為大家分享一篇關(guān)于Java源碼解析ThreadLocal及使用場(chǎng)景,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧
    2019-01-01
  • spring boot + mybatis實(shí)現(xiàn)動(dòng)態(tài)切換數(shù)據(jù)源實(shí)例代碼

    spring boot + mybatis實(shí)現(xiàn)動(dòng)態(tài)切換數(shù)據(jù)源實(shí)例代碼

    這篇文章主要給大家介紹了關(guān)于spring boot + mybatis實(shí)現(xiàn)動(dòng)態(tài)切換數(shù)據(jù)源的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2018-10-10

最新評(píng)論

SHOW| 额尔古纳市| 怀来县| 保定市| 襄城县| 靖西县| 苍梧县| 金湖县| 昌黎县| 新平| 军事| 兴业县| 东港市| 嘉峪关市| 班玛县| 湘潭市| 凉城县| 武山县| 越西县| 磴口县| 九龙县| 濉溪县| 通化市| 封丘县| 兰州市| 肇源县| 离岛区| 屯昌县| 曲麻莱县| 贵溪市| 峨山| 神农架林区| 电白县| 扶风县| 永康市| 南丹县| 沾益县| 荃湾区| 桃园县| 保亭| 襄垣县|