码迷,mamicode.com
首页 > 编程语言 > 详细

Springboot redis 整合

时间:2017-12-12 17:38:15      阅读:188      评论:0      收藏:0      [点我收藏+]

标签:调用   contex   ini   clust   pairs   depend   temp   seconds   str   

年末将至,是时候该把所学的总结下了。最近正好从eclipes转到idea,发现idea对模组的支持很棒。这一片先总结下springboot和redis的整合

首先添加redis服务器

直接用docker 远程拉取即可,这里不再描述

 

直接撸代码

1.添加依赖

1 <dependency>
2             <groupId>org.springframework.boot</groupId>
3             <artifactId>spring-boot-starter-data-redis</artifactId>
4         </dependency>

2.在配置文件中增加配置的连接信息

## Redis 配置
## Redis数据库索引(默认为0)
spring.redis.database=0
## Redis服务器地址
spring.redis.host=127.0.0.1
## Redis服务器连接端口
spring.redis.port=6379
## Redis服务器连接密码(默认为空)
spring.redis.password=
## 连接池最大连接数(使用负值表示没有限制)
spring.redis.pool.max-active=8
## 连接池最大阻塞等待时间(使用负值表示没有限制)
spring.redis.pool.max-wait=-1
## 连接池中的最大空闲连接
spring.redis.pool.max-idle=8
## 连接池中的最小空闲连接
spring.redis.pool.min-idle=0
## 连接超时时间(毫秒)
spring.redis.timeout=0

好了,基本的配置就好了

我们可以写个测试类,看看调用情况

package com.hdkj.redis;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest
public class RedisApplicationTests {
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Test
    public void contextLoads() {
        stringRedisTemplate.opsForValue().set("xmz","23");
        String xmz = stringRedisTemplate.opsForValue().get("xmz");
        System.out.println(xmz);
    }

}

输出结果:23

好了,这就是最简单的配置了。

======================================================================================================

springboot更推荐我们把配置信息写在类里面

我们可以对以上的配置信息改造下

