yufeng0528 4 years ago
parent
commit
9869dc7f66

+ 4 - 0
.gitignore

@@ -0,0 +1,4 @@
1
+/target/
2
+.settings/
3
+.classpath
4
+.project

+ 48 - 0
pom.xml

@@ -0,0 +1,48 @@
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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
4
+	<modelVersion>4.0.0</modelVersion>
5
+	
6
+	<groupId>com.yaozhitech</groupId>
7
+	<artifactId>spring5</artifactId>
8
+	<packaging>jar</packaging>
9
+	<version>0.1.0</version>
10
+	
11
+	<name>${project.artifactId}</name>
12
+    <description>spring5 demo</description>
13
+	
14
+	<!-- Spring Boot 启动父依赖 -->
15
+	<parent>
16
+        <groupId>org.springframework.boot</groupId>
17
+        <artifactId>spring-boot-starter-parent</artifactId>
18
+        <version>2.2.1.RELEASE</version>
19
+        <relativePath />
20
+    </parent>
21
+
22
+	<properties>
23
+		<maven.compiler.source>1.8</maven.compiler.source>
24
+		<maven.compiler.target>1.8</maven.compiler.target>
25
+		<maven-jar-plugin.version>3.1.1</maven-jar-plugin.version>
26
+	</properties>
27
+
28
+	<dependencies>
29
+		<!-- Spring Boot Web Flux 依赖 -->
30
+		<dependency>
31
+			<groupId>org.springframework.boot</groupId>
32
+			<artifactId>spring-boot-starter-webflux</artifactId>
33
+		</dependency>
34
+	
35
+		<!-- Spring Boot 响应式 Redis 依赖 -->
36
+		<dependency>
37
+			<groupId>org.springframework.boot</groupId>
38
+			<artifactId>spring-boot-starter-data-redis-reactive</artifactId>
39
+		</dependency>
40
+		
41
+		<!-- lombok依赖包 -->
42
+		<dependency>
43
+			<groupId>org.projectlombok</groupId>
44
+			<artifactId>lombok</artifactId>
45
+		</dependency>
46
+		
47
+	</dependencies>
48
+</project>

+ 13 - 0
src/main/java/com/yaozhitech/spring5/App.java

@@ -0,0 +1,13 @@
1
+package com.yaozhitech.spring5;
2
+
3
+import org.springframework.boot.SpringApplication;
4
+import org.springframework.boot.autoconfigure.SpringBootApplication;
5
+
6
+@SpringBootApplication
7
+public class App {
8
+
9
+	public static void main(String[] args) {
10
+        // 程序启动入口
11
+        SpringApplication.run(App.class,args);
12
+    }
13
+}

+ 54 - 0
src/main/java/com/yaozhitech/spring5/config/RedisConfig.java

@@ -0,0 +1,54 @@
1
+package com.yaozhitech.spring5.config;
2
+
3
+
4
+import org.springframework.cache.annotation.CachingConfigurerSupport;
5
+import org.springframework.cache.annotation.EnableCaching;
6
+import org.springframework.context.annotation.Bean;
7
+import org.springframework.context.annotation.Configuration;
8
+import org.springframework.data.redis.connection.RedisConnectionFactory;
9
+import org.springframework.data.redis.core.RedisTemplate;
10
+import org.springframework.data.redis.core.StringRedisTemplate;
11
+import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
12
+import org.springframework.data.redis.serializer.StringRedisSerializer;
13
+
14
+/**
15
+ * redis 配置
16
+ *
17
+ * @author yinzy
18
+ */
19
+@Configuration
20
+@EnableCaching
21
+public class RedisConfig extends CachingConfigurerSupport {
22
+	public static String cachePrefix = "bbc:";
23
+	
24
+    @Bean
25
+    public StringRedisSerializer stringRedisSerializer() {
26
+        return new StringRedisSerializer();
27
+    }
28
+
29
+    @Bean
30
+    public GenericJackson2JsonRedisSerializer genericJackson2JsonRedisSerializer() {
31
+        return new GenericJackson2JsonRedisSerializer();
32
+    }
33
+    
34
+    @Bean
35
+    public StringRedisTemplate stringRedisTemplate(RedisConnectionFactory redisConnectionFactory){
36
+        StringRedisTemplate template = new StringRedisTemplate();
37
+        template.setConnectionFactory(redisConnectionFactory);
38
+        return template;
39
+    }
40
+
41
+    @Bean
42
+    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory,
43
+                                                       StringRedisSerializer stringRedisSerializer,
44
+                                                       GenericJackson2JsonRedisSerializer genericJackson2JsonRedisSerializer) {
45
+        RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
46
+        redisTemplate.setConnectionFactory(redisConnectionFactory);
47
+        redisTemplate.setKeySerializer(stringRedisSerializer);
48
+        redisTemplate.setValueSerializer(genericJackson2JsonRedisSerializer);
49
+        redisTemplate.setHashKeySerializer(stringRedisSerializer);
50
+        redisTemplate.setHashValueSerializer(genericJackson2JsonRedisSerializer);
51
+        return redisTemplate;
52
+    }
53
+    
54
+}

