亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb

首頁 > 數據庫 > Redis > 正文

Redis主從實現讀寫分離

2020-02-17 14:53:27
字體:
來源:轉載
供稿:網友

? ? ? ? Redis主從實現讀寫分離,大家在工作中可能會遇到這樣的需求,即Redis讀寫分離,目的是為了壓力分散化。下面我將為大家介紹借助AWS的ELB實現讀寫分離,以寫主讀從為例。

實現

引用庫文件

  <!-- 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. */@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;  }}

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>

? ? ? ? 以上就是Redis主從實現讀寫分離的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林技術頻道。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表

圖片精選

亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb
91精品美女在线| 欧美性猛交xxxx富婆弯腰| 欧美精品激情blacked18| 欧美日韩国产丝袜另类| 一区二区在线视频播放| 国产经典一区二区| 国产成人在线播放| 国产91精品久久久| 欧美肥老妇视频| 成人有码视频在线播放| 国产精品久久久久久久久久ktv| 国产精品视频中文字幕91| 欧美丰满少妇xxxxx| 伊人青青综合网站| 欧美成人在线免费视频| 激情久久av一区av二区av三区| 黑人巨大精品欧美一区二区三区| 欧美日韩国产中文精品字幕自在自线| 亚洲欧洲在线播放| 国产精品aaaa| 成人免费在线网址| 在线观看国产精品淫| 国产精品美女久久久久av超清| 清纯唯美亚洲激情| 亚洲视频电影图片偷拍一区| 日韩动漫免费观看电视剧高清| 深夜福利一区二区| 久久久精品欧美| 欧美成人精品在线播放| 欧美成人免费全部观看天天性色| 欧美一性一乱一交一视频| 中文字幕欧美精品日韩中文字幕| 成人亚洲欧美一区二区三区| 欧美人与性动交a欧美精品| 欧美在线视频免费| 亚洲国产精品va在线看黑人| 亚洲精品国产精品自产a区红杏吧| 欧美一区二区三区四区在线| 色婷婷久久一区二区| 日本午夜精品理论片a级appf发布| 国产精品久久久久久久久久久不卡| 亚洲老司机av| 综合激情国产一区| 国产精品视频网| 91社区国产高清| 亚洲美女性生活视频| 久久成人免费视频| 久热精品视频在线| 2021久久精品国产99国产精品| 亚洲天堂网站在线观看视频| 91沈先生作品| 国产91精品视频在线观看| 色狠狠av一区二区三区香蕉蜜桃| 国产欧美日韩精品专区| 欧美疯狂xxxx大交乱88av| 午夜精品理论片| 亚洲成人av在线播放| 91久久国产精品91久久性色| 亚洲黄色免费三级| 亚洲欧美日韩视频一区| 在线不卡国产精品| 亚洲韩国日本中文字幕| 欧美激情极品视频| 精品香蕉一区二区三区| 亚洲毛片一区二区| 在线观看欧美日韩国产| 亚洲国产精品yw在线观看| 中文字幕亚洲欧美日韩高清| 国产成人一区二区三区电影| 欧美成人激情视频| 欧美视频免费在线观看| 欧美一级视频在线观看| 亚洲精品一区二区网址| 国产一区二中文字幕在线看| 一本大道香蕉久在线播放29| 日韩国产激情在线| 久久综合久中文字幕青草| 久久免费视频在线观看| 亚洲已满18点击进入在线看片| 成人精品一区二区三区电影免费| 91九色国产视频| 亚洲free性xxxx护士hd| 亚洲国产成人久久综合一区| 亚洲自拍另类欧美丝袜| 日韩av第一页| 日本精品性网站在线观看| 成人观看高清在线观看免费| 国产精品久久999| 欧美视频在线免费看| 欧美中文字幕在线视频| 国产伊人精品在线| 国产主播欧美精品| 欧美日韩精品国产| 久久人人爽人人爽人人片亚洲| 亚洲欧美中文字幕在线一区| 成人在线激情视频| 亚洲国产中文字幕久久网| 久青草国产97香蕉在线视频| 久久亚洲成人精品| 一区二区三区国产视频| 日韩精品中文字幕在线播放| 性欧美xxxx| 亚洲精品99999| 欧美日韩亚洲视频| 国产精品jizz在线观看麻豆| 亚洲天堂免费在线| 亚洲国产精品成人av| 丝袜亚洲另类欧美重口| 91久久综合亚洲鲁鲁五月天| 亚洲国产欧美一区二区丝袜黑人| 国产精品69精品一区二区三区| 在线看欧美日韩| 欧美高清视频在线观看| 国产做受高潮69| 精品久久久久久| 日日狠狠久久偷偷四色综合免费| 中文字幕不卡在线视频极品| 欧美亚州一区二区三区| 成人免费视频97| 欧美激情区在线播放| 国产一区二区三区在线观看视频| 国产精品欧美亚洲777777| 国产人妖伪娘一区91| 国产91|九色| 欧美日韩在线视频一区| 2025国产精品视频| 日本电影亚洲天堂| 日本a级片电影一区二区| 欧美激情中文字幕乱码免费| 亚洲成av人片在线观看香蕉| 亚洲天堂第二页| 欧美成人精品在线| 欧美大片欧美激情性色a∨久久| 国产精品专区第二| 日韩中文字幕在线播放| 欧美激情奇米色| 国产成人啪精品视频免费网| 亚洲国产精品美女| 国产午夜一区二区| 一区二区欧美日韩视频| 5278欧美一区二区三区| 国产精品精品视频| 一区二区欧美久久| 精品国产一区二区三区久久久| 亚洲第一色中文字幕| 日韩欧美在线视频| 欧美一区二区三区免费观看| 亚洲第一天堂无码专区| 一本色道久久88综合日韩精品| 亚洲国产精品va在线看黑人动漫| 国产成人精品av| 日韩视频免费大全中文字幕| 岛国av一区二区在线在线观看| 另类色图亚洲色图| 中文字幕亚洲天堂| 日韩美女视频免费看| 国产精品91在线观看| 日韩人体视频一二区| 亚洲乱亚洲乱妇无码| 美乳少妇欧美精品| 亚洲高清免费观看高清完整版| 51ⅴ精品国产91久久久久久| 亚洲欧美日韩一区二区在线| 欧美精品一区在线播放|