Spring Boot 项目集成Redis的方式详解
0浏览
收藏
在数据库实战开发的过程中,我们经常会遇到一些这样那样的问题,然后要卡好半天,等问题解决了才发现原来一些细节知识点还是没有掌握好。今天golang学习网就整理分享《Spring Boot 项目集成Redis的方式详解》,聊聊Redis、springboot、集成,希望可以帮助到正在努力赚钱的你。
集成方式
使用Jedis
Jedis是Redis官方推荐的面向Java的操作Redis的客户端,是对服务端直连后进行操作。如果直接使用Jedis进行连接,多线程环境下是非线程安全的,正式生产环境一般使用连接池进行连接。
redis.clients
绑定配置
完成Redis服务端的安装之后,我们开始在项目中进行集成。这里我们先介绍使用Jedis的方式进行的集成。先按上面的提及的方式进行依赖的引入。然后将Redis的相关信息配置到配置文件中去。我们可以的新建一个配置文件redis.properties,内容如下:
# 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.timeout=0
接下来我们要为Redis客户端连接绑定上面的配置,创建出来的客户端实例才能够连接到我们的想连的Redis服务端。你可以使用@Value注解或@ConfigurationProperties注解的方式,本文采用的是后者,如果还不清楚的该注解的用法,可以移步我的另一篇博文查看,这里不做展开。
以下是Redis服务端信息配置的接收类:MyRedisProperties.java
@ConfigurationProperties(
prefix = "spring.redis"
)
@Component
@Data
@PropertySource("classpath:/redis.properties")
public class MyRedisProperties {
private String database;
private String host;
private Integer port;
private String password;
private Integer timeOut;
}
由于我们正式生产环境一般都是采用连接池方式实现,所以我们还需要关于连接池的配置如下:
# 连接池最大连接数(使用负值表示没有限制)
spring.redis.pool.max-active=8
# 连接池最大阻塞等待时间(使用负值表示没有限制)
spring.redis.pool.max-wait=-1
# 连接池中的最大空闲连接
spring.redis.pool.max-idle=8
# 连接池中的最小空闲连接
spring.redis.pool.min-idle=0
对应的接收类如下:
@ConfigurationProperties(
prefix = "spring.redis.pool"
)
@Data
@Component
@PropertySource("classpath:/redis.properties")
public class RedisPoolProperties {
private Integer maxActive;
private Integer maxWait;
private Integer maxIdle;
private Integer minIdle;
}
然后向Spring容器装配客户端实例,分为单个客户端和连接池两种实现,如下代码:
@Configuration
public class RedisConfig {
@Autowired
private RedisPoolProperties redisPoolProperties;
@Autowired
private MyRedisProperties myRedisProperties;
@Bean
public Jedis singleJedis(){
return new Jedis(myRedisProperties.getHost(),myRedisProperties.getPort());
}
@Bean
public JedisPool jedisPool(){
JedisPoolConfig poolConfig = new JedisPoolConfig();
poolConfig.setMaxIdle(redisPoolProperties.getMaxIdle());
poolConfig.setMaxTotal(redisPoolProperties.getMaxActive());
poolConfig.setMaxWaitMillis(redisPoolProperties.getMaxWait() * 1000);
JedisPool jp = new JedisPool(poolConfig, myRedisProperties.getHost(), myRedisProperties.getPort(),
myRedisProperties.getTimeOut()*1000, myRedisProperties.getPassword(), 0);
return jp;
}
}
获取Redis客户端
进行相关配置的绑定之后,意味着我们程序可以拿到Redis和连接池的相关信息,然后进行客户端的创建和连接了。所以我们要向Spring容器装配客户端实例,分为单个客户端和连接池两种实现,如下代码:
@Configuration
public class RedisConfig {
@Autowired
private RedisPoolProperties redisPoolProperties;
@Autowired
private MyRedisProperties myRedisProperties;
@Bean
public Jedis singleJedis(){
return new Jedis(myRedisProperties.getHost(),myRedisProperties.getPort());
}
@Bean
public JedisPool jedisPool(){
JedisPoolConfig poolConfig = new JedisPoolConfig();
poolConfig.setMaxIdle(redisPoolProperties.getMaxIdle());
poolConfig.setMaxTotal(redisPoolProperties.getMaxActive());
poolConfig.setMaxWaitMillis(redisPoolProperties.getMaxWait() * 1000);
JedisPool jp = new JedisPool(poolConfig, myRedisProperties.getHost(), myRedisProperties.getPort(),
myRedisProperties.getTimeOut()*1000, myRedisProperties.getPassword(), 0);
return jp;
}
}
Redis工具的编写
装配好客户端实例后,我们就可以通过@Autowired的方式进行注入使用了。我们都知道,Redis有5中数据类型,分别是:
- string(字符串)
- hash(哈希)
- list(列表)
- set(集合)
- zset(sorted set:有序集合)
所以的有必要的封装一个操作者5种数据列表的工具类,由于篇幅的关系,我们以Redis最基本的数据类型String为例,简单封装几个操作方法作为示例如下,更详细的封装,可参考这一博文
@Service
public class RedisService {
@Autowired
private JedisPool jedisPool; // 连接池方式
@Autowired
private Jedis myJedis; // 单个客户端
public
T get(String key, Class
clazz) { Jedis jedis = null; try { jedis = jedisPool.getResource(); String str = jedis.get(key); return stringToBean(str,clazz); } finally { close(jedis); } } public
void set(String key, T value) { try { String str = value.toString(); if (str == null || str.length() * @return */ public static
T stringToBean(String str, Class
clazz) { if(str == null || str.length()
其中get方法使用连接池中的客户端实例,set方法用到的是非连接池的实例,以区分两种不同的使用方式
使用
封装好的Redis的操作工具类后,我们就可以直接使用该工具类来进行对Redis的各种操作 。如下,直接注入即可。
@RestController
public class TestController {
@Autowired
private RedisService redisService;
......
}
以上就是本文的全部内容了,是否有顺利帮助你解决问题?若是能给你带来学习上的帮助,请大家多多支持golang学习网!更多关于数据库的相关知识,也可关注golang学习网公众号。
版本声明 本文转载于:脚本之家 如有侵犯,请联系 删除
- 详解Redis数据类型实现原理
- Redis如何实现分布式锁
