# springboot-redis
**Repository Path**: coderrui/springboot-redis
## Basic Information
- **Project Name**: springboot-redis
- **Description**: SpringBoot2.1.6 集成 Redis Cluster
- **Primary Language**: Java
- **License**: Not specified
- **Default Branch**: master
- **Homepage**: None
- **GVP Project**: No
## Statistics
- **Stars**: 1
- **Forks**: 0
- **Created**: 2020-05-23
- **Last Updated**: 2021-03-19
## Categories & Tags
**Categories**: Uncategorized
**Tags**: None
## README
# 5.【多级缓存架构】库存服务开发环境整合和搭建:springboot+mybatis+jenkins
* 知识拓展
## 1. 创建 eshop-parent 父工程
### 1.1 pom.xml
```xml
4.0.0
org.springframework.boot
spring-boot-starter-parent
2.1.6.RELEASE
com.roncoo.eshop.parent
eshop-parent
1.0.0-SNAPSHOT
eshop-parent
UTF-8
1.8
1.8
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-test
test
org.junit.vintage
junit-vintage-engine
org.springframework.boot
spring-boot-maven-plugin
```
## 2. 创建 eshop-inventory 库存服务子模块
### 2.1 pom.xml
```xml
4.0.0
com.roncoo.eshop.parent
eshop-parent
1.0.0-SNAPSHOT
com.roncoo.eshop.inventory
eshop-inventory
0.0.1-SNAPSHOT
eshop-inventory
Demo project for Spring Boot
1.8
org.mybatis.spring.boot
mybatis-spring-boot-starter
2.1.1
mysql
mysql-connector-java
5.1.46
com.alibaba
fastjson
1.2.46
org.springframework.boot
spring-boot-starter-data-redis
org.springframework.boot
spring-boot-maven-plugin
```
### 2.2 application.yml
```yml
server:
port: 8080
spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/eshop
username: root
password: root
redis: # redis cluster配置
jedis:
pool:
max-active: 8
max-wait: -1
max-idle: 8
min-idle: 0
timeout: 10000
cluster:
nodes:
- 192.168.0.102:7001
- 192.168.0.102:7002
- 192.168.0.103:7003
- 192.168.0.103:7004
- 192.168.0.104:7005
- 192.168.0.104:7006
database: 0
mybatis:
mapper-locations: classpath:mybatis/mapper/*.xml
config-location: classpath:mybatis/config/mybatis-config.xml
```
### 2.3 mybatis 配置文件 mybatis-config.xml
```xml
```
### 2.4 mybatis 数据库映射文件 UserMapper.xml
```xml
```
### 2.5 创建 JedisConfigProperties类
```java
package com.springboot.redis.config;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
public class JedisConfigProperties {
@Value("${spring.redis.jedis.pool.max-active}")
private Integer maxActive;
@Value("${spring.redis.jedis.pool.max-wait}")
private Integer maxWait;
@Value("${spring.redis.jedis.pool.max-idle}")
private Integer maxIdle;
@Value("${spring.redis.jedis.pool.min-idle}")
private Integer minIdle;
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 getMaxIdle() {
return maxIdle;
}
public void setMaxIdle(Integer maxIdle) {
this.maxIdle = maxIdle;
}
public Integer getMinIdle() {
return minIdle;
}
public void setMinIdle(Integer minIdle) {
this.minIdle = minIdle;
}
}
```
### 2.6 创建 RedisClusterConfigProperties 类
```java
package com.springboot.redis.config;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import java.util.List;
@Component
@ConfigurationProperties(prefix = "spring.redis.cluster")
public class RedisClusterConfigProperties {
private List nodes;
private Integer maxRedirects = 3;
public List getNodes() {
return nodes;
}
public void setNodes(List nodes) {
this.nodes = nodes;
}
public Integer getMaxRedirects() {
return maxRedirects;
}
public void setMaxRedirects(Integer maxRedirects) {
this.maxRedirects = maxRedirects;
}
}
```
### 2.7 创建 RedisClusterConfig 类
```java
package com.springboot.redis.config;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisClusterConfiguration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import redis.clients.jedis.JedisPoolConfig;
@Configuration
public class RedisClusterConfig {
@Autowired
private JedisConfigProperties jedisConfigProperties;
@Autowired
private RedisClusterConfigProperties redisClusterConfigProperties;
@Bean
public RedisClusterConfiguration getRedisClusterConfiguration() {
RedisClusterConfiguration redisClusterConfiguration = new RedisClusterConfiguration(redisClusterConfigProperties.getNodes());
redisClusterConfiguration.setMaxRedirects(redisClusterConfigProperties.getMaxRedirects());
return redisClusterConfiguration;
}
@Bean
public RedisConnectionFactory connectionFactory(JedisConfigProperties clusterConfigurationProperties) {
// 设置jedis客户端配置
JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
jedisPoolConfig.setMaxIdle(jedisConfigProperties.getMaxIdle());
jedisPoolConfig.setMinIdle(jedisConfigProperties.getMinIdle());
jedisPoolConfig.setMaxWaitMillis(jedisConfigProperties.getMaxWait());
jedisPoolConfig.setMaxTotal(jedisConfigProperties.getMaxActive());
return new JedisConnectionFactory(
new RedisClusterConfiguration(redisClusterConfigProperties.getNodes()), jedisPoolConfig);
}
// 1.项目启动时此方法先被注册成bean被spring管理,
// 如果没有这个bean,
// 则redis可视化工具中的中文内容(key或者value)都会以二进制存储,不易检查。
@Bean
public RedisTemplate redisTemplate(RedisConnectionFactory redisConnectionFactory) {
RedisTemplate template = new RedisTemplate();
template.setConnectionFactory(redisConnectionFactory);
Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
jackson2JsonRedisSerializer.setObjectMapper(objectMapper);
StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
// key采用String的序列化方式
template.setKeySerializer(stringRedisSerializer);
// hash的key也采用String的序列化方式
template.setHashKeySerializer(stringRedisSerializer);
// value序列化方式采用jackson
template.setValueSerializer(jackson2JsonRedisSerializer);
// hash的value序列化方式采用jackson
template.setHashValueSerializer(jackson2JsonRedisSerializer);
template.afterPropertiesSet();
return template;
}
}
```
### 2.8 创建RedisService接口
```java
package com.roncoo.eshop.inventory.service.rediscluster;
/**
* @Description:
* @Auther: zrblog
* @CreateTime: 2019-11-19 07:24
* @Version:v1.0
*/
public interface RedisService {
/**
* 功能描述: 设置key-value
*
* @Author: zr
* @Date: 2019/11/20
* @param key
* @param value
* @return: void
*/
void setValue(String key, String value);
/*
*功能描述 :根据key获取value
*
* @author zr
* @date 2019/11/20
* @param key
* @return java.lang.String
*/
String getValue(String key);
}
```
### 2.9 创建RedisServiceImpl实现类
```java
package com.roncoo.eshop.inventory.service.rediscluster.impl;
import com.roncoo.eshop.inventory.service.rediscluster.RedisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
/**
* @Description:
* @Auther: zrblog
* @CreateTime: 2019-11-19 07:25
* @Version:v1.0
*/
@Service
public class RedisServiceImpl implements RedisService {
@Autowired
private RedisTemplate redisTemplate;
@Override
public void setValue(String key, String value) {
redisTemplate.opsForValue().set(key, value);
}
@Override
public String getValue(String key) {
return (String) redisTemplate.opsForValue().get(key);
}
}
```
### 2.10 创建RedisController控制层类
```java
package com.roncoo.eshop.inventory.controller.redis;
import com.roncoo.eshop.inventory.service.rediscluster.RedisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
/**
* @Description:
* @Auther: zrblog
* @CreateTime: 2019-11-19 07:37
* @Version:v1.0
*/
@Controller
@RequestMapping("/redis")
public class RedisController {
@Autowired
private RedisService redisService;
@RequestMapping("/set")
@ResponseBody
public String setValue(@RequestParam("key") String key, @RequestParam("value") String value) {
redisService.setValue(key, value);
return "success";
}
@RequestMapping("/get")
@ResponseBody
public String getValue(@RequestParam("key") String key) {
String result = redisService.getValue(key);
return result;
}
}
```
### 2.11 测试结果
* 查询Redis cluster 中 key1 是否有值
* 请求接口:设置key1值为value1
* 查看Redis cluster 中key1 是否有值

