最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Redis主從實(shí)現(xiàn)讀寫(xiě)分離

 更新時(shí)間:2016年10月28日 16:19:01   作者:KeySilenceM  
這篇文章主要為大家詳細(xì)介紹了Redis主從實(shí)現(xiàn)讀寫(xiě)分離的相關(guān)資料 ,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

前言

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

實(shí)現(xiàn)

引用庫(kù)文件

  <!-- redis客戶端 -->
  <dependency>
   <groupId>redis.clients</groupId>
   <artifactId>jedis</artifactId>
   <version>2.6.2</version>
  </dependency>

方式一,借助切面

JedisPoolSelector

此類的目的是為讀和寫(xiě)分別配置不同的注解,用來(lái)區(qū)分是主還是從。

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

此類的目的是針對(duì)主和從的注解,進(jìn)行動(dòng)態(tài)鏈接池調(diào)配,即主的使用主鏈接池,從的使用從連接池。

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

此類目的是存儲(chǔ)當(dāng)前使用的JedisPool,即上面類賦值后的結(jié)果保存。

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

此類目的是對(duì)Redis具體的調(diào)用,里面包含使用主還是從的方式調(diào)用。

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">
    <!-- 池中最大鏈接數(shù) -->
    <property name="maxTotal" value="100"/>
    <!-- 池中最大空閑鏈接數(shù) -->
    <property name="maxIdle" value="50"/>
    <!-- 池中最小空閑鏈接數(shù) -->
    <property name="minIdle" value="20"/>
    <!-- 當(dāng)池中鏈接耗盡,調(diào)用者最大阻塞時(shí)間,超出此時(shí)間將跑出異常。(單位:毫秒;默認(rèn)為-1,表示永不超時(shí)) -->
    <property name="maxWaitMillis" value="1000"/>
    <!-- 參考:http://biasedbit.com/redis-jedispool-configuration/ -->
    <!-- 調(diào)用者獲取鏈接時(shí),是否檢測(cè)當(dāng)前鏈接有效性。無(wú)效則從鏈接池中移除,并嘗試?yán)^續(xù)獲取。(默認(rèn)為false) -->
    <property name="testOnBorrow" value="true" />
    <!-- 向鏈接池中歸還鏈接時(shí),是否檢測(cè)鏈接有效性。(默認(rèn)為false) -->
    <property name="testOnReturn" value="true" />
    <!-- 調(diào)用者獲取鏈接時(shí),是否檢測(cè)空閑超時(shí)。如果超時(shí),則會(huì)被移除(默認(rèn)為false) -->
    <property name="testWhileIdle" value="true" />
    <!-- 空閑鏈接檢測(cè)線程一次運(yùn)行檢測(cè)多少條鏈接 -->
    <property name="numTestsPerEvictionRun" value="10" />
    <!-- 空閑鏈接檢測(cè)線程檢測(cè)周期。如果為負(fù)值,表示不運(yùn)行檢測(cè)線程。(單位:毫秒,默認(rèn)為-1) -->
    <property name="timeBetweenEvictionRunsMillis" value="60000" />
    <!-- 鏈接獲取方式。隊(duì)列: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"));
  }

}

方式二,依賴注入

