anyuan2002.com - vwin网

查找: 您的方位主页 > 网络频道 > 阅览资讯:怎样样经过Redis主从完成读写别离?

怎样样经过Redis主从完成读写别离?

2016-11-23 07:38:25 来历:www.anyuan2002.com 【

怎样样经过Redis主从完成读写别离?

前语

咱们在工作中可能会遇到这样的需求,即Redis读写别离,意图是为了压力分散化。下面我将为咱们介绍凭借AWS的ELB完成读写别离,以写主读从为例。

完成

引证库文件

vwin娱乐场
 <!-- redis客户端 -->
 <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();

}

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<JedisPool> holder = new ThreadLocal<JedisPool>();

 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

<?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"/>
  <!-- 当池中链接耗尽,调用者最大堵塞时刻,超出此时刻将跑出反常。(单位:毫秒;默以为-1,表明永不超时) -->
  <property name="maxWaitMillis" value="1000"/>
  <!-- 参阅:http://biasedbit.com/redis-jedispool-configuration/ -->
  <!-- 调用者获取链接时,是否检测当时链接有效性。无效则从链接池中移除,并测验持续获龋(默以为false) -->
  <property name="testOnBorrow" value="true" />
  <!-- 向链接池中偿还链接时,是否检测链接有效性。(默以为false) -->
  <property name="testOnReturn" value="true" />
  <!-- 调用者获取链接时,是否检测闲暇超时。假如超时,则会被移除(默以为false) -->
  <property name="testWhileIdle" value="true" />
  <!-- 闲暇链接检测线程一次运转检测多少条链接 -->
  <property name="numTestsPerEvictionRun" value="10" />
  <!-- 闲暇链接检测线程检测周期。假如为负值,表明不运转检测线程。(单位:毫秒,默以为-1) -->
  <property name="timeBetweenEvictionRunsMillis" value="60000" />
  <!-- 链接获取方法。行列:false;栈:true -->
  <!--<property name="lifo" value="false" />-->
 </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"/>
  <!-- 此处Host装备成ELB地址 -->
  <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"));
 }

}

方法二,依靠注入

与方法一相似,可是需要写死详细运用主的池仍是从的池,思路如下:
抛弃注解的方法,直接将主和从的两个链接池注入到详细完成类中。

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

<?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"/>
  <!-- 当池中链接耗尽,调用者最大堵塞时刻,超出此时刻将跑出反常。(单位:毫秒;默以为-1,表明永不超时) -->
  <property name="maxWaitMillis" value="1000"/>
  <!-- 参阅:http://biasedbit.com/redis-jedispool-configuration/ -->
  <!-- 调用者获取链接时,是否检测当时链接有效性。无效则从链接池中移除,并测验持续获龋(默以为false) -->
  <property name="testOnBorrow" value="true" />
  <!-- 向链接池中偿还链接时,是否检测链接有效性。(默以为false) -->
  <property name="testOnReturn" value="true" />
  <!-- 调用者获取链接时,是否检测闲暇超时。假如超时,则会被移除(默以为false) -->
  <property name="testWhileIdle" value="true" />
  <!-- 闲暇链接检测线程一次运转检测多少条链接 -->
  <property name="numTestsPerEvictionRun" value="10" />
  <!-- 闲暇链接检测线程检测周期。假如为负值,表明不运转检测线程。(单位:毫秒,默以为-1) -->
  <property name="timeBetweenEvictionRunsMillis" value="60000" />
  <!-- 链接获取方法。行列:false;栈:true -->
  <!--<property name="lifo" value="false" />-->
 </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>

以上便是本文的全部内容,期望对咱们的学习有所协助,也期望咱们多多支撑vwin。


本文地址:http://www.anyuan2002.com/a/question/83979.html
Tags: 完成 Redis 读写
修正:vwin网
关于咱们 | 联络咱们 | 友情链接 | 网站地图 | Sitemap | App | 回来顶部