Spring boot 2.1学習ノート【19】SpringBoot 2統合応答式redis reactive
Springbootシリーズ学習ノートすべての文章は値ブロガーコラム*:spring boot 2に移動してください.X/spring cloud Greenwich. 一連の文章なので、後の文章は前の文章の項目に使われる可能性があります.SpringbootシリーズコードはすべてGitHubにアップロードされます.https://github.com/liubenlong/springboot2_demo本シリーズ環境:Java 11;springboot 2.1.1.RELEASE;springcloud Greenwich.RELEASE;MySQL 8.0.5;
文書ディレクトリ
redis:spring boot 2.1学習ノート【九】SpringBoot 2統合redisを継承する方法について説明した.この記事では、統合応答式のredisについて説明します.
環境説明:springboot 2.1.1 redisサーバで使用される2.8
ここでは、応答式redis依存
アプリケーションを構成します.yml
テストコントローラを作成します.ここでは単純で実用的なものですが、各方法の役割はコメントを参照してください.
Springbootシリーズ学習ノートすべての文章は値ブロガーコラム*:spring boot 2に移動してください.X/spring cloud Greenwich. 一連の文章なので、後の文章は前の文章の項目に使われる可能性があります.SpringbootシリーズコードはすべてGitHubにアップロードされます.https://github.com/liubenlong/springboot2_demo本シリーズ環境:Java 11;springboot 2.1.1.RELEASE;springcloud Greenwich.RELEASE;MySQL 8.0.5;
文書ディレクトリ
redis:spring boot 2.1学習ノート【九】SpringBoot 2統合redisを継承する方法について説明した.この記事では、統合応答式のredisについて説明します.
環境説明:springboot 2.1.1 redisサーバで使用される2.8
ここでは、応答式redis依存
spring-boot-starter-data-redis-reactive
を使用する必要がある.<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-data-redis-reactiveartifactId>
dependency>
<dependency>
<groupId>org.apache.commonsgroupId>
<artifactId>commons-pool2artifactId>
dependency>
<dependency>
<groupId>com.alibabagroupId>
<artifactId>fastjsonartifactId>
<version>1.2.54version>
dependency>
アプリケーションを構成します.yml
spring:
redis:
host: 127.0.0.1
port: 6379
lettuce:
pool:
max-active: 200 # ( )
max-idle: 20 #
min-idle: 5 #
max-wait: 1000 # ( )
テストコントローラを作成します.ここでは単純で実用的なものですが、各方法の役割はコメントを参照してください.
/**
* controller RouterFunction
*/
@RestController
@Slf4j
public class MyController {
/**
* ReactiveRedisTemplate
*/
@Autowired
private ReactiveRedisTemplate reactiveRedisTemplate;
/**
* webflux
*
* @return
*/
@GetMapping("/hello")
public Mono<String> hello1() {
return Mono.just("Welcome to reactive world ~");
}
/**
*
* 。 flatMap
* @return
*/
@GetMapping("/deleteVal")
public Flux deleteVal() {
Mono a = reactiveRedisTemplate.delete("a");
Mono b = reactiveRedisTemplate.delete("b");
Mono c = reactiveRedisTemplate.delete("c");
a.subscribe(System.out::println);// 。 。
b.subscribe(System.out::println);
c.subscribe(System.out::println);
return Flux.just(a, c);
}
@GetMapping("testReactorRedis1")
public void findCityById() {
Mono mono1 = reactiveRedisTemplate.opsForValue().set("c", "vvvv");
mono1.subscribe(System.out::println);
Stu stu = Stu.builder().name(" ").age(20).build();
Mono mono2 = reactiveRedisTemplate.opsForValue().set("a", JSONObject.toJSONString(stu));
mono2.subscribe(System.out::println);
//
Mono mono3 = reactiveRedisTemplate.opsForValue().set("b", Stu.builder().age(11).name("ds").build());
mono3.subscribe(System.out::println);
}
/**
*
* @return
*/
@GetMapping("/testReactorRedis2")
public Mono testReactorRedis2() {
Mono monoa = reactiveRedisTemplate.opsForValue().get("a");
return monoa;
}
@GetMapping("/testReactorRedis3")
public Mono<Stu> testReactorRedis3() {
return reactiveRedisTemplate.opsForValue().get("b");
}
/**
*
* testReactorRedis5 , ,
* flatMap , 。
* @return
*/
@GetMapping("/testReactorRedis4")
public Flux testReactorRedis4() {
Flux flux = Flux.just("a", "b", "c")
.flatMap(s -> reactiveRedisTemplate.opsForValue().get(s));
flux.subscribe(System.out::println);
return flux;
}
/**
* , 。 :
* {
* monoa: {
* scanAvailable: true
* },
* monoc: {
* scanAvailable: true
* }
* }
*
* @return
*/
@GetMapping("/testReactorRedis5")
public Map testReactorRedis5() {
Mono monoa = reactiveRedisTemplate.opsForValue().get("a");
Mono monoc = reactiveRedisTemplate.opsForValue().get("c");
return Map.of("monoa", monoa, "monoc", monoc);
}
}
Springbootシリーズ学習ノートすべての文章は値ブロガーコラム*:spring boot 2に移動してください.X/spring cloud Greenwich. 一連の文章なので、後の文章は前の文章の項目に使われる可能性があります.SpringbootシリーズコードはすべてGitHubにアップロードされます.https://github.com/liubenlong/springboot2_demo本シリーズ環境:Java 11;springboot 2.1.1.RELEASE;springcloud Greenwich.RELEASE;MySQL 8.0.5;