新建一个配置实体类   RedisProperties

  1 package com.hdkj.redis.config;
  2 
  3 import org.springframework.boot.context.properties.ConfigurationProperties;
  4 import org.springframework.context.annotation.PropertySource;
  5 
  6 import java.util.List;
  7 
  8 /**
  9  * @author xuminzhe
 10  * @version V1.0
 11  * @Project study
 12  * @Package com.hdkj.redis.config
 13  * @Description
 14  * @Date 2017/12/4
 15  */
 16 @ConfigurationProperties(prefix = "spring.redis")
 17 //@PropertySource("classpath:application.properties")
 18 public class RedisProperties {
 19     /**
 20      * Database index used by the connection factory.
 21      */
 22     private int database = 0;
 23 
 24     /**
 25      * Redis url, which will overrule host, port and password if set.
 26      */
 27     private String url;
 28 
 29     /**
 30      * Redis server host.
 31      */
 32     private String host = "localhost";
 33 
 34     /**
 35      * Login password of the redis server.
 36      */
 37     private String password;
 38 
 39     /**
 40      * Redis server port.
 41      */
 42     private int port = 6379;
 43 
 44     /**
 45      * Enable SSL.
 46      */
 47     private boolean ssl;
 48 
 49     /**
 50      * Connection timeout in milliseconds.
 51      */
 52     private int timeout;
 53 
 54     private RedisProperties.Pool pool;
 55 
 56     private RedisProperties.Sentinel sentinel;
 57 
 58     private RedisProperties.Cluster cluster;
 59 
 60     public int getDatabase() {
 61         return this.database;
 62     }
 63 
 64     public void setDatabase(int database) {
 65         this.database = database;
 66     }
 67 
 68     public String getUrl() {
 69         return this.url;
 70     }
 71 
 72     public void setUrl(String url) {
 73         this.url = url;
 74     }
 75 
 76     public String getHost() {
 77         return this.host;
 78     }
 79 
 80     public void setHost(String host) {
 81         this.host = host;
 82     }
 83 
 84     public String getPassword() {
 85         return this.password;
 86     }
 87 
 88     public void setPassword(String password) {
 89         this.password = password;
 90     }
 91 
 92     public int getPort() {
 93         return this.port;
 94     }
 95 
 96     public void setPort(int port) {
 97         this.port = port;
 98     }
 99 
100     public boolean isSsl() {
101         return this.ssl;
102     }
103 
104     public void setSsl(boolean ssl) {
105         this.ssl = ssl;
106     }
107 
108     public void setTimeout(int timeout) {
109         this.timeout = timeout;
110     }
111 
112     public int getTimeout() {
113         return this.timeout;
114     }
115 
116     public RedisProperties.Sentinel getSentinel() {
117         return this.sentinel;
118     }
119 
120     public void setSentinel(RedisProperties.Sentinel sentinel) {
121         this.sentinel = sentinel;
122     }
123 
124     public RedisProperties.Pool getPool() {
125         return this.pool;
126     }
127 
128     public void setPool(RedisProperties.Pool pool) {
129         this.pool = pool;
130     }
131 
132     public RedisProperties.Cluster getCluster() {
133         return this.cluster;
134     }
135 
136     public void setCluster(RedisProperties.Cluster cluster) {
137         this.cluster = cluster;
138     }
139 
140     /**
141      * Pool properties.
142      */
143     public static class Pool {
144 
145         /**
146          * Max number of "idle" connections in the pool. Use a negative value to indicate
147          * an unlimited number of idle connections.
148          */
149         private int maxIdle = 8;
150 
151         /**
152          * Target for the minimum number of idle connections to maintain in the pool. This
153          * setting only has an effect if it is positive.
154          */
155         private int minIdle = 0;
156 
157         /**
158          * Max number of connections that can be allocated by the pool at a given time.
159          * Use a negative value for no limit.
160          */
161         private int maxActive = 8;
162 
163         /**
164          * Maximum amount of time (in milliseconds) a connection allocation should block
165          * before throwing an exception when the pool is exhausted. Use a negative value
166          * to block indefinitely.
167          */
168         private int maxWait = -1;
169 
170         public int getMaxIdle() {
171             return this.maxIdle;
172         }
173 
174         public void setMaxIdle(int maxIdle) {
175             this.maxIdle = maxIdle;
176         }
177 
178         public int getMinIdle() {
179             return this.minIdle;
180         }
181 
182         public void setMinIdle(int minIdle) {
183             this.minIdle = minIdle;
184         }
185 
186         public int getMaxActive() {
187             return this.maxActive;
188         }
189 
190         public void setMaxActive(int maxActive) {
191             this.maxActive = maxActive;
192         }
193 
194         public int getMaxWait() {
195             return this.maxWait;
196         }
197 
198         public void setMaxWait(int maxWait) {
199             this.maxWait = maxWait;
200         }
201 
202     }
203 
204     /**
205      * Cluster properties.
206      */
207     public static class Cluster {
208 
209         /**
210          * Comma-separated list of "host:port" pairs to bootstrap from. This represents an
211          * "initial" list of cluster nodes and is required to have at least one entry.
212          */
213         private List<String> nodes;
214 
215         /**
216          * Maximum number of redirects to follow when executing commands across the
217          * cluster.
218          */
219         private Integer maxRedirects;
220 
221         public List<String> getNodes() {
222             return this.nodes;
223         }
224 
225         public void setNodes(List<String> nodes) {
226             this.nodes = nodes;
227         }
228 
229         public Integer getMaxRedirects() {
230             return this.maxRedirects;
231         }
232 
233         public void setMaxRedirects(Integer maxRedirects) {
234             this.maxRedirects = maxRedirects;
235         }
236 
237     }
238 
239     /**
240      * Redis sentinel properties.
241      */
242     public static class Sentinel {
243 
244         /**
245          * Name of Redis server.
246          */
247         private String master;
248 
249         /**
250          * Comma-separated list of host:port pairs.
251          */
252         private String nodes;
253 
254         public String getMaster() {
255             return this.master;
256         }
257 
258         public void setMaster(String master) {
259             this.master = master;
260         }
261 
262         public String getNodes() {
263             return this.nodes;
264         }
265 
266         public void setNodes(String nodes) {
267             this.nodes = nodes;
268         }
269 
270     }
271 
272 }

