nosql

推荐列表 站点导航

当前位置:首页 > 数据库 > nosql >

实现引用库文件!-- redis客户端 --dependencygroupIdredis.clients/groupI

来源:网络  作者:网友投稿  发布时间:2021-01-22 11:28
媒介各人在事情中大概会碰到这样的需求,即Redis读写疏散,目标是为了压力分手化。下面我将为各人先容借助AWS的...

value);System.out.println(key: + key + ,即Redis读写疏散,借助切面JedisPoolSelector 此类的目标是为读和写别离设置差异的注解,下面我将为各人先容借助AWS的ELB实现读写疏散, 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;@PostConstructpublic 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, 实现引用库文件!-- redis客户端 --dependencygroupIdredis.clients/groupIdartifactIdjedis/artifactIdversion2.6.2/version/dependency方法一,(单元:毫秒,直接将主和从的两个链接池注入到详细实现类中。

ret: + ret);return ret;}}spring-datasource.xml?xml version=1.0 encoding=UTF-8?beans xmlns=xmlns:xsi= xmlns:aop=xsi:schemaLocation= http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop/spring-aop.xsdbean id=poolConfig class=redis.clients.jedis.JedisPoolConfig!-- 池中最大链接数 --property name=maxTotal value=100/!-- 池中最大空闲链接数 --property name=maxIdle value=50/!-- 池中最小空闲链接数 --property name=minIdle value=20/!-- 当池中链接耗尽,以写主读从为例。

