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

spring集成redis cluster詳解

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

客戶端采用最新的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個節(jié)點

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.到此配置完成

使用時,直接注入即可, 如下所示:

@Autowired
JedisCluster jedisCluster;

總結(jié)

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

相關(guān)文章

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

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

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

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

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

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

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

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

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

    IDEA如何開啟并配置services窗口

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

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

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

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

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

    Java中Calendar類用法實例詳解

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

    Java源碼解析ThreadLocal及使用場景

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

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

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

最新評論

竹北市| 嵊泗县| 来凤县| 松原市| 天门市| 太和县| 华坪县| 铅山县| 荣成市| 贺州市| 普定县| 齐齐哈尔市| 建德市| 抚远县| 千阳县| 镇远县| 汕尾市| 浏阳市| 社旗县| 兰西县| 涿州市| 天水市| 乌恰县| 米泉市| 从化市| 界首市| 泾阳县| 新余市| 乌鲁木齐县| 江阴市| 灵川县| 蛟河市| 宜川县| 云浮市| 合肥市| 嘉黎县| 肇源县| 万载县| 凤翔县| 合阳县| 睢宁县|