分散キャッシュ技術redis学習シリーズ(九):Redis主従の読み書き分離
31446 ワード
前言みんなは仕事の中でこのような需要に出会うかもしれません.つまり、Redisの読み書きは分離して、圧力の分散化を目的としています.以下では、AWSのELBによる読み書き分離について説明します.
リファレンスライブラリファイルの実装
方式1、切面を借りる
JedisPoolSelector
このような目的は、主か従かを区別するために、読み取りと書き込みのために異なる注釈を構成することです.
JedisPoolAspectのような目的は,プライマリとセカンダリの注釈に対して動的リンクプールのプロビジョニング,すなわちプライマリはプライマリリンクプールを用い,セカンダリは接続プールから用いる.
DynamicJedisPoolHolderのような目的は、現在使用されているJedisPool、すなわち、上記のクラスが割り当てられた結果を保存することです.
RedisUtilsのような目的は、Redisの具体的な呼び出しであり、マスターを使用するか、スレーブを使用するかを含む.
spring-datasource.xml
Test
方式二、依存注入
方式と同様ですが、主を具体的に使用するプールは従のプールであるかを書く必要があります.構想は以下の通りです.注釈を放棄する方法で、主と従の2つのリンクプールを直接具体的な実装クラスに注入します.
RedisUtils
spring-datasource.xml
転載:http://www.jb51.net/article/95997.htm
リファレンスライブラリファイルの実装
<dependency>
<groupId>redis.clientsgroupId>
<artifactId>jedisartifactId>
<version>2.6.2version>
dependency>
方式1、切面を借りる
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();
}
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.
*/
@Aspect
public 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;
}
}
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 holder = new ThreadLocal();
public static void putJedisPool(JedisPool jedisPool) {
holder.set(jedisPool);
}
public static JedisPool getJedisPool() {
return holder.get();
}
}
RedisUtilsのような目的は、Redisの具体的な呼び出しであり、マスターを使用するか、スレーブを使用するかを含む.
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;
/**
* Created by keysilence on 16/10/26.
*/
public 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;
}
}
spring-datasource.xml
<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>
Test
package com.silence.spring.redis.readwriteseparation;
import com.silence.spring.redis.readwriteseparation.util.RedisUtils;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* Created by keysilence on 16/10/26.
*/
public 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"));
}
}
方式二、依存注入
方式と同様ですが、主を具体的に使用するプールは従のプールであるかを書く必要があります.構想は以下の通りです.注釈を放棄する方法で、主と従の2つのリンクプールを直接具体的な実装クラスに注入します.
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;
}
}
spring-datasource.xml
<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>
転載:http://www.jb51.net/article/95997.htm