欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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)文章

  • 解決mybatis 執(zhí)行mapper的方法時(shí)報(bào)空指針問(wèn)題

    解決mybatis 執(zhí)行mapper的方法時(shí)報(bào)空指針問(wèn)題

    這篇文章主要介紹了解決mybatis 執(zhí)行mapper的方法時(shí)報(bào)空指針問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-07-07
  • 詳談@Autowired和static的關(guān)系

    詳談@Autowired和static的關(guān)系

    這篇文章主要介紹了@Autowired和static的關(guān)系,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-08-08
  • NetBeans安裝提示neatbeans cannot find java 1.8 or higher

    NetBeans安裝提示neatbeans cannot find java 1.8 or higher

    今天小編就為大家分享一篇關(guān)于NetBeans安裝提示neatbeans cannot find java 1.8 or higher,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧
    2019-04-04
  • Java多線程基本用法總結(jié)

    Java多線程基本用法總結(jié)

    本篇文章主要總結(jié)了Java線程的一些基本的用法。具有一定的參考價(jià)值,下面跟著小編一起來(lái)看下吧
    2017-01-01
  • java實(shí)現(xiàn)商品信息管理系統(tǒng)

    java實(shí)現(xiàn)商品信息管理系統(tǒng)

    這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)商品信息管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-11-11
  • java去除if...else的七種方法總結(jié)

    java去除if...else的七種方法總結(jié)

    相信小伙伴一定看過(guò)多篇怎么去掉?if...else?的文章,也知道大家都很有心得,知道多種方法來(lái)去掉?if...else?,本文為大家整理了7個(gè)常用的方法,希望對(duì)大家有所幫助
    2023-11-11
  • Spring中BeanFactory?FactoryBean和ObjectFactory的三種的區(qū)別

    Spring中BeanFactory?FactoryBean和ObjectFactory的三種的區(qū)別

    關(guān)于FactoryBean?和?BeanFactory的對(duì)比文章比較多,但是對(duì)ObjectFactory的描述就比較少,今天我們對(duì)比下這三種的區(qū)別,感興趣的朋友跟隨小編一起看看吧
    2023-01-01
  • 通過(guò)Java實(shí)現(xiàn)添加或刪除PDF中的附件

    通過(guò)Java實(shí)現(xiàn)添加或刪除PDF中的附件

    當(dāng)我們?cè)谥谱鱌DF文件或者PPT演示文稿的時(shí)候,為了讓自己的文件更全面詳細(xì),就會(huì)在文件中添加附件。本文為大家整理了Java實(shí)現(xiàn)添加或刪除PDF中的附件的方法,需要的可以參考下
    2023-01-01
  • 編輯器Ueditor和SpringBoot 的整合方法

    編輯器Ueditor和SpringBoot 的整合方法

    本文通過(guò)實(shí)例代碼給大家介紹了編輯器Ueditor和SpringBoot 的整合方法,需要的朋友參考下吧
    2017-08-08
  • 使用fastjson中的JSONPath處理json數(shù)據(jù)的方法

    使用fastjson中的JSONPath處理json數(shù)據(jù)的方法

    這篇文章主要介紹了使用fastjson中的JSONPath處理json數(shù)據(jù)的方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-04-04

最新評(píng)論