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中的更多相关文章

  1. C基础 带你手写 redis sds

    前言 - Simple Dynamic Strings  antirez 想统一 Redis,Disque,Hiredis 项目中 SDS 代码, 因此构建了这个项目 https://github.c ...

  2. C基础 带你手写 redis adlist 双向链表

    引言 - 导航栏目 有些朋友可能对 redis 充满着数不尽的求知欲, 也许是 redis 属于工作, 交流(面试)的大头戏, 不得不 ... 而自己当下对于 redis 只是停留在会用层面, 细节层 ...

  3. 手写redis的docker文件,通过docker-compose配置redis

    在前面一遍随笔,配置的是mysql主从的docker-compose配置.今天我们来学习配置编排容器redis. 准备环境: docker 18.06.1-ce docker-compose 1.23 ...

  4. 用C、python手写redis客户端,兼容redis集群 (-MOVED和-ASK),快速搭建redis集群

    想没想过,自己写一个redis客户端,是不是很难呢? 其实,并不是特别难. 首先,要知道redis服务端用的通信协议,建议直接去官网看,博客啥的其实也是从官网摘抄的,或者从其他博客抄的(忽略). 协议 ...

  5. 手写redis客户端

    一.RESP通信协议 Redis Serialization Protocol (Redis序列化协议). 特点:容易实现.解析快.可读性强 以\r\n分割数据. 二.撸代码 package com. ...

  6. Java连接Redis,存储对象获取对象()byte和json),连接池

    Java连接Redis Jedis连接Redis,Lettuce连接Redis Jedis连接Redis 1. 创建maven项目 2. 引入依赖 <dependencies> <d ...

  7. Java Redis系列3(Jedis的使用+jedis连接池技术)

    Jedis的使用 什么是Jedis? 一款Java操作redis数据库的工具 使用步骤 1.下载redis所需的java包 2.使用步骤 import org.junit.Test; public c ...

  8. C基础 带你手写 redis ae 事件驱动模型

    引言 - 整体认识 redis ae 事件驱动模型, 网上聊得很多. 但当你仔细看完一篇又一篇之后, 可能你看的很舒服, 但对于 作者为什么要这么写, 出发点, 好处, 缺点 ... 可能还是好模糊, ...

  9. 关于布隆过滤器,手写你真的知其原理吗?让我来带你手写redis布隆过滤器。

    说到布隆过滤器不得不提到,redis, redis作为现在主流的nosql数据库,备受瞩目:它的丰富的value类型,以及它的偏向计算向数据移动属性减少IO的成本问题.备受开发人员的青睐.通常我们使用 ...

随机推荐

  1. OpenGl 中的基本数据类型

    OpenGl 中的基本数据类型 为了便于 OpenGL在各种平台上移植,OpenGL定义了自己的数据类型. 如果你愿意也可用这些数据类型对应的标准C的数据类型来替代.如OpenGL也定义 GLvoid ...

  2. opencpu

    前端通过它调用后端的R语言,对R函数进行一个封装. 网址:https://github.com/jeroenooms/opencpu.js 使用的是opencpu-0.5.js,对它进行了修改. 1. ...

  3. Linux应用监控工具

    Linux下的监控工具丰富繁杂,如果只知道top.free之类的就太少了,而且也不能胜任日常的Linux管理工作,尤其是在排除Web服务器问题时. 本文给出5个Linux下功能更为强大的监控工具,有了 ...

  4. linux 下mysql/php编译配置参数

    mysql cmake 编译参数 cmake -DCMAKE_INSTALL_PREFIX=/opt/mysql \-DSYSCONFDIR=/opt/mysql/etc \-DMYSQL_DATAD ...

  5. Lucene.net 全文检索文件

    using Lucene.Net.Analysis; using Lucene.Net.Analysis.Tokenattributes; using Lucene.Net.Documents; us ...

  6. From Alpha to Gamma (II)

    这篇文章被拖延得这么久是因为我没有找到合适的引言 -- XXX 这一篇接着讲Gamma.近几年基于物理的渲染(Physically Based Shading, 后文简称PBS)开始在游戏业界受到关注 ...

  7. 使用xargs与awk联合使用批量杀进程,很方便

    ps -ef | grep java | grep alarm | awk '{print $2}' | xargs kill -9 注*A.  $2表示第2列,即进程号PID; awk很强大,这里不 ...

  8. Nigix配置

  9. django 获取request请求对象及response响应对象中的各种属性值

    django request对象和HttpResponse对象 HttpRequest对象(除非特殊说明,所有属性都是只读,session属性是个例外) HttpRequest.scheme 请求方案 ...

  10. IOS 开发入门

    1.Getting Started https://developer.apple.com/library/ios/navigation/#section=Resource%20Types&t ...