diff --git a/springboot-webflux-6-redis/pom.xml b/springboot-webflux-6-redis/pom.xml new file mode 100755 index 0000000..8f68262 --- /dev/null +++ b/springboot-webflux-6-redis/pom.xml @@ -0,0 +1,47 @@ + + + 4.0.0 + + springboot + springboot-webflux-6-redis + 0.0.1-SNAPSHOT + springboot-webflux-6-redis :: Spring Boot WebFlux 整合 Redis + + + + org.springframework.boot + spring-boot-starter-parent + 2.0.1.RELEASE + + + + + + + org.springframework.boot + spring-boot-starter-webflux + + + + + org.springframework.boot + spring-boot-starter-data-redis-reactive + + + + + org.springframework.boot + spring-boot-starter-test + test + + + + + junit + junit + 4.12 + + + + diff --git a/springboot-webflux-6-redis/src/main/java/org/spring/springboot/Application.java b/springboot-webflux-6-redis/src/main/java/org/spring/springboot/Application.java new file mode 100644 index 0000000..718c100 --- /dev/null +++ b/springboot-webflux-6-redis/src/main/java/org/spring/springboot/Application.java @@ -0,0 +1,18 @@ +package org.spring.springboot; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +/** + * Spring Boot 应用启动类 + * + */ +// Spring Boot 应用的标识 +@SpringBootApplication +public class Application { + + public static void main(String[] args) { + // 程序启动入口 + SpringApplication.run(Application.class,args); + } +} diff --git a/springboot-webflux-6-redis/src/main/java/org/spring/springboot/domain/City.java b/springboot-webflux-6-redis/src/main/java/org/spring/springboot/domain/City.java new file mode 100644 index 0000000..d6a4668 --- /dev/null +++ b/springboot-webflux-6-redis/src/main/java/org/spring/springboot/domain/City.java @@ -0,0 +1,67 @@ +package org.spring.springboot.domain; + +import org.springframework.data.annotation.Id; + +import java.io.Serializable; + +/** + * 城市实体类 + * + */ +public class City implements Serializable { + + private static final long serialVersionUID = -2081742442561524068L; + + /** + * 城市编号 + */ + @Id + private Long id; + + /** + * 省份编号 + */ + private Long provinceId; + + /** + * 城市名称 + */ + private String cityName; + + /** + * 描述 + */ + private String description; + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public Long getProvinceId() { + return provinceId; + } + + public void setProvinceId(Long provinceId) { + this.provinceId = provinceId; + } + + public String getCityName() { + return cityName; + } + + public void setCityName(String cityName) { + this.cityName = cityName; + } + + public String getDescription() { + return description; + } + + public void setDescription(String description) { + this.description = description; + } +} diff --git a/springboot-webflux-6-redis/src/main/java/org/spring/springboot/webflux/controller/CityWebFluxController.java b/springboot-webflux-6-redis/src/main/java/org/spring/springboot/webflux/controller/CityWebFluxController.java new file mode 100644 index 0000000..abd4983 --- /dev/null +++ b/springboot-webflux-6-redis/src/main/java/org/spring/springboot/webflux/controller/CityWebFluxController.java @@ -0,0 +1,50 @@ +package org.spring.springboot.webflux.controller; + +import org.spring.springboot.domain.City; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.redis.core.RedisTemplate; +import org.springframework.data.redis.core.ValueOperations; +import org.springframework.web.bind.annotation.*; +import reactor.core.publisher.Mono; + +import java.util.concurrent.TimeUnit; + +@RestController +@RequestMapping(value = "/city") +public class CityWebFluxController { + + @Autowired + private RedisTemplate redisTemplate; + + @GetMapping(value = "/{id}") + public Mono findCityById(@PathVariable("id") Long id) { + String key = "city_" + id; + ValueOperations operations = redisTemplate.opsForValue(); + boolean hasKey = redisTemplate.hasKey(key); + City city = operations.get(key); + + if (!hasKey) { + return Mono.create(monoSink -> monoSink.success(null)); + } + return Mono.create(monoSink -> monoSink.success(city)); + } + + @PostMapping() + public Mono saveCity(@RequestBody City city) { + String key = "city_" + city.getId(); + ValueOperations operations = redisTemplate.opsForValue(); + operations.set(key, city, 60, TimeUnit.SECONDS); + + return Mono.create(monoSink -> monoSink.success(city)); + } + + @DeleteMapping(value = "/{id}") + public Mono deleteCity(@PathVariable("id") Long id) { + String key = "city_" + id; + boolean hasKey = redisTemplate.hasKey(key); + if (hasKey) { + redisTemplate.delete(key); + } + return Mono.create(monoSink -> monoSink.success(id)); + } +} diff --git a/springboot-webflux-6-redis/src/main/resources/application.properties b/springboot-webflux-6-redis/src/main/resources/application.properties new file mode 100644 index 0000000..c3885f6 --- /dev/null +++ b/springboot-webflux-6-redis/src/main/resources/application.properties @@ -0,0 +1,9 @@ +## Redis 配置 +## Redis服务器地址 +spring.redis.host=127.0.0.1 +## Redis服务器连接端口 +spring.redis.port=6379 +## Redis服务器连接密码(默认为空) +spring.redis.password= +# 连接超时时间(毫秒) +spring.redis.timeout=5000