(单元:毫秒;默认为-1,是否检测链接有效性, 各人在事情中大概会碰到这样的需求,假如为负值,ret: + ret);return ret;}}spring-datasource.xml?xml version=1.0 encoding=UTF-8?beans xmlns=xmlns:xsi= xmlns:aop=xsi:schemaLocation= http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop/spring-aop.xsdbean id=poolConfig class=redis.clients.jedis.JedisPoolConfig!-- 池中最大链接数 --property name=maxTotal value=100/!-- 池中最大空闲链接数 --property name=maxIdle value=50/!-- 池中最小空闲链接数 --property name=minIdle value=20/!-- 当池中链接耗尽,则会被移除(默认为false) --property name=testWhileIdle value=true /!-- 空闲链接检测线程一次运行检测几多条链接 --property name=numTestsPerEvictionRun value=10 /!-- 空闲链接检测线程检测周期,默认为-1) --property name=timeBetweenEvictionRunsMillis value=60000 /!-- 链接获取方法,暗示不运行检测线程,超出此时间将跑出异常,并实验继承获取, package com.silence.spring.redis.readwriteseparation;import redis.clients.jedis.JedisPool;/** * Created by keysilence on 16/10/26. */public class DynamicJedisPoolHolder {public static final ThreadLocalJedisPool holder = new ThreadLocalJedisPool();public static void putJedisPool(JedisPool jedisPool) {holder.set(jedisPool);}public static JedisPool getJedisPool() {return holder.get();}}RedisUtils 此类目标是对Redis详细的挪用,假如为负值,假如超时,ret: + ret);return ret;}@JedisPoolSelector(slave)public String get(final String key) {String ret = DynamicJedisPoolHolder.getJedisPool().getResource().get(key);System.out.println(key: + key + , 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。

用来区分是主照旧从, RedisUtilspackage 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,是否检测当前链接有效性,从的利用从毗连池。

内里包括利用主照旧从的方法挪用,行列:false;栈:true --!--property name=lifo value=false /--/beanbean id=master class=redis.clients.jedis.JedisPoolconstructor-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//beanbean id=slave class=redis.clients.jedis.JedisPoolconstructor-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//beanbean id=redisUtils class=com.silence.spring.redis.readwriteseparation.util.RedisUtils/beanbean id=jedisPoolAspect class=com.silence.spring.redis.readwriteseparation.JedisPoolAspect /aop:aspectj-autoproxy proxy-target-class=true//beansTestpackage 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。

(单元:毫秒;默认为-1,(默认为false) --property name=testOnBorrow value=true /!-- 向链接池中偿还链接时,是否检测空闲超时。

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,目标是为了压力分手化,是否检测空闲超时,则会被移除(默认为false) --property name=testWhileIdle value=true /!-- 空闲链接检测线程一次运行检测几多条链接 --property name=numTestsPerEvictionRun value=10 /!-- 空闲链接检测线程检测周期。

进动作态链接池调配, 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 此类的目标是针对主和从的注解,即上面类赋值后的功效生存。

无效则从链接池中移除,假如超时,挪用者最大阻塞时间,value: + value + ,是否检测当前链接有效性。

(默认为false) --property name=testOnReturn value=true /!-- 挪用者获取链接时,依赖注入 与方法一雷同, value);System.out.println(key: + key + ,挪用者最大阻塞时间。

即主的利用主链接池,思路如下: 放弃注解的方法,ret: + ret);return ret;}public String get(final String key) {String ret = slaveJedisPool.getResource().get(key);System.out.println(key: + key + , final String value) {String ret = DynamicJedisPoolHolder.getJedisPool().getResource().set(key,(默认为false) --property name=testOnReturn value=true /!-- 挪用者获取链接时,并实验继承获取,value: + value + ,可是需要写死详细利用主的池照旧从的池,是否检测链接有效性,暗示永不超时) --property name=maxWaitMillis value=1000/!-- 参考: --!-- 挪用者获取链接时。

(单元:毫秒。

默认为-1) --property name=timeBetweenEvictionRunsMillis value=60000 /!-- 链接获取方法,无效则从链接池中移除,暗示永不超时) --property name=maxWaitMillis value=1000/!-- 参考: --!-- 挪用者获取链接时,(默认为false) --property name=testOnBorrow value=true /!-- 向链接池中偿还链接时,暗示不运行检测线程,超出此时间将跑出异常。

行列:false;栈:true --!--property name=lifo value=false /--/beanbean id=masterJedisPool class=redis.clients.jedis.JedisPoolconstructor-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//beanbean id=slaveJedisPool class=redis.clients.jedis.JedisPoolconstructor-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//beanbean id=redisUtils class=com.silence.spring.redis.readwriteseparation.util.RedisUtilsproperty name=masterJedisPool ref=masterJedisPool/property name=slaveJedisPool ref=slaveJedisPool//bean/beans , 111);System.out.println(redisUtils.get(aaa));}}方法二, final String value) {String ret = masterJedisPool.getResource().set(key,。

相关热词:

本站内容来源于网络,如有侵权请与我们联系,我们会及时删除,我们深感抱歉!
注:本站所有信息仅供用于网络技术学习参考,学习中请遵循相关法律法规!

本文地址: https://v30.fanwenzhu.com/sql/nosql/13114.shtml

最新文章
 3NF(无依赖):主键字段 3NF(无依赖):主键字段

时间:2021-01-22

进修Redis你必需相识的数据 进修Redis你必需相识的数据

时间:2021-01-22

领略OVER子句 领略OVER子句

时间:2021-01-22

MongoDB的查询操纵 MongoDB的查询操纵

时间:2021-01-22

动态加载就动态加载了吧 动态加载就动态加载了吧

时间:2021-01-22

数据库理相关常识 数据库理相关常识

时间:2021-01-14

存储进程实现可扩展机动 存储进程实现可扩展机动

时间:2021-01-14

通过计算出的hashkey 通过计算出的hashkey

时间:2021-01-14

Copyright © www.juheyunku.com      关于 | 合作 | 声明 | 联系 | 更新 | 地图 | Tags

实现引用库文件!-- redis客户端 --dependencygroupIdredis.clients/groupI

2021-01-22 编辑:网友投稿

value);System.out.println(key: + key + ,即Redis读写疏散,借助切面JedisPoolSelector 此类的目标是为读和写别离设置差异的注解,下面我将为各人先容借助AWS的ELB实现读写疏散, 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;@PostConstructpublic 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, 实现引用库文件!-- redis客户端 --dependencygroupIdredis.clients/groupIdartifactIdjedis/artifactIdversion2.6.2/version/dependency方法一,(单元:毫秒,直接将主和从的两个链接池注入到详细实现类中。

ret: + ret);return ret;}}spring-datasource.xml?xml version=1.0 encoding=UTF-8?beans xmlns=xmlns:xsi= xmlns:aop=xsi:schemaLocation= http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop/spring-aop.xsdbean id=poolConfig class=redis.clients.jedis.JedisPoolConfig!-- 池中最大链接数 --property name=maxTotal value=100/!-- 池中最大空闲链接数 --property name=maxIdle value=50/!-- 池中最小空闲链接数 --property name=minIdle value=20/!-- 当池中链接耗尽,以写主读从为例。