如果熟悉mybatis配置的朋友一定对这种方式不会陌生,接下来我们在新建一个配置类

 1 package com.hdkj.redis.config;
 2 
 3 import org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration;
 4 import org.springframework.boot.context.properties.EnableConfigurationProperties;
 5 import org.springframework.cache.annotation.CachingConfigurerSupport;
 6 import org.springframework.cache.annotation.EnableCaching;
 7 import org.springframework.context.annotation.Bean;
 8 import org.springframework.context.annotation.Configuration;
 9 import org.springframework.context.annotation.Import;
10 import org.springframework.data.redis.connection.RedisConnectionFactory;
11 import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
12 import org.springframework.data.redis.core.RedisTemplate;
13 import redis.clients.jedis.JedisPoolConfig;
14 import redis.clients.jedis.JedisShardInfo;
15 import redis.clients.jedis.ShardedJedisPool;
16 
17 import javax.inject.Inject;
18 import java.util.ArrayList;
19 import java.util.List;
20 
21 /**
22  * @author xuminzhe
23  * @version V1.0
24  * @Project study
25  * @Package com.hdkj.redis.config
26  * @Description
27  * @Date 2017/12/4
28  */
29 @Configuration
30 @EnableCaching
31 @Import(value = RedisAutoConfiguration.class)
32 @EnableConfigurationProperties(RedisProperties.class)
33 public class RedisConfiguration extends CachingConfigurerSupport {
34     @Inject
35     private RedisProperties properties;
36 
37     @Bean
38     public RedisConnectionFactory jedisConnectionFactory() {
39         JedisPoolConfig poolConfig = new JedisPoolConfig();
40         poolConfig.setMaxTotal(properties.getPool().getMaxActive());
41         poolConfig.setMaxIdle(properties.getPool().getMaxIdle());
42         poolConfig.setMaxWaitMillis(properties.getPool().getMaxWait());
43         poolConfig.setTestOnBorrow(true);
44         poolConfig.setTestOnCreate(true);
45         poolConfig.setTestWhileIdle(true);
46         JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory(poolConfig);
47         jedisConnectionFactory.setHostName(properties.getHost());
48         if (null != properties.getPassword()) {
49             jedisConnectionFactory.setPassword(properties.getPassword());
50         }
51         jedisConnectionFactory.setDatabase(properties.getDatabase());
52         jedisConnectionFactory.setPort(properties.getPort());
53 
54         //其他配置,可再次扩展
55         return jedisConnectionFactory;
56     }
57 
58     @Bean(name = "redisTemplate")
59     public RedisTemplate redisTemplate() {
60         RedisTemplate redisTemplate = new RedisTemplate();
61 
62         redisTemplate.setConnectionFactory(jedisConnectionFactory());
63 
64 //        redisTemplate.setHashValueSerializer(new StringRedisSerializer());
65 //        redisTemplate.setKeySerializer(new StringRedisSerializer());
66 //        redisTemplate.setValueSerializer(new StringRedisSerializer());
67 //        redisTemplate.setHashKeySerializer(new StringRedisSerializer());
68 
69         redisTemplate.afterPropertiesSet();
70         redisTemplate.setEnableTransactionSupport(true);
71 
72         return redisTemplate;
73     }
74 
75     /**
76      * 分布式环境下多台redis的配置
77      * @return
78      */
79     @Bean
80     public ShardedJedisPool shardedJedisPool(){
81         JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
82         jedisPoolConfig.setMaxTotal(properties.getPool().getMaxActive());
83         jedisPoolConfig.setMaxIdle(properties.getPool().getMaxIdle());
84         jedisPoolConfig.setMaxWaitMillis(properties.getPool().getMaxWait());
85         List<JedisShardInfo> jedisShardInfoList = new ArrayList<>();
86         JedisShardInfo jedisShardInfo = new JedisShardInfo(properties.getHost(), properties.getPort());
87         jedisShardInfo.setPassword(properties.getPassword());
88         jedisShardInfoList.add(jedisShardInfo);
89         return new ShardedJedisPool(jedisPoolConfig, jedisShardInfoList);
90     }
91 
92 
93 }