+ 58 - 0
src/main/java/com/yaozhitech/spring5/controller/CityWebFluxController.java

@@ -0,0 +1,58 @@
1
+package com.yaozhitech.spring5.controller;
2
+
3
+import java.util.concurrent.TimeUnit;
4
+
5
+import org.springframework.beans.factory.annotation.Autowired;
6
+import org.springframework.data.redis.core.RedisTemplate;
7
+import org.springframework.data.redis.core.ValueOperations;
8
+import org.springframework.web.bind.annotation.DeleteMapping;
9
+import org.springframework.web.bind.annotation.GetMapping;
10
+import org.springframework.web.bind.annotation.PathVariable;
11
+import org.springframework.web.bind.annotation.PostMapping;
12
+import org.springframework.web.bind.annotation.RequestBody;
13
+import org.springframework.web.bind.annotation.RequestMapping;
14
+import org.springframework.web.bind.annotation.RestController;
15
+
16
+import com.yaozhitech.spring5.domain.City;
17
+
18
+import reactor.core.publisher.Mono;
19
+
20
+@RestController
21
+@RequestMapping(value = "/city")
22
+public class CityWebFluxController {
23
+
24
+	@Autowired
25
+	private RedisTemplate<String, Object> redisTemplate;
26
+
27
+	@GetMapping(value = "/{id}")
28
+	public Mono<City> findCityById(@PathVariable("id") Long id) {
29
+		String key = "city_" + id;
30
+		ValueOperations<String, Object> operations = redisTemplate.opsForValue();
31
+		boolean hasKey = redisTemplate.hasKey(key);
32
+		City city = (City) operations.get(key);
33
+
34
+		if (!hasKey) {
35
+			return Mono.create(monoSink -> monoSink.success(null));
36
+		}
37
+		return Mono.create(monoSink -> monoSink.success(city));
38
+	}
39
+
40
+	@PostMapping()
41
+	public Mono<City> saveCity(@RequestBody City city) {
42
+		String key = "city_" + city.getId();
43
+		ValueOperations<String, Object> operations = redisTemplate.opsForValue();
44
+		operations.set(key, city, 160, TimeUnit.SECONDS);
45
+
46
+		return Mono.create(monoSink -> monoSink.success(city));
47
+	}
48
+
49
+	@DeleteMapping(value = "/{id}")
50
+	public Mono<Long> deleteCity(@PathVariable("id") Long id) {
51
+		String key = "city_" + id;
52
+		boolean hasKey = redisTemplate.hasKey(key);
53
+		if (hasKey) {
54
+			redisTemplate.delete(key);
55
+		}
56
+		return Mono.create(monoSink -> monoSink.success(id));
57
+	}
58
+}

+ 27 - 0
src/main/java/com/yaozhitech/spring5/domain/City.java

@@ -0,0 +1,27 @@
1
+package com.yaozhitech.spring5.domain;
2
+
3
+import org.springframework.data.annotation.Id;
4
+
5
+import lombok.Data;
6
+
7
+@Data
8
+public class City {
9
+
10
+	@Id
11
+    private Long id;
12
+
13
+    /**
14
+     * 省份编号
15
+     */
16
+    private Long pid;
17
+
18
+    /**
19
+     * 城市名称
20
+     */
21
+    private String cityName;
22
+
23
+    /**
24
+     * 描述
25
+     */
26
+    private String desc;
27
+}

+ 9 - 0
src/main/resources/application.properties

@@ -0,0 +1,9 @@
1
+## Redis 配置
2
+## Redis服务器地址
3
+spring.redis.host=127.0.0.1
4
+## Redis服务器连接端口
5
+spring.redis.port=6279
6
+## Redis服务器连接密码(默认为空)
7
+spring.redis.password=bbztx123456
8
+# 连接超时时间(毫秒)
9
+spring.redis.timeout=5000