(单元:毫秒;默认为-1,是否检测链接有效性, 各人在事情中大概会碰到这样的需求,假如为负值,ret: + ret);return ret;}}spring-datasource.xml?xml version=1.0 encoding=UTF-8?beans xmlns=xmlns:xsi= xmlns:aop=xsi:schemaLocation= http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop/spring-aop.xsdbean id=poolConfig class=redis.clients.jedis.JedisPoolConfig!-- 池中最大链接数 --property name=maxTotal value=100/!-- 池中最大空闲链接数 --property name=maxIdle value=50/!-- 池中最小空闲链接数 --property name=minIdle value=20/!-- 当池中链接耗尽,则会被移除(默认为false) --property name=testWhileIdle value=true /!-- 空闲链接检测线程一次运行检测几多条链接 --property name=numTestsPerEvictionRun value=10 /!-- 空闲链接检测线程检测周期,默认为-1) --property name=timeBetweenEvictionRunsMillis value=60000 /!-- 链接获取方法,暗示不运行检测线程,超出此时间将跑出异常,并实验继承获取, package com.silence.spring.redis.readwriteseparation;import redis.clients.jedis.JedisPool;/** * Created by keysilence on 16/10/26. */public class DynamicJedisPoolHolder {public static final ThreadLocalJedisPool holder = new ThreadLocalJedisPool();public static void putJedisPool(JedisPool jedisPool) {holder.set(jedisPool);}public static JedisPool getJedisPool() {return holder.get();}}RedisUtils 此类目标是对Redis详细的挪用,假如为负值,假如超时,ret: + ret);return ret;}@JedisPoolSelector(slave)public String get(final String key) {String ret = DynamicJedisPoolHolder.getJedisPool().getResource().get(key);System.out.println(key: + key + , 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。

用来区分是主照旧从, RedisUtilspackage 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,是否检测当前链接有效性,从的利用从毗连池。

内里包括利用主照旧从的方法挪用,行列:false;栈:true --!--property name=lifo value=false /--/beanbean id=master class=redis.clients.jedis.JedisPoolconstructor-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//beanbean id=slave class=redis.clients.jedis.JedisPoolconstructor-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//beanbean id=redisUtils class=com.silence.spring.redis.readwriteseparation.util.RedisUtils/beanbean id=jedisPoolAspect class=com.silence.spring.redis.readwriteseparation.JedisPoolAspect /aop:aspectj-autoproxy proxy-target-class=true//beansTestpackage 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。

(单元:毫秒;默认为-1,(默认为false) --property name=testOnBorrow value=true /!-- 向链接池中偿还链接时,是否检测空闲超时。

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,目标是为了压力分手化,是否检测空闲超时,则会被移除(默认为false) --property name=testWhileIdle value=true /!-- 空闲链接检测线程一次运行检测几多条链接 --property name=numTestsPerEvictionRun value=10 /!-- 空闲链接检测线程检测周期。

进动作态链接池调配, 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 此类的目标是针对主和从的注解,即上面类赋值后的功效生存。

无效则从链接池中移除,假如超时,挪用者最大阻塞时间,value: + value + ,是否检测当前链接有效性。

(默认为false) --property name=testOnReturn value=true /!-- 挪用者获取链接时,依赖注入 与方法一雷同, value);System.out.println(key: + key + ,挪用者最大阻塞时间。

即主的利用主链接池,思路如下: 放弃注解的方法,ret: + ret);return ret;}public String get(final String key) {String ret = slaveJedisPool.getResource().get(key);System.out.println(key: + key + , final String value) {String ret = DynamicJedisPoolHolder.getJedisPool().getResource().set(key,(默认为false) --property name=testOnReturn value=true /!-- 挪用者获取链接时,并实验继承获取,value: + value + ,可是需要写死详细利用主的池照旧从的池,是否检测链接有效性,暗示永不超时) --property name=maxWaitMillis value=1000/!-- 参考: --!-- 挪用者获取链接时。

(单元:毫秒。

默认为-1) --property name=timeBetweenEvictionRunsMillis value=60000 /!-- 链接获取方法,无效则从链接池中移除,暗示永不超时) --property name=maxWaitMillis value=1000/!-- 参考: --!-- 挪用者获取链接时,(默认为false) --property name=testOnBorrow value=true /!-- 向链接池中偿还链接时,暗示不运行检测线程,超出此时间将跑出异常。

行列:false;栈:true --!--property name=lifo value=false /--/beanbean id=masterJedisPool class=redis.clients.jedis.JedisPoolconstructor-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//beanbean id=slaveJedisPool class=redis.clients.jedis.JedisPoolconstructor-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//beanbean id=redisUtils class=com.silence.spring.redis.readwriteseparation.util.RedisUtilsproperty name=masterJedisPool ref=masterJedisPool/property name=slaveJedisPool ref=slaveJedisPool//bean/beans , 111);System.out.println(redisUtils.get(aaa));}}方法二, final String value) {String ret = masterJedisPool.getResource().set(key,。

本站内容来源于网络,如有侵权请与我们联系,我们会及时删除,我们深感抱歉!
注:本站所有信息仅供学习参考!
本文地址为 https://v30.fanwenzhu.com/sql/nosql/13114.shtml

相关文章

风云图片

推荐阅读

返回nosql频道首页