與方式一類似,但是需要寫(xiě)死具體使用主的池還是從的池,思路如下:
放棄注解的方式,直接將主和從的兩個(gè)鏈接池注入到具體實(shí)現(xiàn)類中。

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">
    <!-- 池中最大鏈接數(shù) -->
    <property name="maxTotal" value="100"/>
    <!-- 池中最大空閑鏈接數(shù) -->
    <property name="maxIdle" value="50"/>
    <!-- 池中最小空閑鏈接數(shù) -->
    <property name="minIdle" value="20"/>
    <!-- 當(dāng)池中鏈接耗盡,調(diào)用者最大阻塞時(shí)間,超出此時(shí)間將跑出異常。(單位:毫秒;默認(rèn)為-1,表示永不超時(shí)) -->
    <property name="maxWaitMillis" value="1000"/>
    <!-- 參考:http://biasedbit.com/redis-jedispool-configuration/ -->
    <!-- 調(diào)用者獲取鏈接時(shí),是否檢測(cè)當(dāng)前鏈接有效性。無(wú)效則從鏈接池中移除,并嘗試?yán)^續(xù)獲取。(默認(rèn)為false) -->
    <property name="testOnBorrow" value="true" />
    <!-- 向鏈接池中歸還鏈接時(shí),是否檢測(cè)鏈接有效性。(默認(rèn)為false) -->
    <property name="testOnReturn" value="true" />
    <!-- 調(diào)用者獲取鏈接時(shí),是否檢測(cè)空閑超時(shí)。如果超時(shí),則會(huì)被移除(默認(rèn)為false) -->
    <property name="testWhileIdle" value="true" />
    <!-- 空閑鏈接檢測(cè)線程一次運(yùn)行檢測(cè)多少條鏈接 -->
    <property name="numTestsPerEvictionRun" value="10" />
    <!-- 空閑鏈接檢測(cè)線程檢測(cè)周期。如果為負(fù)值,表示不運(yùn)行檢測(cè)線程。(單位:毫秒,默認(rèn)為-1) -->
    <property name="timeBetweenEvictionRunsMillis" value="60000" />
    <!-- 鏈接獲取方式。隊(duì)列: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>

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • 詳解Redis中Lua腳本的應(yīng)用和實(shí)踐

    詳解Redis中Lua腳本的應(yīng)用和實(shí)踐

    這篇文章主要介紹了詳解Redis中Lua腳本的應(yīng)用和實(shí)踐,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2019-01-01
  • redis復(fù)制集群搭建的實(shí)現(xiàn)

    redis復(fù)制集群搭建的實(shí)現(xiàn)

    redis 復(fù)制集群是開(kāi)發(fā)中一種比較常用的集群模式,本文主要介紹了redis復(fù)制集群搭建的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2022-08-08
  • Redis持久化RDB和AOF區(qū)別詳解

    Redis持久化RDB和AOF區(qū)別詳解

    這篇文章主要介紹了Redis持久化RDB和AOF區(qū)別詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-10-10
  • Redis實(shí)現(xiàn)好友關(guān)注的示例代碼

    Redis實(shí)現(xiàn)好友關(guān)注的示例代碼

    本文主要介紹了Redis實(shí)現(xiàn)好友關(guān)注的示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2023-01-01
  • 寶塔中ThinkPHP框架使用Redis的一系列教程

    寶塔中ThinkPHP框架使用Redis的一系列教程

    關(guān)于redis的安裝,在服務(wù)器或者虛擬機(jī)上安裝寶塔面板安裝redis,這樣很簡(jiǎn)單就可以使用redis了,記得安裝redis的時(shí)候不僅要安裝redis軟件,還要進(jìn)入項(xiàng)目使用的php版本中安裝redis擴(kuò)展,然后開(kāi)啟redis軟件
    2023-03-03
  • redis-trib.rb命令詳解

    redis-trib.rb命令詳解

    redis-trib.rb是官方提供的Redis Cluster的管理工具,無(wú)需額外下載,默認(rèn)位于源碼包的src目錄下,但因該工具是用ruby開(kāi)發(fā)的,所以需要準(zhǔn)備相關(guān)的依賴環(huán)境,這篇文章主要介紹了redis-trib.rb命令詳解,需要的朋友可以參考下
    2023-10-10
  • 通過(guò)redis的腳本lua如何實(shí)現(xiàn)搶紅包功能

    通過(guò)redis的腳本lua如何實(shí)現(xiàn)搶紅包功能

    這篇文章主要給大家介紹了關(guān)于通過(guò)redis的腳本lua如何實(shí)現(xiàn)搶紅包功能的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-05-05
  • Redis如何部署哨兵

    Redis如何部署哨兵

    本文主要介紹了Redis如何部署哨兵,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2023-07-07
  • Redis cluster集群模式的原理解析

    Redis cluster集群模式的原理解析

    這篇文章主要介紹了Redis cluster集群模式的原理解析,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2021-04-04
  • 配置redis.conf遠(yuǎn)程訪問(wèn)的操作

    配置redis.conf遠(yuǎn)程訪問(wèn)的操作

    文章詳細(xì)介紹了Redis的配置文件位置、如何編輯配置文件以實(shí)現(xiàn)遠(yuǎn)程訪問(wèn),以及驗(yàn)證和監(jiān)控Redis配置的方法,感興趣的朋友一起看看吧
    2025-02-02

最新評(píng)論

合川市| 枞阳县| 唐海县| 通榆县| 宁强县| 思南县| 津南区| 荆门市| 赤水市| 新源县| 虞城县| 厦门市| 潜山县| 新郑市| 丰顺县| 岳阳市| 内黄县| 柘荣县| 响水县| 灯塔市| 香河县| 青海省| 彭阳县| 河南省| 友谊县| 肃北| 衡阳市| 连云港市| 大安市| 韶关市| 故城县| 益阳市| 凉山| 郸城县| 丰镇市| 平和县| 泸水县| 达拉特旗| 仙桃市| 四会市| 桃源县|