1、其实在springboot框架中,已经自己整合redis,可以直接使用
最终咱们自己根据springboot整合redis的源码来实现自己的redis
因为springboot自带的redis已经非常完善,一旦redis报错的时候,错误信息很难找到!!!
所以需要自己去配置springboot整合的redis
—>如果redis报错,就可以非常精准的定位到具体的错误信息以及代码的行数
2、创建一个maven工程
3、导入所需要的jar包
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.10.RELEASE</version>
</parent>
<dependencies>
<!--
springboot-starter-web
-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--
springboot-mybatis整合包
-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.0</version>
</dependency>
<!--
mysql的驱动包
-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.38</version>
</dependency>
<!--
druid连接池
-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.10</version>
</dependency>
<!--
redis的jar包以及jedis的jar包
-->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
</dependency>
<!--
redis和springboot的整合包
-->
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
</dependency>
<!--
fastjson包
-->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.8.1</version>
</dependency>
<!--
添加springboot的进程jar包
里面包含了properties文件的读取(其实就是包含了@ConfigurationProperties()注解)
-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
4、在resources目录下创建一个config包
在config包中创建application.properties文件
在application.properties文件中自定义redis的配置
#配置redis集群的端口号和ip
spring.redis.nodes=192.168.23.138:6380,192.168.23.138:6381,192.168.23.138:6382,192.168.23.138:6383,192.168.23.138:6384,192.168.23.138:6385
#配置了redis的最大连接数
spring.redis.maxAttempts=1000
#配置了redis最大超时时间
spring.redis.commandTimeout=500000
#配置了redis的失效时间
spring.redis.expire=1000
5、src目录下创建config包,在包中创建RedisProperties类
在该类中定义与application.properties文件中自定义redis的配置的名字一样的属性
@Component:把RedisProperties作为spring的一个组件
作用就是把redis所需要配置和连接信息封装进RedisProperties类中
该组件为可拆卸的(面向组件的编程思想)
配置该组件类的作用就是从application.properties文件中读取自定义的属性信息
@ConfigurationProperties:作用是把application.properties中的属性读进RedisProperties类中
当使用到该注解的时候必须要使用spring-boot-configuration-processor的jar包
prefix = “spring.redis”:作用就是选中在application.properties文件中所有以spring.redis开头的配置信息
RedisProperties类中的所有属性必须要和application.properties文件中属性名进行对应(必须要一致)
注意:在其他类调用这个类中的属性的时候,不能进行修改,只能读取数据
@Component
@ConfigurationProperties(prefix = "spring.redis")
public class RedisProperties {
/**
* redis集群的节点(ip地址和端口号)
*/
private String nodes;
/**
* redis的最大连接数
*/
private String maxAttempts;
/**
* redis的连接最大超时时间
*/
private String commandTimeout;
/**
* redis的失效时间
*/
private String expire;
public String getNodes() {
return nodes;
}
public void setNodes(String nodes) {
this.nodes = nodes;
}
public String getMaxAttempts() {
return maxAttempts;
}
public void setMaxAttempts(String maxAttempts) {
this.maxAttempts = maxAttempts;
}
public String getCommandTimeout() {
return commandTimeout;
}
public void setCommandTimeout(String commandTimeout) {
this.commandTimeout = commandTimeout;
}
public String getExpire() {
return expire;
}
public void setExpire(String expire) {
this.expire = expire;
}
}
6、在config包下创建RedisConfiguration类
通过@SpringBootApplication或@Configuration注解把RedisConfiguration类标识为springboot的配置类
使用Jedis的jar包操作redis集群数据库
1.Redis:标识了redis是单节点模式(只有一台redis)
2.RedisCluster:标识了redis的集群模式
@SpringBootApplication
public class RedisConfiguration {
@Autowired
private RedisProperties redisProperties;
@Bean
public JedisCluster getJedisCluster(){
//1、获取到application.properties中配置的redis集群的端口号和ip
String nodes = redisProperties.getNodes();
//2、使用split进行分割nodes(以","分割)
String[] nodesArray = nodes.split(",");
//3.创建一个Set集合,以HostandPort对象作为泛型
Set<HostAndPort> hostAndPortSet = new HashSet<HostAndPort>();
//4、遍历nodesArray
for (String node : nodesArray) {
//5.使用split进行分割node(以":"为分隔符)
String[] hostAndprotArray = node.split(":");
//6、创建HostAndPort对象
HostAndPort hostAndPort = new HostAndPort(hostAndprotArray[0],
Integer.parseInt(hostAndprotArray[1]));
//7、把每一个HostAndPort对象装进set集合中
hostAndPortSet.add(hostAndPort);
}
//8、返回JedisCluster对象
return new JedisCluster(hostAndPortSet, Integer.parseInt(redisProperties.getCommandTimeout()),
Integer.parseInt(redisProperties.getMaxAttemts()));
}
}
7、在src目录下创建service包
8、在service包中创建 RedisService类
这里需要 @Autowired JedisCluster --> 因为需要调用JedisCluster(对redis数据库中进行增删改查)
@Service
public class RedisService {
@Autowired
private JedisCluster jedisCluster;
/**
* @author zxz
* @description
* 通过key获取redis中的数据
* @param [key]
* @date 2019/8/28
* @return java.lang.String
* @throws
**/
public String get(String key){
return jedisCluster.get(key);
}
/**
* @author zxz
* @description
* 向redis数据库中存入String类型的数据
* set的时候返回值是"OK"
* @param [key, value]
* @date 2019/8/28
* @return java.lang.String
* @throws
**/
public String set(String key,String value){
return jedisCluster.set(key, value);
}
/**
* @author zxz
* @description
* 通过key删除redis集群中的数据
* @param [key]
* @date 2019/8/28
* @return java.lang.String
* @throws
**/
public Long del(String key){
return jedisCluster.del(key);
}
/**
* @author zxz
* @description
* 通过key设置失效时间(单位是秒)
* @param [key, seconds]
* @date 2019/8/28
* @return java.lang.String
* @throws
**/
public Long expire(String key,Integer seconds){
return jedisCluster.expire(key, seconds);
}
}