spring-data整合了redispool, 并提供redisTemplate使用, 但有时需要用到shradedJedisPool, 就需要手动注入了
手写redispool并注入springboot中
1, redis配置文件
redis.properties
redis.config.ip=192.168.50.37redis.config.port=6379redis.config.maxTotal=20redis.config.maxIdle=5redis.config.maxWaitmillis=10000redis.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 */@Componentpublic 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()); ArrayListlist = new ArrayList (); 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中
org.springframework.boot spring-boot-configuration-processor true
入口程序中, 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@EnableConfigurationPropertiespublic class APP { public static void main(String[] args) { SpringApplication.run(APP.class, args); } }
在使用redis时, 就可以通过 @Autowired 进行注入了
系列原创, 转载请注明出处, 谢谢 @Wenbronk: http://www.cnblogs.com/wenbronk/p/6672054.html