欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

spring集成redis cluster详解

程序员文章站 2024-04-01 17:59:16
客户端采用最新的jedis 2.7 1.maven依赖: redis.clients&...

客户端采用最新的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个节点

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;

总结

以上就是本文关于spring集成redis cluster详解的全部内容,希望对大家有所帮助。如有不足之处,欢迎留言指出。感谢朋友们对本站的支持!