SpringBoot项目集成Redis的方式详解
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
SpringBoot项⽬集成Redis的⽅式详解
集成⽅式
使⽤Jedis
Jedis是Redis官⽅推荐的⾯向Java的操作Redis的客户端,是对服务端直连后进⾏操作。
如果直接使⽤Jedis进⾏连接,多线程环境下是⾮线程安全的,正式⽣产环境⼀般使⽤连接池进⾏连接。
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
</dependency>
使⽤spring-data-redis
由Spring 框架提供,是对Redis客户端的进⼀步封装,屏蔽了不同客户端的不同实现⽅式,让服务端和客户端进⼀步解耦;也就是你可以切换不同的客户端实现,⽐如Jedis或Lettuce(Redis客户端实现之⼀),⽽不影响你的业务逻辑。
类似于的SpringCloud的服务治理框架对不同服务治理组件的适配,或是AMQP
它利⽤RedisTemplate对JedisApi进⾏⾼度封装。
使⽤的依赖如下:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
Redis的安装
收先要安装Redis服务端,Redis官⽅提供的是Linux安装包。
⽹上有很多详细的安装教程,这⾥不做展开。
关于Windows下的安装,可参考我的另⼀篇博⽂
绑定配置
完成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> T get(String key, Class<T> clazz) {
Jedis jedis = null;
try {
jedis = jedisPool.getResource();
String str = jedis.get(key);
return stringToBean(str,clazz);
} finally {
close(jedis);
}
}
public <T> void set(String key, T value) {
try {
String str = value.toString();
if (str == null || str.length() <= 0) {
return;
}
myJedis.set(key, str);
} finally {
close(myJedis);
}
}
private void close(Jedis jedis) {
if (jedis != null) {
jedis.close();
}
}
/**
* 把⼀个字符串转换成bean对象
* @param str
* @param <T>
* @return
*/
public static <T> T stringToBean(String str, Class<T> clazz) {
if(str == null || str.length() <= 0 || clazz == null) {
return null;
}
if(clazz == int.class || clazz == Integer.class) {
return (T)Integer.valueOf(str);
}else if(clazz == String.class) {
return (T)str;
}else if(clazz == long.class || clazz == Long.class) {
return (T)Long.valueOf(str);
}else {
return JSON.toJavaObject(JSON.parseObject(str), clazz);
}
}
}
其中get⽅法使⽤连接池中的客户端实例,set⽅法⽤到的是⾮连接池的实例,以区分两种不同的使⽤⽅式
使⽤
封装好的Redis的操作⼯具类后,我们就可以直接使⽤该⼯具类来进⾏对Redis的各种操作。
如下,直接注⼊即可。
@RestController
public class TestController {
@Autowired
private RedisService redisService;
......
}
到此这篇关于Spring Boot 项⽬集成Redis的⽂章就介绍到这了,更多相关Spring Boot 项⽬集成Redis内容请搜索以前的⽂章或继续浏览下⾯的相关⽂章希望⼤家以后多多⽀持!。