ReactiveElasticsearchTemplate的使用

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";
    }
}

四、测试

 

 

 

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值