Skip to content

Commit f2c7644

Browse files
author
liqiangqiang
committed
Spring Boot WebFlux 整合 Redis
1 parent 1975b9b commit f2c7644

File tree

5 files changed

+191
-0
lines changed

5 files changed

+191
-0
lines changed

springboot-webflux-6-redis/pom.xml

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
6+
<groupId>springboot</groupId>
7+
<artifactId>springboot-webflux-6-redis</artifactId>
8+
<version>0.0.1-SNAPSHOT</version>
9+
<name>springboot-webflux-6-redis :: Spring Boot WebFlux 整合 Redis</name>
10+
11+
<!-- Spring Boot 启动父依赖 -->
12+
<parent>
13+
<groupId>org.springframework.boot</groupId>
14+
<artifactId>spring-boot-starter-parent</artifactId>
15+
<version>2.0.1.RELEASE</version>
16+
</parent>
17+
18+
<dependencies>
19+
20+
<!-- Spring Boot Web Flux 依赖 -->
21+
<dependency>
22+
<groupId>org.springframework.boot</groupId>
23+
<artifactId>spring-boot-starter-webflux</artifactId>
24+
</dependency>
25+
26+
<!-- Spring Boot 响应式 Redis 依赖 -->
27+
<dependency>
28+
<groupId>org.springframework.boot</groupId>
29+
<artifactId>spring-boot-starter-data-redis-reactive</artifactId>
30+
</dependency>
31+
32+
<!-- Spring Boot Test 依赖 -->
33+
<dependency>
34+
<groupId>org.springframework.boot</groupId>
35+
<artifactId>spring-boot-starter-test</artifactId>
36+
<scope>test</scope>
37+
</dependency>
38+
39+
<!-- Junit -->
40+
<dependency>
41+
<groupId>junit</groupId>
42+
<artifactId>junit</artifactId>
43+
<version>4.12</version>
44+
</dependency>
45+
</dependencies>
46+
47+
</project>
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package org.spring.springboot;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
6+
/**
7+
* Spring Boot 应用启动类
8+
*
9+
*/
10+
// Spring Boot 应用的标识
11+
@SpringBootApplication
12+
public class Application {
13+
14+
public static void main(String[] args) {
15+
// 程序启动入口
16+
SpringApplication.run(Application.class,args);
17+
}
18+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package org.spring.springboot.domain;
2+
3+
import org.springframework.data.annotation.Id;
4+
5+
import java.io.Serializable;
6+
7+
/**
8+
* 城市实体类
9+
*
10+
*/
11+
public class City implements Serializable {
12+
13+
private static final long serialVersionUID = -2081742442561524068L;
14+
15+
/**
16+
* 城市编号
17+
*/
18+
@Id
19+
private Long id;
20+
21+
/**
22+
* 省份编号
23+
*/
24+
private Long provinceId;
25+
26+
/**
27+
* 城市名称
28+
*/
29+
private String cityName;
30+
31+
/**
32+
* 描述
33+
*/
34+
private String description;
35+
36+
public Long getId() {
37+
return id;
38+
}
39+
40+
public void setId(Long id) {
41+
this.id = id;
42+
}
43+
44+
public Long getProvinceId() {
45+
return provinceId;
46+
}
47+
48+
public void setProvinceId(Long provinceId) {
49+
this.provinceId = provinceId;
50+
}
51+
52+
public String getCityName() {
53+
return cityName;
54+
}
55+
56+
public void setCityName(String cityName) {
57+
this.cityName = cityName;
58+
}
59+
60+
public String getDescription() {
61+
return description;
62+
}
63+
64+
public void setDescription(String description) {
65+
this.description = description;
66+
}
67+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package org.spring.springboot.webflux.controller;
2+
3+
import org.spring.springboot.domain.City;
4+
import org.springframework.beans.factory.annotation.Autowired;
5+
import org.springframework.data.redis.core.RedisTemplate;
6+
import org.springframework.data.redis.core.ValueOperations;
7+
import org.springframework.web.bind.annotation.*;
8+
import reactor.core.publisher.Mono;
9+
10+
import java.util.concurrent.TimeUnit;
11+
12+
@RestController
13+
@RequestMapping(value = "/city")
14+
public class CityWebFluxController {
15+
16+
@Autowired
17+
private RedisTemplate redisTemplate;
18+
19+
@GetMapping(value = "/{id}")
20+
public Mono<City> findCityById(@PathVariable("id") Long id) {
21+
String key = "city_" + id;
22+
ValueOperations<String, City> operations = redisTemplate.opsForValue();
23+
boolean hasKey = redisTemplate.hasKey(key);
24+
City city = operations.get(key);
25+
26+
if (!hasKey) {
27+
return Mono.create(monoSink -> monoSink.success(null));
28+
}
29+
return Mono.create(monoSink -> monoSink.success(city));
30+
}
31+
32+
@PostMapping()
33+
public Mono<City> saveCity(@RequestBody City city) {
34+
String key = "city_" + city.getId();
35+
ValueOperations<String, City> operations = redisTemplate.opsForValue();
36+
operations.set(key, city, 60, TimeUnit.SECONDS);
37+
38+
return Mono.create(monoSink -> monoSink.success(city));
39+
}
40+
41+
@DeleteMapping(value = "/{id}")
42+
public Mono<Long> deleteCity(@PathVariable("id") Long id) {
43+
String key = "city_" + id;
44+
boolean hasKey = redisTemplate.hasKey(key);
45+
if (hasKey) {
46+
redisTemplate.delete(key);
47+
}
48+
return Mono.create(monoSink -> monoSink.success(id));
49+
}
50+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
## Redis 配置
2+
## Redis服务器地址
3+
spring.redis.host=127.0.0.1
4+
## Redis服务器连接端口
5+
spring.redis.port=6379
6+
## Redis服务器连接密码(默认为空)
7+
spring.redis.password=
8+
# 连接超时时间(毫秒)
9+
spring.redis.timeout=5000

0 commit comments

Comments
 (0)