ReactiveElasticsearchTemplate的使用

    科技2022-07-31  81

    Spring Data Elasticsearch提供了响应式的模板ReactiveElasticsearchTemplate。本文示例如何使用。

    目录

    一、添加依赖

    二、配置Bean:ReactiveElasticsearchClient

    三、创建ReactiveElasticsearchTemplate并调用方法

    四、测试


    一、添加依赖

    <dependency> <groupId>io.projectreactor</groupId> <artifactId>reactor-core</artifactId> <version>3.3.10.RELEASE</version> </dependency> <dependency> <groupId>io.projectreactor.netty</groupId> <artifactId>reactor-netty</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-webflux</artifactId> </dependency>

    二、配置Bean:ReactiveElasticsearchClient

    配置ReactiveElasticsearchClient,用于后续创建ReactiveElasticsearchTemplate。

    package cn.jack.elasticsearchdemo.config; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.data.elasticsearch.client.ClientConfiguration; import org.springframework.data.elasticsearch.client.reactive.ReactiveElasticsearchClient; import org.springframework.data.elasticsearch.client.reactive.ReactiveRestClients; import org.springframework.data.elasticsearch.config.AbstractReactiveElasticsearchConfiguration; import org.springframework.http.HttpHeaders; import java.time.Duration; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; @Configuration public class ReactiveEsConfig extends AbstractReactiveElasticsearchConfiguration { @Bean @Override public ReactiveElasticsearchClient reactiveElasticsearchClient() { HttpHeaders httpHeaders = new HttpHeaders(); httpHeaders.add("some-header", "on every request"); ClientConfiguration clientConfiguration = ClientConfiguration.builder() .connectedTo("localhost:9200"/*, "localhost:9291"*/) //.useSsl() //.withProxy("localhost:8888") //.withPathPrefix("ela") .withConnectTimeout(Duration.ofSeconds(5)) .withSocketTimeout(Duration.ofSeconds(3)) //.withDefaultHeaders(defaultHeaders) //.withBasicAuth(username, password) .withHeaders(() -> { HttpHeaders headers = new HttpHeaders(); headers.add("currentTime", LocalDateTime.now().format(DateTimeFormatter.ISO_LOCAL_DATE_TIME)); return headers; }) .build(); return ReactiveRestClients.create(clientConfiguration); } }

    三、创建ReactiveElasticsearchTemplate并调用方法

    package cn.jack.elasticsearchdemo.controller; import cn.jack.elasticsearchdemo.domain.Person; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.elasticsearch.client.reactive.ReactiveElasticsearchClient; import org.springframework.data.elasticsearch.core.ReactiveElasticsearchTemplate; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import javax.annotation.PostConstruct; @RestController @RequestMapping("/react") public class ReactiveEsTemplateController { @Autowired private ReactiveElasticsearchClient reactiveElasticsearchClient; private ReactiveElasticsearchTemplate reactiveElasticsearchTemplate; @PostConstruct public void init() { this.reactiveElasticsearchTemplate = new ReactiveElasticsearchTemplate(this.reactiveElasticsearchClient); } @RequestMapping("/test") public String test() { Person person = new Person(); person.setId("30"); person.setName("ReactiveElasticsearchTemplate"); reactiveElasticsearchTemplate.save(person) .doOnNext(System.out::println) .flatMap(person1 -> reactiveElasticsearchTemplate.findById(person.getId(), Person.class)) .doOnNext(System.out::println) .flatMap(id -> reactiveElasticsearchTemplate.count(Person.class)) .doOnNext(System.out::println) .subscribe(); return "success"; } }

    四、测试

     

     

     

     

    Processed: 0.014, SQL: 8