首先附上maven仓库jar包的下载地址:https://repo.spring.io/webapp/#/artifacts/browse/tree/General/libs-release-local/org
首先在linux系统安装redis3.0以上的版本,并且保证redis集群已经启动:
本次项目所需jar包:
完整图视:
1 新建属性文件:在src/conf/redis.properties:
address0=127.0.0.1:7000
address1=127.0.0.1:7001
address2=127.0.0.1:7002
address3=127.0.0.1:7003
address4=127.0.0.1:7004
address5=127.0.0.1:7005
redis.timeout=300000
redis.maxActive=1024
redis.minIdle=8
redis.maxIdle=100
redis.maxWaitMillis=1000
redis.maxRedirections=6
redis.testOnBorrow=true
2 新建文件夹 :src/xml/redis-context.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<!-- 加载配置文件 -->
<context:property-placeholder location="classpath:/conf/redis.properties" ignore-unresolvable="true"/>
<context:component-scan base-package="conf"/>
<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" />
<property name="testOnBorrow" value="true" />
</bean>
<bean id="jedisCluster" class="testDao.JedisClusterFactory">
<property name="addressConfig" value="classpath:/conf/redis.properties"/>
<property name="addressKeyPrefix" value="address" /> <!-- 属性文件里 key的前缀 -->
<property name="timeout" value="300000" />
<property name="maxRedirections" value="6" />
<property name="genericObjectPoolConfig" ref="genericObjectPoolConfig" />
</bean>
</beans>
3 实现bean工厂:src/testDao/JedisClusterFactory
package testDao;
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*$");
public JedisClusterFactory() {
}
@Override
public void afterPropertiesSet() throws Exception {
Set<HostAndPort> haps = this.parseHostAndPort();
jedisCluster = new JedisCluster(haps, timeout, maxRedirections,genericObjectPoolConfig);
}
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 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;
}
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;
}
}
4 新建Test类 测试redis-cluster
public class Test {
@Autowired
static
JedisCluster jedisCluster;
private static ApplicationContext context;
static{
context = new ClassPathXmlApplicationContext("classpath:/xml/redis-context.xml");
}
public static void main(String[] args) {
jedisCluster = (JedisCluster) context.getBean("jedisCluster",JedisCluster.class);
System.out.println(jedisCluster.get("name1"));
System.out.println(jedisCluster.get("name2"));
System.out.println(jedisCluster.get("first"));
int num = 100;
String key = "wusc";
String value = "";
for (int i=1; i <= num; i++){
// 存数据
// jedisCluster.set(key+i,"WuShuicheng"+i);
// 取数据
value= jedisCluster.get(key+i);
System.out.println(value);
// // 删除数据
// jedisCluster.del(key+i);
}
}
}