Commit 0d225da4 by liqiuwei

完善demo

parent 8798fd1d
......@@ -10,6 +10,24 @@
<modelVersion>4.0.0</modelVersion>
<artifactId>matrix-sample-redis-biz</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</dependency>
</dependencies>
</project>
\ No newline at end of file
/**
* Created by Administrator on 2018/1/16.
*/
package com.secooframework.redis;
package com.secoo.mall.redis;
import lombok.Data;
import java.util.Date;
......@@ -11,6 +13,7 @@ import java.util.Date;
* @author Administrator
* @create 2018-01-16 11:19
**/
@Data
public class Person {
/**
......@@ -23,9 +26,16 @@ public class Person {
private String loginName;
/**
* 用户名
*/
private String userName;
/**
* 部门名称
*/
private String deptName;
// private String deptName;
/**
* 操作时间
......@@ -48,13 +58,6 @@ public class Person {
this.loginName = loginName;
}
public String getDeptName() {
return deptName;
}
public void setDeptName(String deptName) {
this.deptName = deptName;
}
public Date getOperDate() {
return operDate;
......@@ -65,13 +68,4 @@ public class Person {
}
@Override
public String toString() {
return "Person{" +
"id=" + id +
", loginName='" + loginName + '\'' +
", deptName='" + deptName + '\'' +
", operDate=" + operDate +
'}';
}
}
/**
* Created by Administrator on 2018/1/17.
*/
package com.secooframework.redis;
package com.secoo.mall.redis;
import com.secooframework.redis.annotation.RedisCache;
import java.util.Date;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import java.util.Date;
/**
* aa
*
......@@ -20,12 +20,11 @@ public class PersonService {
private final static Logger logger = LoggerFactory.getLogger(PersonService.class);
@RedisCache(expired = 60L, key = "'frame:PersonTest:'+#id")
public Person get(Long id) {
logger.info("get;{}", id);
Person p = new Person();
p.setOperDate(new Date());
p.setDeptName("ddd");
// p.setDeptName("ddd");
p.setId(id);
return p;
}
......
......@@ -10,6 +10,40 @@
<modelVersion>4.0.0</modelVersion>
<artifactId>matrix-sample-redis-spring</artifactId>
<dependencies>
<dependency>
<groupId>com.secoo.mall</groupId>
<artifactId>matrix-datahelper-redis-core</artifactId>
</dependency>
<dependency>
<groupId>com.secoo.mall</groupId>
<artifactId>matrix-sample-redis-biz</artifactId>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
</dependency>
<dependency>
<groupId>io.lettuce</groupId>
<artifactId>lettuce-core</artifactId>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
</dependencies>
</project>
\ No newline at end of file
package com.secoo.mall.redis;
import com.secoo.mall.redis.helper.RedisHelper;
import org.junit.Assert;
import org.junit.Test;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;
import javax.annotation.Resource;
import java.util.*;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
/**
* Created by Administrator on 2018/1/15.
*/
//@ContextConfiguration({"classpath*:spring-jedis-cluster-config-sample.xml"})
@ContextConfiguration({"classpath*:spring-lettuce-cluster-config-sample.xml"})
public class RedisClusterTest extends AbstractJUnit4SpringContextTests {
@Resource
private RedisHelper redisClusterHelper;
@Resource
private PersonService personService;
@Test
public void testSet() throws InterruptedException {
redisClusterHelper.set("aaa", "bbb");
String ret = redisClusterHelper.get("aaa", String.class);
Assert.assertEquals("bbb", ret);
redisClusterHelper.set("aaa", "ccc", 2l);
ret = redisClusterHelper.get("aaa", String.class);
Assert.assertEquals("ccc", ret);
Thread.sleep(3000l);
ret = redisClusterHelper.get("aaa", String.class);
Assert.assertNull(ret);
}
@Test
public void testSet2() throws InterruptedException {
//批量设置测试
Map<String, String> dtos1 = new HashMap<>();
for (int i = 0; i < 100; i++) {
dtos1.put("testKey" + i, "testValue" + i);
}
long st = System.currentTimeMillis();
redisClusterHelper.sets(dtos1, 1000L);
long ed = System.currentTimeMillis();
System.out.println("耗时:" + (ed - st));
for (int i = 0; i < 100; i++) {
dtos1.put("testKeyi" + i, "testValue" + i);
}
st = System.currentTimeMillis();
redisClusterHelper.sets(dtos1, 1000L);
ed = System.currentTimeMillis();
System.out.println("耗时:" + (ed - st));
List<String> list = redisClusterHelper.gets(new ArrayList<String>(dtos1.keySet()), String.class);
System.out.println(list);
Map<String, String> dtos2 = new HashMap<>();
for (int i = 0; i < 1000; i++) {
dtos2.put("testKey2:" + i, "testValue2:" + i);
}
st = System.currentTimeMillis();
redisClusterHelper.pipelineSets(dtos2, 1000L);
ed = System.currentTimeMillis();
System.out.println("耗时2:" + (ed - st));
}
/**
* 测试多线程
*/
@Test
public void testThreads() throws InterruptedException {
ExecutorService executorService = Executors.newCachedThreadPool();
for (int i = 0; i < 20; i++) {
final int j = i;
executorService.submit(new Runnable() {
@Override
public void run() {
Map<String, String> dtos1 = new HashMap<>();
for (int i = 0; i < 100; i++) {
dtos1.put("testKey" + j + ":" + i, "testValue" + i);
}
long st = System.currentTimeMillis();
redisClusterHelper.sets(dtos1, 1000L);
long ed = System.currentTimeMillis();
System.out.println("耗时:" + j + ":" + (ed - st));
}
});
}
Thread.sleep(5000L);
for (int i = 20; i < 40; i++) {
final int j = i;
executorService.submit(new Runnable() {
@Override
public void run() {
Map<String, String> dtos1 = new HashMap<>();
for (int i = 0; i < 100; i++) {
dtos1.put("testKey" + j + ":" + i, "testValue" + i);
}
long st = System.currentTimeMillis();
redisClusterHelper.sets(dtos1, 1000L);
long ed = System.currentTimeMillis();
System.out.println("耗时:" + j + ":" + (ed - st));
}
});
}
Thread.sleep(5000L);
}
@Test
public void testGet() {
Person p = new Person();
p.setDeptName("hhhhhh");
p.setOperDate(new Date());
redisClusterHelper.set("zhangsan111", p);
Person o = redisClusterHelper.get("zhangsan111", Person.class);
Assert.assertEquals("hhhhhh", o.getDeptName());
}
@Test
public void testGet2() throws InterruptedException {
//批量设置测试
Map<String, String> dtos2 = new HashMap<>();
for (int i = 0; i < 100; i++) {
dtos2.put("testKey2:" + i, "testValue2:" + i);
}
long st = System.currentTimeMillis();
redisClusterHelper.pipelineSets(dtos2, 1000L);
long ed = System.currentTimeMillis();
System.out.println("耗时2:" + (ed - st));
st = System.currentTimeMillis();
List<String> vList = redisClusterHelper
.pipelineGets(new ArrayList<String>(dtos2.keySet()), String.class);
ed = System.currentTimeMillis();
System.out.println("耗时2:" + (ed - st));
System.out.println(vList);
}
@Test
public void testH() {
Person p = new Person();
p.setDeptName("hhhhhh");
p.setOperDate(new Date());
redisClusterHelper.hSet("hsetKey", "f1", p);
Person person = redisClusterHelper.hGet("hsetKey", "f1", Person.class);
Assert.assertEquals("hhhhhh", p.getDeptName());
}
@Test
public void testAnn() {
Person person = personService.get(1L);
System.out.println(person);
person = personService.get(1L);
System.out.println(person);
}
@Test
public void testIncr() {
redisClusterHelper.set("incyKey", 1);
System.out.println(redisClusterHelper.get("incyKey", Long.class));
redisClusterHelper.incr("incyKey", 1);
System.out.println(redisClusterHelper.get("incyKey", Long.class));
redisClusterHelper.incr("incyKey", 10);
System.out.println(redisClusterHelper.get("incyKey", Long.class));
}
@Test
public void testTtl() throws InterruptedException {
redisClusterHelper.set("incyKey", 1, 100);
System.out.println(redisClusterHelper.ttl("incyKey", TimeUnit.MILLISECONDS));
Thread.sleep(444L);
System.out.println(redisClusterHelper.ttl("incyKey", TimeUnit.MILLISECONDS));
Thread.sleep(444L);
System.out.println(redisClusterHelper.exists("incyKey"));
System.out.println(redisClusterHelper.exists("incyKey111"));
}
@Test
public void testHmset() {
Map<String, Integer> vv = new HashMap<>();
vv.put("one", 1);
vv.put("two", 2);
redisClusterHelper.hMSet("hmsetNumKey", vv);
Map<String, Integer> ret = redisClusterHelper
.hMGet("hmsetNumKey", new String[]{"one", "two"}, Integer.class);
System.out.println(ret);
ret = redisClusterHelper
.hGetAll("hmsetNumKey", String.class, Integer.class);
System.out.println(ret);
}
}
......@@ -7,199 +7,53 @@ import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;
import javax.annotation.Resource;
import java.util.*;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import javax.xml.crypto.Data;
import java.util.Date;
/**
* Created by Administrator on 2018/1/15.
*/
//@ContextConfiguration({"classpath*:spring-jedis-test.xml"})
@ContextConfiguration({"classpath*:spring-lettuce-test.xml"})
public class RedisClusterTest extends AbstractJUnit4SpringContextTests {
//@ContextConfiguration({"classpath*:spring-jedis-alone-config-sample.xml"})
@ContextConfiguration({"classpath*:spring-lettuce-alone-config-sample.xml"})
public class RedisTest extends AbstractJUnit4SpringContextTests {
@Resource
private RedisHelper redisClusterHelper;
@Resource
private PersonService personService;
private RedisHelper redisHelper;
@Test
public void testSet() throws InterruptedException {
redisClusterHelper.set("aaa", "bbb");
redisHelper.set("aaa", "bbb");
String ret = redisClusterHelper.get("aaa", String.class);
String ret = redisHelper.get("aaa", String.class);
Assert.assertEquals("bbb", ret);
redisClusterHelper.set("aaa", "ccc", 2l);
ret = redisClusterHelper.get("aaa", String.class);
redisHelper.set("aaa", "ccc", 2l);
ret = redisHelper.get("aaa", String.class);
Assert.assertEquals("ccc", ret);
Thread.sleep(3000l);
ret = redisClusterHelper.get("aaa", String.class);
ret = redisHelper.get("aaa", String.class);
Assert.assertNull(ret);
//对实体对象操作
Person person=new Person();
Date date=new Date();
person.setOperDate(date);
person.setLoginName("李四");
redisHelper.set("person03",person);
Assert.assertEquals("李四",redisHelper.get("person03",Person.class).getLoginName());
Assert.assertEquals(date,person.getOperDate());
}
@Test
public void testSet2() throws InterruptedException {
//批量设置测试
Map<String, String> dtos1 = new HashMap<>();
for (int i = 0; i < 100; i++) {
dtos1.put("testKey" + i, "testValue" + i);
}
long st = System.currentTimeMillis();
redisClusterHelper.sets(dtos1, 1000L);
long ed = System.currentTimeMillis();
System.out.println("耗时:" + (ed - st));
for (int i = 0; i < 100; i++) {
dtos1.put("testKeyi" + i, "testValue" + i);
public void testIncr() {
redisHelper.set("incyKey01", 1);
redisHelper.incr("incyKey01",2);
System.out.println(redisHelper.get("incyKey01", Long.class));
}
st = System.currentTimeMillis();
redisClusterHelper.sets(dtos1, 1000L);
ed = System.currentTimeMillis();
System.out.println("耗时:" + (ed - st));
List<String> list = redisClusterHelper.gets(new ArrayList<String>(dtos1.keySet()), String.class);
System.out.println(list);
Map<String, String> dtos2 = new HashMap<>();
for (int i = 0; i < 1000; i++) {
dtos2.put("testKey2:" + i, "testValue2:" + i);
}
st = System.currentTimeMillis();
redisClusterHelper.pipelineSets(dtos2, 1000L);
ed = System.currentTimeMillis();
System.out.println("耗时2:" + (ed - st));
}
/**
* 测试多线程
* 其他方法使用详见 @com.secoo.mall.redis.RedisClusterTest
*/
@Test
public void testThreads() throws InterruptedException {
ExecutorService executorService = Executors.newCachedThreadPool();
for (int i = 0; i < 20; i++) {
final int j = i;
executorService.submit(new Runnable() {
@Override
public void run() {
Map<String, String> dtos1 = new HashMap<>();
for (int i = 0; i < 100; i++) {
dtos1.put("testKey" + j + ":" + i, "testValue" + i);
}
long st = System.currentTimeMillis();
redisClusterHelper.sets(dtos1, 1000L);
long ed = System.currentTimeMillis();
System.out.println("耗时:" + j + ":" + (ed - st));
}
});
}
Thread.sleep(5000L);
for (int i = 20; i < 40; i++) {
final int j = i;
executorService.submit(new Runnable() {
@Override
public void run() {
Map<String, String> dtos1 = new HashMap<>();
for (int i = 0; i < 100; i++) {
dtos1.put("testKey" + j + ":" + i, "testValue" + i);
}
long st = System.currentTimeMillis();
redisClusterHelper.sets(dtos1, 1000L);
long ed = System.currentTimeMillis();
System.out.println("耗时:" + j + ":" + (ed - st));
}
});
}
Thread.sleep(5000L);
}
@Test
public void testGet() {
Person p = new Person();
p.setDeptName("hhhhhh");
p.setOperDate(new Date());
redisClusterHelper.set("zhangsan111", p);
Person o = redisClusterHelper.get("zhangsan111", Person.class);
Assert.assertEquals("hhhhhh", o.getDeptName());
}
@Test
public void testGet2() throws InterruptedException {
//批量设置测试
Map<String, String> dtos2 = new HashMap<>();
for (int i = 0; i < 100; i++) {
dtos2.put("testKey2:" + i, "testValue2:" + i);
}
long st = System.currentTimeMillis();
redisClusterHelper.pipelineSets(dtos2, 1000L);
long ed = System.currentTimeMillis();
System.out.println("耗时2:" + (ed - st));
st = System.currentTimeMillis();
List<String> vList = redisClusterHelper
.pipelineGets(new ArrayList<String>(dtos2.keySet()), String.class);
ed = System.currentTimeMillis();
System.out.println("耗时2:" + (ed - st));
System.out.println(vList);
}
@Test
public void testH() {
Person p = new Person();
p.setDeptName("hhhhhh");
p.setOperDate(new Date());
redisClusterHelper.hSet("hsetKey", "f1", p);
Person person = redisClusterHelper.hGet("hsetKey", "f1", Person.class);
Assert.assertEquals("hhhhhh", p.getDeptName());
}
@Test
public void testAnn() {
Person person = personService.get(1L);
System.out.println(person);
person = personService.get(1L);
System.out.println(person);
}
@Test
public void testIncr() {
redisClusterHelper.set("incyKey", 1);
System.out.println(redisClusterHelper.get("incyKey", Long.class));
redisClusterHelper.incr("incyKey", 1);
System.out.println(redisClusterHelper.get("incyKey", Long.class));
redisClusterHelper.incr("incyKey", 10);
System.out.println(redisClusterHelper.get("incyKey", Long.class));
}
@Test
public void testTtl() throws InterruptedException {
redisClusterHelper.set("incyKey", 1, 100);
System.out.println(redisClusterHelper.ttl("incyKey", TimeUnit.MILLISECONDS));
Thread.sleep(444L);
System.out.println(redisClusterHelper.ttl("incyKey", TimeUnit.MILLISECONDS));
Thread.sleep(444L);
System.out.println(redisClusterHelper.exists("incyKey"));
System.out.println(redisClusterHelper.exists("incyKey111"));
}
@Test
public void testHmset() {
Map<String, Integer> vv = new HashMap<>();
vv.put("one", 1);
vv.put("two", 2);
redisClusterHelper.hMSet("hmsetNumKey", vv);
Map<String, Integer> ret = redisClusterHelper
.hMGet("hmsetNumKey", new String[]{"one", "two"}, Integer.class);
System.out.println(ret);
ret = redisClusterHelper
.hGetAll("hmsetNumKey", String.class, Integer.class);
System.out.println(ret);
}
}
redis.maxRedirects=5
redis.host1=10.185.240.142
redis.host1=dev01-rediscluster01.secoolocal.com
redis.port1=7001
redis.host2=10.185.240.142
redis.host2=dev01-rediscluster01.secoolocal.com
redis.port2=7002
redis.host3=10.185.240.142
redis.port3=7003
redis.host4=10.185.240.142
redis.port4=7004
redis.host5=10.185.240.142
redis.port5=7005
redis.host6=10.185.240.142
redis.port6=7006
redis.host3=dev01-rediscluster02.secoolocal.com
redis.port3=7001
redis.host4=dev01-rediscluster02.secoolocal.com
redis.port4=7002
redis.host5=dev01-rediscluster03.secoolocal.com
redis.port5=7001
redis.host6=dev01-rediscluster03.secoolocal.com
redis.port6=7002
# connnet pool
redis.maxIdle=50
redis.maxTotal=100
......
<?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:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-txs-4.3.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd">
<context:property-placeholder ignore-unresolvable="true"
location="classpath:redis-cluster.properties"/>
<context:component-scan base-package="com.secooframework"></context:component-scan>
<bean id="redisClusterConfiguration"
class="org.springframework.data.redis.connection.RedisClusterConfiguration">
<property name="maxRedirects" value="${redis.maxRedirects}"></property>
<property name="clusterNodes">
<set>
<bean class="org.springframework.data.redis.connection.RedisClusterNode">
<constructor-arg name="host" value="${redis.host1}"></constructor-arg>
<constructor-arg name="port" value="${redis.port1}"></constructor-arg>
</bean>
<bean class="org.springframework.data.redis.connection.RedisClusterNode">
<constructor-arg name="host" value="${redis.host2}"></constructor-arg>
<constructor-arg name="port" value="${redis.port2}"></constructor-arg>
</bean>
<bean class="org.springframework.data.redis.connection.RedisClusterNode">
<constructor-arg name="host" value="${redis.host3}"></constructor-arg>
<constructor-arg name="port" value="${redis.port3}"></constructor-arg>
</bean>
<bean class="org.springframework.data.redis.connection.RedisClusterNode">
<constructor-arg name="host" value="${redis.host4}"></constructor-arg>
<constructor-arg name="port" value="${redis.port4}"></constructor-arg>
</bean>
<bean class="org.springframework.data.redis.connection.RedisClusterNode">
<constructor-arg name="host" value="${redis.host5}"></constructor-arg>
<constructor-arg name="port" value="${redis.port5}"></constructor-arg>
</bean>
<bean class="org.springframework.data.redis.connection.RedisClusterNode">
<constructor-arg name="host" value="${redis.host6}"></constructor-arg>
<constructor-arg name="port" value="${redis.port6}"></constructor-arg>
</bean>
</set>
</property>
<context:component-scan base-package="com.secoo.mall"></context:component-scan>
<!--单主机配置-->
<bean id="redisAloneConfiguration" class="org.springframework.data.redis.connection.RedisStandaloneConfiguration">
<property name="hostName" value="127.0.0.1"/>
<property name="port" value="6379"/>
</bean>
<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxIdle" value="${redis.maxIdle}"/>
<property name="maxTotal" value="${redis.maxTotal}"/>
......@@ -60,14 +30,19 @@
<bean id="jeidsConnectionFactory"
class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
<constructor-arg ref="redisClusterConfiguration"/>
<constructor-arg ref="redisAloneConfiguration"/>
<constructor-arg ref="jedisPoolConfig"/>
</bean>
<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
<property name="connectionFactory" ref="jeidsConnectionFactory"/>
<!-- 序列化方式 key/hashKey采用StringRedisSerializer -->
<property name="keySerializer">
</bean>
<bean id="redisHelper" class="com.secoo.mall.redis.helper.RedisHelper">
<constructor-arg name="redisTemplate" ref="redisTemplate"/>
<!-- 序列化方式 key/hashKey采用StringRedisSerializer-->
<!--<property name="keySerializer">
<bean
class="org.springframework.data.redis.serializer.StringRedisSerializer"/>
</property>
......@@ -82,9 +57,7 @@
<property name="hashValueSerializer">
<bean
class="org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer"/>
</property>
</property>-->
</bean>
<bean id="secooRedisTemplate" class="com.secooframework.redis.SecooRedisTemplate"/>
</beans>
......@@ -17,7 +17,7 @@
<context:property-placeholder ignore-unresolvable="true"
location="classpath:redis-cluster.properties"/>
<context:component-scan base-package="com.secooframework"></context:component-scan>
<context:component-scan base-package="com.secoo.mall"></context:component-scan>
<bean id="redisClusterConfiguration"
class="org.springframework.data.redis.connection.RedisClusterConfiguration">
......@@ -64,10 +64,14 @@
<constructor-arg ref="jedisPoolConfig"/>
</bean>
<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
<bean id="redisClusterTemplate" class="org.springframework.data.redis.core.RedisTemplate">
<property name="connectionFactory" ref="jeidsConnectionFactory"/>
</bean>
<bean id="secooRedisTemplate" class="com.secoo.mall.redis.helper.RedisHelper">
<constructor-arg name="redisTemplate" ref="redisClusterTemplate"/>
<!-- 序列化方式 key/hashKey采用StringRedisSerializer -->
<property name="keySerializer">
<!-- <property name="keySerializer">
<bean
class="org.springframework.data.redis.serializer.StringRedisSerializer"/>
</property>
......@@ -82,9 +86,7 @@
<property name="hashValueSerializer">
<bean
class="org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer"/>
</property>
</property>-->
</bean>
<bean id="secooRedisTemplate" class="com.secooframework.redis.SecooRedisTemplate"/>
</beans>
<?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:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-txs-4.3.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/context
......@@ -21,65 +19,23 @@
<aop:config proxy-target-class="true"></aop:config>
<aop:aspectj-autoproxy/>
<bean id="redisClusterConfiguration"
class="org.springframework.data.redis.connection.RedisClusterConfiguration">
<property name="maxRedirects" value="${redis.maxRedirects}"></property>
<property name="clusterNodes">
<set>
<bean class="org.springframework.data.redis.connection.RedisClusterNode">
<constructor-arg name="host" value="${redis.host1}"></constructor-arg>
<constructor-arg name="port" value="${redis.port1}"></constructor-arg>
</bean>
<bean class="org.springframework.data.redis.connection.RedisClusterNode">
<constructor-arg name="host" value="${redis.host2}"></constructor-arg>
<constructor-arg name="port" value="${redis.port2}"></constructor-arg>
</bean>
<bean class="org.springframework.data.redis.connection.RedisClusterNode">
<constructor-arg name="host" value="${redis.host3}"></constructor-arg>
<constructor-arg name="port" value="${redis.port3}"></constructor-arg>
</bean>
<bean class="org.springframework.data.redis.connection.RedisClusterNode">
<constructor-arg name="host" value="${redis.host4}"></constructor-arg>
<constructor-arg name="port" value="${redis.port4}"></constructor-arg>
</bean>
<bean class="org.springframework.data.redis.connection.RedisClusterNode">
<constructor-arg name="host" value="${redis.host5}"></constructor-arg>
<constructor-arg name="port" value="${redis.port5}"></constructor-arg>
<!--单主机配置-->
<bean id="redisAloneConfiguration" class="org.springframework.data.redis.connection.RedisStandaloneConfiguration">
<property name="hostName" value="127.0.0.1"/>
<property name="port" value="6379"/>
</bean>
<bean class="org.springframework.data.redis.connection.RedisClusterNode">
<constructor-arg name="host" value="${redis.host6}"></constructor-arg>
<constructor-arg name="port" value="${redis.port6}"></constructor-arg>
</bean>
</set>
</property>
</bean>
<!--<bean id="clientResources" class="com.lambdaworks.redis.resource.DefaultClientResources"-->
<!--factory-method="create">-->
<!--</bean>-->
<bean id="lettuceConnectionFactory"
class="org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory">
<constructor-arg ref="redisClusterConfiguration"/>
<!--<property name="clientResources" ref="clientResources"/>-->
</bean>
<constructor-arg ref="redisAloneConfiguration"/>
<!--可以注入一个mapper,默认的序列化会序列化类名-->
<!--ObjectMapper mapper = new ObjectMapper();-->
<!--mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);-->
<!--//日期格式-->
<!--mapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));-->
<!--//null不序列化 减小报文大小-->
<!--mapper.setSerializationInclusion(Include.NON_NULL);-->
</bean>
<bean id="redisCluserTemplate" class="org.springframework.data.redis.core.RedisTemplate">
<bean id="rediredisAloneConfigurationTemplate" class="org.springframework.data.redis.core.RedisTemplate">
<property name="connectionFactory" ref="lettuceConnectionFactory"/>
<!-- 序列化方式 key/hashKey采用StringRedisSerializer -->
<property name="keySerializer">
<bean
class="org.springframework.data.redis.serializer.StringRedisSerializer"/>
<bean class="org.springframework.data.redis.serializer.StringRedisSerializer"/>
</property>
<property name="hashKeySerializer">
<bean
......@@ -93,10 +49,27 @@
<bean
class="org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer"/>
</property>
</bean>
<bean id="redisClusterHelper" class="com.secoo.mall.redis.helper.RedisHelper">
<constructor-arg name="redisTemplate" ref="redisCluserTemplate"/>
<bean id="redisHelper" class="com.secoo.mall.redis.helper.RedisHelper">
<constructor-arg name="redisTemplate" ref="rediredisAloneConfigurationTemplate"/>
<!-- 序列化方式 key/hashKey采用StringRedisSerializer ,不指定可以使用默认的-->
<!-- <property name="keySerializer">
<bean class="org.springframework.data.redis.serializer.StringRedisSerializer"/>
</property>
<property name="hashKeySerializer">
<bean
class="org.springframework.data.redis.serializer.StringRedisSerializer"/>
</property>
<property name="valueSerializer">
<bean
class="org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer"/>
</property>
<property name="hashValueSerializer">
<bean
class="org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer"/>
</property>-->
</bean>
......
<?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:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-txs-4.3.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd">
<context:property-placeholder ignore-unresolvable="true"
location="classpath:redis-cluster.properties"/>
<context:component-scan base-package="com.secoo.mall"></context:component-scan>
<aop:config proxy-target-class="true"></aop:config>
<aop:aspectj-autoproxy/>
<bean id="redisClusterConfiguration"
class="org.springframework.data.redis.connection.RedisClusterConfiguration">
<property name="maxRedirects" value="${redis.maxRedirects}"></property>
<property name="clusterNodes">
<set>
<bean class="org.springframework.data.redis.connection.RedisClusterNode">
<constructor-arg name="host" value="${redis.host1}"></constructor-arg>
<constructor-arg name="port" value="${redis.port1}"></constructor-arg>
</bean>
<bean class="org.springframework.data.redis.connection.RedisClusterNode">
<constructor-arg name="host" value="${redis.host2}"></constructor-arg>
<constructor-arg name="port" value="${redis.port2}"></constructor-arg>
</bean>
<bean class="org.springframework.data.redis.connection.RedisClusterNode">
<constructor-arg name="host" value="${redis.host3}"></constructor-arg>
<constructor-arg name="port" value="${redis.port3}"></constructor-arg>
</bean>
<bean class="org.springframework.data.redis.connection.RedisClusterNode">
<constructor-arg name="host" value="${redis.host4}"></constructor-arg>
<constructor-arg name="port" value="${redis.port4}"></constructor-arg>
</bean>
<bean class="org.springframework.data.redis.connection.RedisClusterNode">
<constructor-arg name="host" value="${redis.host5}"></constructor-arg>
<constructor-arg name="port" value="${redis.port5}"></constructor-arg>
</bean>
<bean class="org.springframework.data.redis.connection.RedisClusterNode">
<constructor-arg name="host" value="${redis.host6}"></constructor-arg>
<constructor-arg name="port" value="${redis.port6}"></constructor-arg>
</bean>
</set>
</property>
</bean>
<!--<bean id="clientResources" class="com.lambdaworks.redis.resource.DefaultClientResources"-->
<!--factory-method="create">-->
<!--</bean>-->
<bean id="lettuceConnectionFactory"
class="org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory">
<constructor-arg ref="redisClusterConfiguration"/>
<!--<property name="clientResources" ref="clientResources"/>-->
</bean>
<!--可以注入一个mapper,默认的序列化会序列化类名-->
<!--ObjectMapper mapper = new ObjectMapper();-->
<!--mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);-->
<!--//日期格式-->
<!--mapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));-->
<!--//null不序列化 减小报文大小-->
<!--mapper.setSerializationInclusion(Include.NON_NULL);-->
<bean id="redisCluserTemplate" class="org.springframework.data.redis.core.RedisTemplate">
<property name="connectionFactory" ref="lettuceConnectionFactory"/>
</bean>
<bean id="redisClusterHelper" class="com.secoo.mall.redis.helper.RedisHelper">
<constructor-arg name="redisTemplate" ref="redisCluserTemplate"/>
<!-- 序列化方式 key/hashKey采用StringRedisSerializer ,也可以使用默认的-->
<!--<property name="keySerializer">
<bean
class="org.springframework.data.redis.serializer.StringRedisSerializer"/>
</property>
<property name="hashKeySerializer">
<bean
class="org.springframework.data.redis.serializer.StringRedisSerializer"/>
</property>
<property name="valueSerializer">
<bean
class="org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer"/>
</property>
<property name="hashValueSerializer">
<bean
class="org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer"/>
</property>-->
</bean>
</beans>
......@@ -10,6 +10,20 @@
<modelVersion>4.0.0</modelVersion>
<artifactId>matrix-sample-redis-starter</artifactId>
<dependencies>
<dependency>
<groupId>com.secoo.mall</groupId>
<artifactId>matrix-sample-redis-biz</artifactId>
</dependency>
<dependency>
<groupId>com.secoo.mall</groupId>
<artifactId>matrix-datahelper-redis-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
</dependency>
</dependencies>
</project>
\ No newline at end of file
package com.secoo.mall.redis;
public class RedisSampleApplication {
import com.secoo.mall.redis.helper.RedisHelper;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import javax.annotation.Resource;
import java.util.Date;
@SpringBootApplication
public class RedisSampleApplication implements ApplicationRunner {
@Resource
private RedisHelper redisHelper;
@Resource
private RedisHelper productRedisHelper;
public static void main(String[] args) {
new SpringApplicationBuilder(RedisSampleApplication.class).run(args);
}
@Override
public void run(ApplicationArguments args) throws Exception {
redisHelper.set("key01","word");
String key01 = redisHelper.get("key01",String.class);
Person person=new Person();
person.setId(2L);
// person.setDeptName("技术部");
person.setLoginName("zhangsan");
person.setOperDate(new Date());
redisHelper.set("person05",person);
//1.通过测试,Fastjson,对象缺少属性后支持序列化
System.out.println("person05::"+redisHelper.get("person05",Person.class).getLoginName());
productRedisHelper.set("p:01","帽子");
System.out.println("p:01=" +productRedisHelper.get("p:01",String.class));
}
}
package com.secoo.mall.redis.config;
import com.secoo.mall.redis.helper.RedisHelper;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
/**
* 多数据源配置
*/
@Configuration
@ConfigurationProperties(prefix = "redis.custom")
@Data
public class MuliRedisConfig {
public String host;
public Integer port;
/**
* 仅仅提供简单配置demo
*
* @return
*/
@Bean
public LettuceConnectionFactory productConnectionFactory() {
LettuceConnectionFactory connectionFactory = new LettuceConnectionFactory(host, port);
return connectionFactory;
}
@Bean
public RedisTemplate productRedisTemplate(LettuceConnectionFactory productConnectionFactory){
RedisTemplate redisTemplate = new RedisTemplate();
redisTemplate.setConnectionFactory(productConnectionFactory);
return redisTemplate;
}
@Bean("productRedisHelper")
public RedisHelper productRedisHelper(RedisTemplate productRedisTemplate) {
return new RedisHelper(productRedisTemplate);
}
}
spring:
redis:
cluster:
nodes: dev01-rediscluster01.secoolocal.com:7001,dev01-rediscluster01.secoolocal.com:7002,dev01-rediscluster02.secoolocal.com:7001,dev01-rediscluster02.secoolocal.com:7002,dev01-rediscluster03.secoolocal.com:7001,dev01-rediscluster03.secoolocal.com:7002
timeout: 10000
# host: localhost
redis:
custom:
host: localhost
port: 6379
\ No newline at end of file
......@@ -14,7 +14,38 @@
<modules>
<module>matrix-sample-redis-starter</module>
<module>matrix-sample-redis-biz</module>
<module>matrix-sample-redis-spring</module>
</modules>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.secoo.mall</groupId>
<artifactId>matrix-sample-redis-biz</artifactId>
<version>1.3.2.RELEASE</version>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>3.2.0</version>
</dependency>
<!-- 推荐使用 lettuce -->
<dependency>
<groupId>io.lettuce</groupId>
<artifactId>lettuce-core</artifactId>
<version>5.2.2.RELEASE</version>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>com.secoo.mall</groupId>
<artifactId>common-util</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
</dependencies>
</project>
\ No newline at end of file
......@@ -20,6 +20,16 @@
<artifactId>matrix-sample-rocketmq-biz</artifactId>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>biz.paluch.redis</groupId>
<artifactId>lettuce</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment