前言
大家在工作中可能会遇到这样的需求,即Redis读写分离,目的是为了压力分散化。下面我将为大家介绍借助AWS的ELB实现读写分离,以写主读从为例。
实现
引用库文件
<dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>2.6.2</version></dependency>
方式一,借助切面
JedisPoolSelector
此类的目的是为读和写分别配置不同的注解,用来区分是主还是从。
package com.silence.spring.redis.readwriteseparation;import java.lang.annotation.ElementType;import java.lang.annotation.Retention;import java.lang.annotation.RetentionPolicy;import java.lang.annotation.Target;/** * Created by keysilence on 16/10/26. */@Retention(RetentionPolicy.RUNTIME)@Target(ElementType.METHOD)public @interface JedisPoolSelector { String value();}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
JedisPoolAspect
此类的目的是针对主和从的注解,进行动态链接池调配,即主的使用主链接池,从的使用从连接池。
package com.silence.spring.redis.readwriteseparation;import org.aspectj.lang.JoinPoint;import org.aspectj.lang.annotation.Aspect;import org.aspectj.lang.annotation.Before;import org.aspectj.lang.annotation.Pointcut;import org.aspectj.lang.reflect.MethodSignature;import org.springframework.beans.BeansException;import org.springframework.context.ApplicationContext;import org.springframework.context.ApplicationContextAware;import redis.clients.jedis.JedisPool;import javax.annotation.PostConstruct;import java.lang.reflect.Method;import java.util.Date;/** * Created by keysilence on 16/10/26. */@Aspectpublic class JedisPoolAspect implements ApplicationContextAware { private ApplicationContext ctx; @PostConstruct public void init() { System.out.println("jedis pool aspectj started @" + new Date()); } @Pointcut("execution(* com.silence.spring.redis.readwriteseparation.util.*.*(..))") private void allMethod() { } @Before("allMethod()") public void before(JoinPoint point) { Object target = point.getTarget(); String method = point.getSignature().getName(); Class classz = target.getClass(); Class<?>[] parameterTypes = ((MethodSignature) point.getSignature()) .getMethod().getParameterTypes(); try { Method m = classz.getMethod(method, parameterTypes); if (m != null && m.isAnnotationPresent(JedisPoolSelector.class)) { JedisPoolSelector data = m .getAnnotation(JedisPoolSelector.class); JedisPool jedisPool = (JedisPool) ctx.getBean(data.value()); DynamicJedisPoolHolder.putJedisPool(jedisPool); } } catch (Exception e) { e.printStackTrace(); } } public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { this.ctx = applicationContext; }}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
DynamicJedisPoolHolder
此类目的是存储当前使用的JedisPool,即上面类赋值后的结果保存。
package com.silence.spring.redis.readwriteseparation;import redis.clients.jedis.JedisPool;/** * Created by keysilence on 16/10/26. */public class DynamicJedisPoolHolder { public static final ThreadLocal<JedisPool> holder = new ThreadLocal<JedisPool>(); public static void putJedisPool(JedisPool jedisPool) { holder.set(jedisPool); } public static JedisPool getJedisPool() { return holder.get(); }}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
RedisUtils
此类目的是对Redis具体的调用,里面包含使用主还是从的方式调用。
package com.silence.spring.redis.readwriteseparation.utilimport com.silence.spring.redis.readwriteseparation.DynamicJedisPoolHolderimport com.silence.spring.redis.readwriteseparation.JedisPoolSelectorimport org.slf4j.Loggerimport org.slf4j.LoggerFactorypublic class RedisUtils { private static Logger logger = LoggerFactory.getLogger(RedisUtils.class) @JedisPoolSelector("master") public String setString(final String key, final String value) { String ret = DynamicJedisPoolHolder.getJedisPool().getResource().set(key, value) System.out.println("key:" + key + ",value:" + value + ",ret:" + ret) return ret } @JedisPoolSelector("slave") public String get(final String key) { String ret = DynamicJedisPoolHolder.getJedisPool().getResource().get(key) System.out.println("key:" + key + ",ret:" + ret) return ret }}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
spring-datasource.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:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig"> <property name="maxTotal" value="100"/> <property name="maxIdle" value="50"/> <property name="minIdle" value="20"/> <property name="maxWaitMillis" value="1000"/> <property name="testOnBorrow" value="true" /> <property name="testOnReturn" value="true" /> <property name="testWhileIdle" value="true" /> <property name="numTestsPerEvictionRun" value="10" /> <property name="timeBetweenEvictionRunsMillis" value="60000" /> </bean> <bean id="master" class="redis.clients.jedis.JedisPool"> <constructor-arg index="0" ref="poolConfig"/> <constructor-arg index="1" value="192.168.100.110" type="java.lang.String"/> <constructor-arg index="2" value="6379" type="int"/> </bean> <bean id="slave" class="redis.clients.jedis.JedisPool"> <constructor-arg index="0" ref="poolConfig"/> <constructor-arg index="1" value="192.168.100.110" type="java.lang.String"/> <constructor-arg index="2" value="6380" type="int"/> </bean> <bean id="redisUtils" class="com.silence.spring.redis.readwriteseparation.util.RedisUtils"> </bean> <bean id="jedisPoolAspect" class="com.silence.spring.redis.readwriteseparation.JedisPoolAspect" /> <aop:aspectj-autoproxy proxy-target-class="true"/></beans>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
Test
package com.silence.spring.redis.readwriteseparationimport com.silence.spring.redis.readwriteseparation.util.RedisUtilsimport org.springframework.context.ApplicationContextimport org.springframework.context.support.ClassPathXmlApplicationContextpublic class Test { public static void main(String[] args) { ApplicationContext ctx = new ClassPathXmlApplicationContext("spring-datasource.xml") System.out.println(ctx) RedisUtils redisUtils = (RedisUtils) ctx.getBean("redisUtils") redisUtils.setString("aaa", "111") System.out.println(redisUtils.get("aaa")) }}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
方式二,依赖注入
与方式一类似,但是需要写死具体使用主的池还是从的池,思路如下:
放弃注解的方式,直接将主和从的两个链接池注入到具体实现类中。
RedisUtils
package com.silence.spring.redis.readwriteseparation.util;import com.silence.spring.redis.readwriteseparation.DynamicJedisPoolHolder;import com.silence.spring.redis.readwriteseparation.JedisPoolSelector;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import redis.clients.jedis.JedisPool;/** * Created by keysilence on 16/10/26. */public class RedisUtils { private static Logger logger = LoggerFactory.getLogger(RedisUtils.class); private JedisPool masterJedisPool; private JedisPool slaveJedisPool; public void setMasterJedisPool(JedisPool masterJedisPool) { this.masterJedisPool = masterJedisPool; } public void setSlaveJedisPool(JedisPool slaveJedisPool) { this.slaveJedisPool = slaveJedisPool; } public String setString(final String key, final String value) { String ret = masterJedisPool.getResource().set(key, value); System.out.println("key:" + key + ",value:" + value + ",ret:" + ret); return ret; } public String get(final String key) { String ret = slaveJedisPool.getResource().get(key); System.out.println("key:" + key + ",ret:" + ret); return ret; }}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
spring-datasource.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:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig"> <property name="maxTotal" value="100"/> <property name="maxIdle" value="50"/> <property name="minIdle" value="20"/> <property name="maxWaitMillis" value="1000"/> <property name="testOnBorrow" value="true" /> <property name="testOnReturn" value="true" /> <property name="testWhileIdle" value="true" /> <property name="numTestsPerEvictionRun" value="10" /> <property name="timeBetweenEvictionRunsMillis" value="60000" /> </bean> <bean id="masterJedisPool" class="redis.clients.jedis.JedisPool"> <constructor-arg index="0" ref="poolConfig"/> <constructor-arg index="1" value="192.168.100.110" type="java.lang.String"/> <constructor-arg index="2" value="6379" type="int"/> </bean> <bean id="slaveJedisPool" class="redis.clients.jedis.JedisPool"> <constructor-arg index="0" ref="poolConfig"/> <constructor-arg index="1" value="192.168.100.110" type="java.lang.String"/> <constructor-arg index="2" value="6380" type="int"/> </bean> <bean id="redisUtils" class="com.silence.spring.redis.readwriteseparation.util.RedisUtils"> <property name="masterJedisPool" ref="masterJedisPool"/> <property name="slaveJedisPool" ref="slaveJedisPool"/> </bean></beans>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
转载:http://www.jb51.net/article/95997.htm