{"id":198622,"date":"2020-08-26T08:09:25","date_gmt":"2020-08-26T00:09:25","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=198622"},"modified":"2020-08-19T17:10:11","modified_gmt":"2020-08-19T09:10:11","slug":"redis-spring-boot","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/redis-spring-boot.html","title":{"rendered":"\u5982\u4f55\u901a\u8fc7WebFlux \u96c6\u6210 Redis\uff0cSpring Boot 2"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\nRedis \u662f\u5185\u5b58\u5f0f\u6570\u636e\u5e93\uff0c\u5b58\u50a8\u5728\u78c1\u76d8\u3002\u652f\u6301\u7684\u6570\u636e\u7c7b\u578b\u5f88\u591a\uff1aStrings, Lists, Sets, Sorted Sets, Hashes, HyperLogLogs, Bitmaps \u7b49\u3002<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n
\u5b89\u88c5\u7b80\u6613\u6559\u7a0b\uff08\u9002\u7528 Mac\/Linux\uff09<\/strong><\/div>\n
\u4e0b\u8f7d\u5e76\u89e3\u538b<\/strong><\/span><\/div>\n

\u4e0b\u8f7d\u5b89\u88c5\u5305 redis-x.x.x.tar.gz<\/p>\n

tar zxvf redis-2.8.17.tar.gz\r\n<\/pre>\n
\u7f16\u8bd1\u5b89\u88c5<\/strong><\/span><\/div>\n
cd redis-x.x.x\/\r\nmake\r\n<\/pre>\n
\u542f\u52a8 Redis<\/strong><\/span><\/div>\n
cd src\r\nredis-server\r\n<\/pre>\n

\u5982\u679c\u9700\u8981\u8fd0\u884c\u5728\u5b88\u62a4\u8fdb\u7a0b\uff0c\u8bbe\u7f6e daemonize \u4ece no \u4fee\u6539\u6210 yes\uff0c\u5e76\u6307\u5b9a\u8fd0\u884c\uff1aredis-server redis.conf<\/p>\n

\"\"<\/p>\n

\u7ed3\u6784<\/strong><\/div>\n

\u7c7b\u4f3c\u4e0a\u9762\u8bb2\u7684\u5de5\u7a0b\u642d\u5efa\uff0c\u65b0\u5efa\u4e00\u4e2a\u5de5\u7a0b\u7f16\u5199\u6b64\u6848\u4f8b\u3002\u5de5\u7a0b\u5982\u56fe\uff1a<\/p>\n

\"\"<\/p>\n

\u76ee\u5f55\u6838\u5fc3\u5982\u4e0b<\/p>\n

pom.xml maven \u914d\u7f6e<\/p>\n

application.properties \u914d\u7f6e\u6587\u4ef6<\/p>\n

domain \u5b9e\u4f53\u7c7b<\/p>\n

controller \u63a7\u5236\u5c42\uff0c\u672c\u6587\u8981\u70b9<\/p>\n

\u65b0\u589e POM \u4f9d\u8d56\u4e0e\u914d\u7f6e<\/p>\n

\u5728 pom.xml \u914d\u7f6e\u65b0\u7684\u4f9d\u8d56\uff1a<\/p>\n

 \r\norg.springframework.boot\r\nspring-boot-starter-data-redis-reactive\r\n<\/pre>\n

\u7c7b\u4f3c MongoDB \u914d\u7f6e\uff0c\u5728 application.properties \u914d\u7f6e\u8fde\u63a5 Redis \uff1a<\/p>\n

## Redis \u914d\u7f6e<\/p>\n

\r\n## Redis\u670d\u52a1\u5668\u5730\u5740\r\n\r\nspring.redis.host=127.0.0.1\r\n\r\n## Redis\u670d\u52a1\u5668\u8fde\u63a5\u7aef\u53e3\r\n\r\nspring.redis.port=6379\r\n\r\n## Redis\u670d\u52a1\u5668\u8fde\u63a5\u5bc6\u7801\uff08\u9ed8\u8ba4\u4e3a\u7a7a\uff09\r\n\r\nspring.redis.password=\r\n\r\n# \u8fde\u63a5\u8d85\u65f6\u65f6\u95f4\uff08\u6beb\u79d2\uff09\r\nspring.redis.timeout=5000\r\n<\/pre>\n

\u9ed8\u8ba4 \u5bc6\u7801\u4e3a\u7a7a\uff0c\u8fd9\u91cc\u6ce8\u610f\u7684\u662f\u8fde\u63a5\u8d85\u65f6\u65f6\u95f4\u4e0d\u80fd\u592a\u5c11\u6216\u8005\u4e3a 0 \uff0c\u4e0d\u7136\u4f1a\u5f15\u8d77\u5f02\u5e38 RedisCommandTimeoutException: Command timed out\u3002<\/p>\n

\u5bf9\u8c61<\/strong><\/div>\n

\u4fee\u6539 org.spring.springboot.domain \u5305\u91cc\u9762\u7684\u57ce\u5e02\u5b9e\u4f53\u5bf9\u8c61\u7c7b\u3002\u57ce\u5e02\uff08City\uff09\u5bf9\u8c61 City\uff0c\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n

\r\nimport org.springframework.data.annotation.Id;\r\n\r\nimport java.io.Serializable;\r\n\r\n\/**\r\n* \u57ce\u5e02\u5b9e\u4f53\u7c7b\r\n*\r\n*\/\r\npublic class City implements Serializable {\r\n\r\nprivate static final long serialVersionUID = -2081742442561524068L;\r\n\r\n\/**\r\n* \u57ce\u5e02\u7f16\u53f7\r\n*\/\r\n@Id\r\nprivate Long id;\r\n\r\n\/**\r\n* \u7701\u4efd\u7f16\u53f7\r\n*\/\r\nprivate Long provinceId;\r\n\r\n\/**\r\n* \u57ce\u5e02\u540d\u79f0\r\n*\/\r\nprivate String cityName;\r\n\r\n\/**\r\n* \u63cf\u8ff0\r\n*\/\r\nprivate String description;\r\n\r\npublic Long getId() {\r\nreturn id;\r\n}\r\n\r\npublic void setId(Long id) {\r\nthis.id = id;\r\n}\r\n\r\npublic Long getProvinceId() {\r\nreturn provinceId;\r\n}\r\n\r\npublic void setProvinceId(Long provinceId) {\r\nthis.provinceId = provinceId;\r\n}\r\n\r\npublic String getCityName() {\r\nreturn cityName;\r\n}\r\n\r\npublic void setCityName(String cityName) {\r\nthis.cityName = cityName;\r\n}\r\n\r\npublic String getDescription() {\r\nreturn description;\r\n}\r\n\r\npublic void setDescription(String description) {\r\nthis.description = description;\r\n}\r\n}\r\n<\/pre>\n

\u503c\u5f97\u6ce8\u610f\u70b9\uff1a<\/p>\n

@Id \u6ce8\u89e3\u6807\u8bb0\u5bf9\u5e94\u5e93\u8868\u7684\u4e3b\u952e\u6216\u8005\u552f\u4e00\u6807\u8bc6\u7b26\u3002\u56e0\u4e3a\u8fd9\u4e2a\u662f\u6211\u4eec\u7684 DO \uff0c\u6570\u636e\u8bbf\u95ee\u5bf9\u8c61\u4e00\u4e00\u6620\u5c04\u5230\u6570\u636e\u5b58\u50a8\u3002<\/p>\n

City \u5fc5\u987b\u5b9e\u73b0\u5e8f\u5217\u5316\uff0c\u56e0\u4e3a\u9700\u8981\u5c06\u5bf9\u8c61\u5e8f\u5217\u5316\u540e\u5b58\u50a8\u5230 Redis\u3002\u5982\u679c\u6ca1\u5b9e\u73b0 Serializable \uff0c\u4f1a\u5f15\u51fa\u5f02\u5e38\uff1ajava.lang.IllegalArgumentException: DefaultSerializer requires a Serializable payload but received an object of type<\/p>\n

\u5982\u679c\u4e0d\u662f\u7528\u9ed8\u8ba4\u7684\u5e8f\u5217\u5316\uff0c\u9700\u8981\u81ea\u5b9a\u4e49\u5e8f\u5217\u5316\u5b9e\u73b0\uff0c\u53ea\u8981\u5b9e\u73b0 RedisSerializer \u63a5\u53e3\u53bb\u5b9e\u73b0\u5373\u53ef\uff0c\u7136\u540e\u5728\u4f7f\u7528 RedisTemplate.setValueSerializer \u65b9\u6cd5\u53bb\u8bbe\u7f6e\u4f60\u5b9e\u73b0\u7684\u5e8f\u5217\u5316\u5b9e\u73b0\u3002\u652f\u6301 JSON\u3001XML \u7b49\u3002<\/p>\n

\u63a7\u5236\u5c42 CityWebFluxController<\/strong><\/span><\/div>\n

\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n

\r\nimport org.spring.springboot.domain.City;\r\nimport org.springframework.beans.factory.annotation.Autowired;\r\nimport org.springframework.data.redis.core.RedisTemplate;\r\nimport org.springframework.data.redis.core.ValueOperations;\r\nimport org.springframework.web.bind.annotation.*;\r\nimport reactor.core.publisher.Mono;\r\n\r\nimport java.util.concurrent.TimeUnit;\r\n\r\n@RestController\r\n@RequestMapping(value = \"\/city\")\r\npublic class CityWebFluxController {\r\n\r\n@Autowired\r\nprivate RedisTemplate redisTemplate;\r\n\r\n@GetMapping(value = \"\/{id}\")\r\npublic Mono findCityById(@PathVariable(\"id\") Long id) {\r\nString key = \"city_\" + id;\r\nValueOperations<String, City> operations = redisTemplate.opsForValue();\r\nboolean hasKey = redisTemplate.hasKey(key);\r\nCity city = operations.get(key);\r\n\r\nif (!hasKey) {\r\nreturn Mono.create(monoSink -> monoSink.success(null));\r\n}\r\nreturn Mono.create(monoSink -> monoSink.success(city));\r\n}\r\n\r\n@PostMapping()\r\npublic Mono saveCity(@RequestBody City city) {\r\nString key = \"city_\" + city.getId();\r\nValueOperations<String, City> operations = redisTemplate.opsForValue();\r\noperations.set(key, city, 60, TimeUnit.SECONDS);\r\n\r\nreturn Mono.create(monoSink -> monoSink.success(city));\r\n}\r\n\r\n@DeleteMapping(value = \"\/{id}\")\r\npublic Mono deleteCity(@PathVariable(\"id\") Long id) {\r\nString key = \"city_\" + id;\r\nboolean hasKey = redisTemplate.hasKey(key);\r\nif (hasKey) {\r\nredisTemplate.delete(key);\r\n}\r\nreturn Mono.create(monoSink -> monoSink.success(id));\r\n}\r\n}\r\n<\/pre>\n

\u4f7f\u7528 @Autowired \u6ce8\u5165 RedisTemplate \u5bf9\u8c61\u3002\u8fd9\u4e2a\u5bf9\u8c61\u548c Spring \u7684 JdbcTemplate \u529f\u80fd\u5341\u5206\u76f8\u4f3c\uff0cRedisTemplate \u5c01\u88c5\u4e86 RedisConnection\uff0c\u5177\u6709\u8fde\u63a5\u7ba1\u7406\u3001\u5e8f\u5217\u5316\u548c\u5404\u4e2a\u64cd\u4f5c\u7b49\u3002\u8fd8\u6709\u9488\u5bf9 String \u7684\u652f\u6301\u5bf9\u8c61 StringRedisTemplate\u3002<\/p>\n

\u5220\u9664 Redis \u67d0\u5bf9\u8c61\uff0c\u76f4\u63a5\u901a\u8fc7 key \u503c\u8c03\u7528 delete(key)\u3002<\/p>\n

Redis \u64cd\u4f5c\u89c6\u56fe\u63a5\u53e3\u7c7b\u7528\u7684\u662f ValueOperations\uff0c\u5bf9\u5e94\u7684\u662f Redis String\/Value \u64cd\u4f5c\u3002get \u662f\u83b7\u53d6\u6570\u636e\uff1bset \u662f\u63d2\u5165\u6570\u636e\uff0c\u53ef\u4ee5\u8bbe\u7f6e\u5931\u6548\u65f6\u95f4\u3002\u8fd9\u91cc\u8bbe\u7f6e\u7684\u5931\u6548\u65f6\u95f4\u662f 60 s\u3002<\/p>\n

\u8fd8\u6709\u5176\u4ed6\u7684\u64cd\u4f5c\u89c6\u56fe\uff0cListOperations\u3001SetOperations\u3001ZSetOperations \u548c HashOperations \u3002<\/p>\n

\u8fd0\u884c\u5de5\u7a0b<\/strong><\/div>\n

\u4e00\u4e2a\u64cd\u4f5c Redis \u5de5\u7a0b\u5c31\u5f00\u53d1\u5b8c\u6bd5\u4e86\uff0c\u4e0b\u9762\u8fd0\u884c\u5de5\u7a0b\u9a8c\u8bc1\u4e0b\u3002\u4f7f\u7528 IDEA \u53f3\u4fa7\u5de5\u5177\u680f\uff0c\u70b9\u51fb Maven Project Tab \uff0c\u70b9\u51fb\u4f7f\u7528\u4e0b Maven \u63d2\u4ef6\u7684 install \u547d\u4ee4\u3002\u6216\u8005\u4f7f\u7528\u547d\u4ee4\u884c\u7684\u5f62\u5f0f\uff0c\u5728\u5de5\u7a0b\u6839\u76ee\u5f55\u4e0b\uff0c\u6267\u884c Maven \u6e05\u7406\u548c\u5b89\u88c5\u5de5\u7a0b\u7684\u6307\u4ee4\uff1a<\/p>\n

\r\ncd springboot-webflux-6-redis\r\nmvn clean install\r\n\r\n\u5728\u63a7\u5236\u53f0\u4e2d\u770b\u5230\u6210\u529f\u7684\u8f93\u51fa\uff1a\r\n\r\n... \u7701\u7565\r\n[INFO] ------------------------------------------------------------------------\r\n[INFO] BUILD SUCCESS\r\n[INFO] ------------------------------------------------------------------------\r\n[INFO] Total time: 01:30 min\r\n[INFO] Finished at: 2018-10-15T10:00:54+08:00\r\n[INFO] Final Memory: 31M\/174M\r\n[INFO] ------------------------------------------------------------------------\r\n\u5728 IDEA \u4e2d\u6267\u884c Application \u7c7b\u542f\u52a8\uff0c\u4efb\u610f\u6b63\u5e38\u6a21\u5f0f\u6216\u8005 Debug \u6a21\u5f0f\u3002\u53ef\u4ee5\u5728\u63a7\u5236\u53f0\u770b\u5230\u6210\u529f\u8fd0\u884c\u7684\u8f93\u51fa\uff1a\r\n\r\n... \u7701\u7565\r\n2018-04-10 08:43:39.932 INFO 2052 --- [ctor-http-nio-1] r.ipc.netty.tcp.BlockingNettyContext : Started HttpServer on \/0:0:0:0:0:0:0:0:8080\r\n2018-04-10 08:43:39.935 INFO 2052 --- [ main] o.s.b.web.embedded.netty.NettyWebServer : Netty started on port(s): 8080\r\n2018-04-10 08:43:39.960 INFO 2052 --- [ main] org.spring.springboot.Application : Started Application in 6.547 seconds (JVM running for 9.851)\r\n<\/pre>\n

\u6253\u5f00 POST MAN \u5de5\u5177\uff0c\u5f00\u53d1\u5fc5\u5907\u3002\u8fdb\u884c\u4e0b\u9762\u64cd\u4f5c\uff1a<\/p>\n

\u65b0\u589e\u57ce\u5e02\u4fe1\u606f POST http:\/\/127.0.0.1:8080\/city
\n\"\"<\/p>\n

\u83b7\u53d6\u57ce\u5e02\u4fe1\u606f GET http:\/\/127.0.0.1:8080\/city\/2
\n\"\"<\/p>\n

\u5982\u679c\u7b49\u5f85 60s \u4ee5\u540e\uff0c\u518d\u6b21\u5219\u4f1a\u83b7\u53d6\u4e3a\u7a7a\u3002\u56e0\u4e3a\u4fdd\u5b58\u7684\u65f6\u5019\u8bbe\u7f6e\u4e86\u5931\u6548\u65f6\u95f4\u662f 60 s\u3002<\/p>\n

\u603b\u7ed3<\/strong><\/div>\n

\u8fd9\u91cc\uff0c\u63a2\u8ba8\u4e86 Spring WebFlux \u7684\u5982\u4f55\u6574\u5408 Redis\u3002\u4ecb\u7ecd\u4e86\u5982\u4f55\u901a\u8fc7 RedisTemplate \u53bb\u64cd\u4f5c Redis\u3002\u56e0\u4e3a Redis \u5728\u83b7\u53d6\u8d44\u6e90\u6027\u80fd\u6781\u4f73\uff0c\u5e38\u7528 Redis \u4f5c\u4e3a\u7f13\u5b58\u5b58\u50a8\u5bf9\u8c61\u3002\u4e0b\u9762\u6211\u4eec\u5229\u7528 Reids \u5b9e\u73b0\u7f13\u5b58\u64cd\u4f5c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"

\u4e0b\u8f7d\u5b89\u88c5\u5305 redis-x.x.x.tar.gz tar zxvf redis-2.8.17.tar.gz c […]<\/p>\n","protected":false},"author":1479,"featured_media":163508,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-198622","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/198622","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/1479"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=198622"}],"version-history":[{"count":4,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/198622\/revisions"}],"predecessor-version":[{"id":198713,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/198622\/revisions\/198713"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/163508"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=198622"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=198622"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=198622"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}