springboot配置redis过程详解
作者:Zs夏至 时间:2022-10-13 12:41:30
在springboot中,默认继承好了一套完好的redis包,可以直接使用,但是如果使用中出了错不容易找到错误的原因,因此这里使用自己配置的redis;
需要使用的三个主要jar包:
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
使用spring-boot-configuration-processor包主要是用来配置加载文件
package com.zs.springboot.config.redis;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
/**
* @Company
* @Author Zs
* 将这个类作为spring的一个组件,添加@ConfigurationProperties(prefix = "spring.redis")注解,就会默认从application.properties
* 文件中加载前缀为spring.redis的配置信息,配置文件中的配置字段与该类中的属性一致,通过setter方法来设值
* @Date Create in 2019/8/30
**/
@Component
@ConfigurationProperties(prefix = "spring.redis")
public class RedisProperties {
private String ip;
private Integer[] ports;
private Integer maxActive;
private Integer maxWait;
private Integer expire;
public String getIp() {
return ip;
}
public void setIp(String ip) {
this.ip = ip;
}
public Integer[] getPorts() {
return ports;
}
public void setPorts(Integer[] ports) {
this.ports = ports;
}
public Integer getMaxActive() {
return maxActive;
}
public void setMaxActive(Integer maxActive) {
this.maxActive = maxActive;
}
public Integer getMaxWait() {
return maxWait;
}
public void setMaxWait(Integer maxWait) {
this.maxWait = maxWait;
}
public Integer getExpire() {
return expire;
}
public void setExpire(Integer expire) {
this.expire = expire;
}
}
在application中配置redis:
然后配置redis的配置类,使用jdisCluster来操作redis:
package com.zs.springboot.config.redis;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.JedisCluster;
import java.util.HashSet;
import java.util.Set;
/**
* @Company
* @Author Zs
* @Date Create in 2019/8/30
**/
@Configuration
public class RedisConfiguration {
private RedisProperties redisProperties;
public RedisConfiguration(RedisProperties redisProperties) {
this.redisProperties = redisProperties;
}
@Bean
public JedisCluster jedisCluster() {
Integer[] ports = redisProperties.getPorts();
String host = redisProperties.getIp();
Set<HostAndPort> hostAndPortSet = new HashSet<>();
for (Integer port : ports) {
hostAndPortSet.add(new HostAndPort(host, port));
}
return new JedisCluster(hostAndPortSet, redisProperties.getMaxActive(), redisProperties.getMaxWait());
}
}
编辑redis的增删该方法:
/**
* @Company
* @Author Zs
* @Date Create in 2019/8/28
**/
@Service
public class RedisService {
@Autowired
private JedisCluster jedisCluster;
private static final String SET_SUCCESS = "OK";
/**
* 添加string数据,成功返回code:200,失败code:404
* @param key
* @param value
* @return
*/
public Map<String, Object> set(String key, Object value) {
Map<String, Object> map = new HashMap<>();
String result = jedisCluster.set(key, JsonUtil.toJsonString(value));
if (SET_SUCCESS.equals(result)) {
map.put(Status.SUCCESS.getCodeName(), Status.SUCCESS.getCode());
} else {
map.put(Status.FILED.getCodeName(), Status.FILED.getCode());
}
return map;
}
/**
* 从redis根据key获取string数据
* @param key
* @return
*/
public String get(String key) {
String jsonString = jedisCluster.get(key);
if (jsonString==null || jsonString.equals("")) {
return null;
}
return jsonString;
}
/**
* 删除string数据
* @param key
* @return
*/
public Map<String, Object> del(String key) {
Map<String, Object> map = new HashMap<>();
Long del = jedisCluster.del(key);
if (del>0) {
map.put("code", 200);
} else {
map.put("code", 404);
}
return map;
}
/**
* 设置失效时间
* @param key
* @param seconds
* @return
*/
public Long expire(String key,Integer seconds) {
return jedisCluster.expire(key, seconds);
}
}
注意不能在service层中注入service,如果需要可以在controller层将redisService做为参数传递进去,如果在service层中注入其他的service对象,可能造成事务的串联,读到脏数据。
该方法需要使用到jsonUtil类,将数据转为json字符串存储
来源:https://www.cnblogs.com/Zs-book1/p/11451689.html
标签:spring,boot,配置,redis
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
详解在Spring中如何自动创建代理
2023-11-15 15:28:52
![](https://img.aspxhome.com/file/2023/2/59172_0s.png)
Java结构型设计模式中建造者模式示例详解
2023-04-24 13:37:31
![](https://img.aspxhome.com/file/2023/6/60286_0s.png)
VMware虚拟机下hadoop1.x的安装方法
2023-07-27 08:01:40
SpringBoot启动访问localhost:8080报错404的解决操作
2021-07-20 08:44:00
![](https://img.aspxhome.com/file/2023/0/64770_0s.jpg)
flutter TextField换行自适应的实现
2023-06-21 01:21:39
springboot项目启动,但是访问报404错误的问题
2022-09-21 10:30:10
![](https://img.aspxhome.com/file/2023/3/61253_0s.jpg)
Spring AOP实现接口请求记录到数据库的示例代码
2023-08-15 19:14:05
![](https://img.aspxhome.com/file/2023/4/58064_0s.png)
Java线程中的常见方法(start方法和run方法)
2023-11-16 17:41:32
![](https://img.aspxhome.com/file/2023/4/68694_0s.png)
Spring整合junit的配置过程图解
2022-12-18 16:37:48
![](https://img.aspxhome.com/file/2023/5/66975_0s.png)
Java二分查找算法实例详解
2022-07-09 14:33:55
Netty分布式pipeline管道创建方法跟踪解析
2023-11-03 02:55:51
![](https://img.aspxhome.com/file/2023/0/58890_0s.png)
java开发之Jdbc分页源码详解
2021-10-28 16:06:48
web 容器的设计如何实现
2022-04-07 00:29:51
![](https://img.aspxhome.com/file/2023/2/68772_0s.jpg)
浅试仿 mapstruct实现微服务编排框架详解
2022-07-12 13:20:44
![](https://img.aspxhome.com/file/2023/0/63420_0s.png)
详解Eclipse 字体、字号的设置、最佳字体推荐
2023-11-26 12:25:32
![](https://img.aspxhome.com/file/2023/9/60149_0s.png)
分享Java常用开发编辑器工具
2023-11-06 07:35:37
![](https://img.aspxhome.com/file/2023/6/58896_0s.png)
Java Collection集合iterator方法解析
2022-11-17 06:43:29
Android SharedPreferences数据存储详解
2023-08-05 13:32:47
Java 中String StringBuilder 与 StringBuffer详解及用法实例
2021-06-17 12:25:32
解析Java设计模式编程中命令模式的使用
2023-11-12 04:49:45
![](https://img.aspxhome.com/file/2023/3/59513_0s.jpg)