一:redisson和 redisCluster性能对比
组件 | 操作 | 数量 | 耗时(ms) |
jediscluster | hset | 100000 | 213959 |
jediscluster | hget | 100000 | 228830 |
jediscluster | hdel | 100000 | |
redisson | hset | 100000 | 116167 |
redisson | hget | 100000 | 37631 |
redisson | hdel | 100000 |
二:java redisCluster单元测试
1:src/main/resources/redis.properties
cluster1.host.port=192.168.18.37:7000
cluster2.host.port=192.168.18.37:7001
cluster3.host.port=192.168.18.37:7002
cluster4.host.port=192.168.18.37:7003
cluster5.host.port=192.168.18.37:7004
cluster6.host.port=192.168.18.37:7005
redis.scanInterval=10000
redis.maxIdle=100
redis.maxActive=300
redis.maxWait=1000
redis.timeout=100000
redis.maxTotal=1000
redis.minIdle=8
redis.testOnBorrow=true
2:src/main/resources/redis.properties/spring-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"
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-3.0.xsd
">
<context:component-scan base-package="com.niiwoo.rce.module.redis" />
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:redis.properties" />
</bean>
<!-- jedis 配置-->
<bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig" >
<!--最大空闲数-->
<property name="maxIdle" value="${redis.maxIdle}" />
<!--最大建立连接等待时间-->
<property name="maxWaitMillis" value="${redis.maxWait}" />
<!--是否在从池中取出连接前进行检验,如果检验失败,则从池中去除连接并尝试取出另一个-->
<property name="testOnBorrow" value="${redis.testOnBorrow}" />
<property name="maxTotal" value="${redis.maxTotal}" />
<property name="minIdle" value="${redis.minIdle}" />
</bean >
<bean id="jedisCluster" class="com.niiwoo.rce.jediscluster.JedisClusterFactory" >
<property name="addressConfig">
<value>classpath:redis.properties</value>
</property>
<property name="addressKeyPrefix" value="cluster" /> <!-- 属性文件里 key的前缀 -->
<property name="timeout" value="300000" />
<property name="maxRedirections" value="6" />
<property name="genericObjectPoolConfig" ref="poolConfig" />
</bean >
<bean id="mystudent" class="com.niiwoo.rce.jediscluster.MyStudent" />
</beans>
3: pom.xml依赖
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.7.2</version>
</dependency>
4:
package com.niiwoo.rce.jediscluster;
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) {
ex.printStackTrace();
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:
package com.niiwoo.rce.jediscluster;
import java.util.UUID;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import redis.clients.jedis.JedisCluster;
import com.niiwoo.rce.module.redis.RedisManager;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:spring-context.xml")
public class JedeisClusterTest {
@BeforeClass
public static void init() {
System.out.println("system is init");
}
@Autowired
private MyStudent myStudent;
@Test
public void testJedisCluster(){
myStudent.setName("www");
myStudent.setAge(12);
System.out.println("testJedisCluster");
/* myjedisCluster.set("name", "啊芝");
String val = myjedisCluster.get("name");
System.out.println(val);*/
}
@Autowired
private JedisCluster jedisCluster;
@Autowired
private RedisManager redisManager;
@Test
public void testRedisManager() {
}
@Test
public void testJedisCluster1(){
// jedisCluster.hset("sessionmap_2", key, "hisname5");
// jedisCluster.hdel("sessionmap_2", key);
// jedisCluster.hgetAll("sessionmap_2");
/*Map<String, String> maps = jedisCluster.hgetAll("sessionmap_2");
for (String s : maps.keySet()) {
System.out.println(s + "\t" + maps.get(s));
}*/
long start=System.currentTimeMillis();
String key = UUID.randomUUID().toString();
redisManager.hset("sessionmap_2", key, key);
/*for (int i = 0; i < 10_0000; i++) {
String key = UUID.randomUUID().toString();
jedisCluster.hset("sessionmaIp_2", key, key);
}*/
/*Map<String, String> maps = jedisCluster.hgetAll("sessionmap_2");
for (String key : maps.keySet()) {
jedisCluster.hdel("sessionmap_2", key);
}*/
/* for (int i = 0; i < 10_0000;i++) {
String key=UUID.randomUUID().toString();
jedisCluster.hset("sessionmap_2", key, key);
}*/
System.out.println("耗时:"+(System.currentTimeMillis()-start));
// jedisCluster.hdel("sessionmap_2", "24c45f47-b8cf-4cf7-be9f-489a87e6397c");
// jedisCluster.setnx(key, value)
// jedisCluster.set("myname", "啊芝");
// String val = jedisCluster.get("myname");
// System.out.println("testJedisCluster1");
// System.out.println(val);
}
}