基本的配置就完成了,这里我们再配置一个工具类用于常规的redis map的操作

package com.hdkj.redis.utils;

import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;

/**
 * @author xuminzhe
 * @version V1.0
 * @Project study
 * @Package com.hdkj.redis.utils
 * @Description redis工具类
 * @Date 2017/12/4
 */
@Component("mapRedisTemplate")
public class MapRedisTemplate {

    @Resource(name = "redisTemplate")
    private RedisTemplate<String,Object> redisTemplate;

    public Boolean hasKey(String key, Object hashKey) {
        HashOperations<String, Object, Object> ops = redisTemplate.opsForHash();
        return ops.hasKey(key, hashKey);
    }

    public Boolean delete(String key) {
        HashOperations<String, Object, Object> ops = redisTemplate.opsForHash();
        Long l = ops.delete(key);
        return l == 1 ? Boolean.TRUE : Boolean.FALSE;
    }

    public Boolean delete(String key, Object hashKey) {
        HashOperations<String, Object, Object> ops = redisTemplate.opsForHash();
        Long l = ops.delete(key, hashKey);
        return l > 0 ? Boolean.TRUE : Boolean.FALSE;
    }

    public Object get(String key, Object hashKey) {
        HashOperations<String, Object, Object> ops = redisTemplate.opsForHash();
        return ops.get(key, hashKey);
    }

    public Map<Object, Object> entries(String key) {
        HashOperations<String, Object, Object> ops = redisTemplate.opsForHash();
        return ops.entries(key);
    }

    public Set<Object> keys(String key) {
        HashOperations<String, Object, Object> ops = redisTemplate.opsForHash();
        return ops.keys(key);
    }

    public List<Object> values(String key) {
        HashOperations<String, Object, Object> ops = redisTemplate.opsForHash();
        return ops.values(key);
    }

    public Long size(String key) {
        HashOperations<String, Object, Object> ops = redisTemplate.opsForHash();
        return ops.size(key);
    }

    public void put(String key, Object hashKey, Object value) {

        HashOperations<String, Object, Object> ops = redisTemplate.opsForHash();
        ops.put(key, hashKey, value);
    }

    public void put(String key, Object hashKey, Object value,Long expireIn) {
        HashOperations<String, Object, Object> ops = redisTemplate.opsForHash();
        ops.put(key, hashKey, value);
        redisTemplate.expire(key, expireIn, TimeUnit.MILLISECONDS);
    }

    public void putIfAbsent(String key, Object hashKey, Object value) {
        HashOperations<String, Object, Object> ops = redisTemplate.opsForHash();
        ops.putIfAbsent(key, hashKey, value);
    }

}

 

好了,这就是一个简单的redis项目的配置。

===================================================================

有个问题还没解决:其他模块在调用redis模块,读取的是自己本身模块路劲下的配置文件信息,所以在调用的时候老是会失败,我暂时的处理方式是把配置信息写到  其他模块中。但是这种方式太不优雅了,有哪位朋友遇到相同情况的可以交流下。

Springboot redis 整合

标签:调用   contex   ini   clust   pairs   depend   temp   seconds   str   

原文地址:http://www.cnblogs.com/xmzJava/p/8028312.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!