redis-手写redis切片和非切片连接池并注入springboot中
spring-data整合了redispool, 并提供redisTemplate使用, 但有时需要用到shradedJedisPool, 就需要手动注入了
手写redispool并注入springboot中
1, redis配置文件
redis.properties
redis.config.ip=192.168.50.37
redis.config.port= redis.config.maxTotal=
redis.config.maxIdle=
redis.config.maxWaitmillis=
redis.config.testOnborrow=false
2, RedisClientConfig.java, 获取spring注入的属性值
package com.iwhere.learn.redis.java; import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component; /**
* 使用一个专门的类来获取配置文件
* @author wenbronk
* @time 2017年4月6日 上午11:11:57 2017
*/ @Component(value="redisClientConfig")
@ConfigurationProperties(prefix = "redis.config")
@PropertySource("classpath:source/redis.properties")
public class RedisClientConfig { private String ip;
private int port;
private int maxTotal;
private int maxIdle;
private long maxWaitmillis;
private boolean testOnborrow; public String getIp() {
return ip;
}
public void setIp(String ip) {
this.ip = ip;
}
public int getPort() {
return port;
}
public void setPort(int port) {
this.port = port;
}
public int getMaxTotal() {
return maxTotal;
}
public void setMaxTotal(int maxTotal) {
this.maxTotal = maxTotal;
}
public int getMaxIdle() {
return maxIdle;
}
public void setMaxIdle(int maxIdle) {
this.maxIdle = maxIdle;
}
public long getMaxWaitmillis() {
return maxWaitmillis;
}
public void setMaxWaitmillis(long maxWaitmillis) {
this.maxWaitmillis = maxWaitmillis;
}
public boolean isTestOnborrow() {
return testOnborrow;
}
public void setTestOnborrow(boolean testOnborrow) {
this.testOnborrow = testOnborrow;
}
}
3, RedisClientUtils, 用于生成 jedisPool 并注入spring中
package com.iwhere.learn.redis.java; import java.util.ArrayList; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component; import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
import redis.clients.jedis.JedisShardInfo;
import redis.clients.jedis.ShardedJedis;
import redis.clients.jedis.ShardedJedisPool; /**
* 用于获取redis连接池
*
* @author wenbronk
* @time 2017年3月24日 下午1:43:36 2017
*/ @Component
public class RedisClientUtil { @Autowired
private RedisClientConfig redisClientConfig; /** 非切片连接池 */
private JedisPool jedisPool; /** 切片连接池 */
private ShardedJedisPool shardedJedisPool; /**
* 初始化非切片连接池
*/
public JedisPool getJedisPool() {
JedisPoolConfig config = new JedisPoolConfig();
config.setMaxTotal(redisClientConfig.getMaxTotal());
config.setMaxIdle(redisClientConfig.getMaxIdle());
config.setMaxWaitMillis(redisClientConfig.getMaxWaitmillis());
config.setTestOnBorrow(redisClientConfig.isTestOnborrow()); jedisPool = new JedisPool(config, redisClientConfig.getIp(), redisClientConfig.getPort());
return jedisPool;
} /**
* 初始化切片连接池
*/
// @Bean(name="shardedJedisPool")
public ShardedJedisPool getShardedJedisPool() {
// 池基本配置
JedisPoolConfig config = new JedisPoolConfig();
config.setMaxTotal(redisClientConfig.getMaxTotal());
config.setMaxIdle(redisClientConfig.getMaxIdle());
config.setMaxWaitMillis(redisClientConfig.getMaxWaitmillis());
config.setTestOnBorrow(redisClientConfig.isTestOnborrow()); ArrayList<JedisShardInfo> list = new ArrayList<JedisShardInfo>();
list.add(new JedisShardInfo(redisClientConfig.getIp(), redisClientConfig.getPort(), "master")); shardedJedisPool = new ShardedJedisPool(config, list);
return shardedJedisPool;
} public JedisPool getSingleJedisPool() {
if (jedisPool == null) {
synchronized(RedisClientUtil.class) {
if (jedisPool == null) {
return getJedisPool();
}
}
}
return jedisPool;
} @Bean(name="jedis")
public Jedis getJedis() {
return getSingleJedisPool().getResource();
} // @Bean(name="shardedJedis")
public ShardedJedis getShardedJedis() {
return shardedJedisPool.getResource();
}
}
4, 引入注值所需要的依赖和注解
pom.xml中
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
入口程序中, springboot环境
package com.iwhere.learn; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.web.bind.annotation.RestController; import com.iwhere.learn.redis.java.RedisClientConfig;
import com.iwhere.learn.redis.java.RedisClientUtil; @RestController
@SpringBootApplication
@EnableConfigurationProperties
public class APP { public static void main(String[] args) {
SpringApplication.run(APP.class, args);
} }
在使用redis时, 就可以通过 @Autowired 进行注入了
系列原创, 转载请注明出处, 谢谢 @Wenbronk: http://www.cnblogs.com/wenbronk/p/6672054.html
redis-手写redis切片和非切片连接池并注入springboot中的更多相关文章
- C基础 带你手写 redis sds
前言 - Simple Dynamic Strings antirez 想统一 Redis,Disque,Hiredis 项目中 SDS 代码, 因此构建了这个项目 https://github.c ...
- C基础 带你手写 redis adlist 双向链表
引言 - 导航栏目 有些朋友可能对 redis 充满着数不尽的求知欲, 也许是 redis 属于工作, 交流(面试)的大头戏, 不得不 ... 而自己当下对于 redis 只是停留在会用层面, 细节层 ...
- 手写redis的docker文件,通过docker-compose配置redis
在前面一遍随笔,配置的是mysql主从的docker-compose配置.今天我们来学习配置编排容器redis. 准备环境: docker 18.06.1-ce docker-compose 1.23 ...
- 用C、python手写redis客户端,兼容redis集群 (-MOVED和-ASK),快速搭建redis集群
想没想过,自己写一个redis客户端,是不是很难呢? 其实,并不是特别难. 首先,要知道redis服务端用的通信协议,建议直接去官网看,博客啥的其实也是从官网摘抄的,或者从其他博客抄的(忽略). 协议 ...
- 手写redis客户端
一.RESP通信协议 Redis Serialization Protocol (Redis序列化协议). 特点:容易实现.解析快.可读性强 以\r\n分割数据. 二.撸代码 package com. ...
- Java连接Redis,存储对象获取对象()byte和json),连接池
Java连接Redis Jedis连接Redis,Lettuce连接Redis Jedis连接Redis 1. 创建maven项目 2. 引入依赖 <dependencies> <d ...
- Java Redis系列3(Jedis的使用+jedis连接池技术)
Jedis的使用 什么是Jedis? 一款Java操作redis数据库的工具 使用步骤 1.下载redis所需的java包 2.使用步骤 import org.junit.Test; public c ...
- C基础 带你手写 redis ae 事件驱动模型
引言 - 整体认识 redis ae 事件驱动模型, 网上聊得很多. 但当你仔细看完一篇又一篇之后, 可能你看的很舒服, 但对于 作者为什么要这么写, 出发点, 好处, 缺点 ... 可能还是好模糊, ...
- 关于布隆过滤器,手写你真的知其原理吗?让我来带你手写redis布隆过滤器。
说到布隆过滤器不得不提到,redis, redis作为现在主流的nosql数据库,备受瞩目:它的丰富的value类型,以及它的偏向计算向数据移动属性减少IO的成本问题.备受开发人员的青睐.通常我们使用 ...
随机推荐
- HDU1181 变形课(DFS) 2016-07-24 13:31 73人阅读 评论(0) 收藏
变形课 Problem Description 呃......变形课上Harry碰到了一点小麻烦,因为他并不像Hermione那样能够记住所有的咒语而随意的将一个棒球变成刺猬什么的,但是他发现了变形咒 ...
- android注解处理技术APT
APT(Annotation Processing Tool)是java的注解处理技术,它对源代码文件进行检测找出其中的Annotation,根据注解和注解处理器和相应的apt自动生成代码. Anno ...
- cocos studio
用了几天,和之前用的cocos creator以及unity的编辑器一对比,很多地方都挺反人类的哈... 拖拽和放大场景元素竟然还要切换一下编辑模式... 移动场景元素竟然没有单独控制x或者y方向的移 ...
- SQL SERVER 2008 附加数据库出现只读问题。
问题描述 在附加数据库时出现的图片: 解决办法 步骤一,修改文件夹的 1.打开该数据库文件存放的目录或数据库文件的属性窗口,选择"属性"菜单->选择"安全&qu ...
- TFS Java SDK使用指南
[2018.3.6 更新] 最新版本的TFS Java SDK(14.123.1)支持Java SDK 1.6版本,可以从Oracle的官方网站(http://www.oracle.com/techn ...
- 设计模式之复合模式(Compound Pattern)
一.什么是复合模式? 在形式上,复合模式确实是多个模式的组合,但满足了这一条并不一定是复合模式,注意它的定义: 将多个模式结合起来形成一个“框架”,以解决一般性问题 一提到“框架”,可能最容易联想到的 ...
- LinkedBlockingQueue源码解析(1)
此文已由作者赵计刚授权网易云社区发布. 欢迎访问网易云社区,了解更多网易技术产品运营经验. 1.对于LinkedBlockingQueue需要掌握以下几点 创建 入队(添加元素) 出队(删除元素) 2 ...
- AJPFX告诉你MT4平台有什么优势?
FX TERMINAL(Meta Trader4)交易平台功能 成功驾驭金融市场的第一步是拥有正确的工具.AJPFX为客户提供二十四小时的在线交易服务,MT4交易软件是目前全世界上最为先进,应用最为广 ...
- KMP Demo
The key of Kmp is to build a look up table that records the match result of prefix and postfix. Valu ...
- Python os.path.join() 进行路径拼接
在python 项目开发过程中,经常需要将获取到的路径进行拼接, # os.path.join(path1,path2) 将两个路径拼接起来 os.path.join("/usr" ...