注:  该集成方式,对Elasticsearch无版本限制,但是需要自行封装请求,解析结果等。

<dependency>
  <groupId>org.elasticsearch.client</groupId>
   <artifactId>elasticsearch-rest-client</artifactId>
   <version>7.3.0</version>
</dependency> <dependency>
<groupId>com.alibaba</groupId>
  <artifactId>fastjson</artifactId>
  <version>1.2.47</version>
</dependency>

客户端配置RestConfig

@Configuration

public class RestConfig {

    @Bean

    public RestClient getClient() throws KeyStoreException, NoSuchAlgorithmException, KeyManagementException {

        // 如果有多个从节点可以持续在内部new多个HttpHost,参数1是ip,参数2是HTTP端口,参数3是通信协议

        RestClientBuilder clientBuilder = RestClient.builder(new HttpHost("134.175.237.32", 9200, "http"));

        // 添加其他配置,返回来的还是RestClientBuilder对象,这些配置都是可选的

        //【1. 设置请求头】 设置请求头,每个请求都会带上这个请求头

        //Header[] defaultHeaders = {new BasicHeader("header", "value")};

        //clientBuilder.setDefaultHeaders(defaultHeaders);

        Header[] defaultHeaders = {new BasicHeader("charset", "utf-8"),

                new BasicHeader("content-type", "application/json")};

        clientBuilder.setDefaultHeaders(defaultHeaders);

        //【2. 设置超时时间】 设置超时时间,多次尝试同一请求时应该遵守的超时。默认值为30秒,与默认套接字超时相同。若自定义套接字超时,则应相应地调整最大重试超时

        //clientBuilder.setMaxRetryTimeoutMillis(60000);

        //【3. 设置节点失败监听器】设置监听器,每次节点失败都可以监听到,可以作额外处理

        /*clientBuilder.setFailureListener(new RestClient.FailureListener() {

            @Override

            public void onFailure(Node node) {

                super.onFailure(node);

                System.out.println(node.getName() + "==节点失败了");

            }

        });*/

        /*【4. 设置节点选择器】 配置节点选择器,客户端以循环方式将每个请求发送到每一个配置的节点上,

        发送请求的节点,用于过滤客户端,将请求发送到这些客户端节点,默认向每个配置节点发送,

        这个配置通常是用户在启用嗅探时向专用主节点发送请求(即只有专用的主节点应该被HTTP请求命中)

        */

       // clientBuilder.setNodeSelector(NodeSelector.SKIP_DEDICATED_MASTERS);

        // 进行详细的配置

       /* clientBuilder.setNodeSelector(new NodeSelector() {

            // 设置分配感知节点选择器,允许选择本地机架中的节点(如果有),否则转到任何机架中的任何其他节点。

            @Override

            public void select(Iterable<Node> nodes) {

                boolean foundOne = false;

                for (Node node: nodes) {

                    String rackId = node.getAttributes().get("rack_id").get(0);

                    if ("rack_one".equals(rackId)) {

                        foundOne = true;

                        break;

                    }

                }

                if (foundOne) {

                    Iterator<Node> nodesIt = nodes.iterator();

                    while (nodesIt.hasNext()) {

                        Node node = nodesIt.next();

                        String rackId = node.getAttributes().get("rack_id").get(0);

                        if ("rack_one".equals(rackId) == false) {

                            nodesIt.remove();

                        }

                    }

                }

            }

        });*/

       /* 【5. 配置HTTP异步请求ES的线程数】

       配置异步请求的线程数量,Apache Http Async Client默认启动一个调度程序线程,以及由连接管理器使用的许多工作线程

        (与本地检测到的处理器数量一样多,取决于Runtime.getRuntime().availableProcessors()返回的数量)。线程数可以修改如下,

        这里是修改为1个线程,即默认情况

        */

        /*clientBuilder.setHttpClientConfigCallback(new RestClientBuilder.HttpClientConfigCallback() {

            @Override

            public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpAsyncClientBuilder) {

                return httpAsyncClientBuilder.setDefaultIOReactorConfig(

                        IOReactorConfig.custom().setIoThreadCount(1).build()

                );

            }

        });*/

        /*【6. 配置连接超时和套接字超时】

        配置请求超时,将连接超时(默认为1秒)和套接字超时(默认为30秒)增加,

        这里配置完应该相应地调整最大重试超时(默认为30秒),即上面的setMaxRetryTimeoutMillis,一般于最大的那个值一致即60000

        */

       /* clientBuilder.setRequestConfigCallback(new RestClientBuilder.RequestConfigCallback() {

            @Override

            public RequestConfig.Builder customizeRequestConfig(RequestConfig.Builder requestConfigBuilder) {

                // 连接5秒超时,套接字连接60s超时

                return requestConfigBuilder.setConnectTimeout(5000).setSocketTimeout(60000);

            }

        });*/

       /*【7. 配置ES安全认证】

        如果ES设置了密码,那这里也提供了一个基本的认证机制,下面设置了ES需要基本身份验证的默认凭据提供程序

            */

        /*final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();

        credentialsProvider.setCredentials(AuthScope.ANY,

                new UsernamePasswordCredentials("user", "password"));

        clientBuilder.setHttpClientConfigCallback(new RestClientBuilder.HttpClientConfigCallback() {

            @Override

            public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpClientBuilder) {

                return httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider);

            }

        });*/

        /*

        上面采用异步机制实现抢先认证,这个功能也可以禁用,这意味着每个请求都将在没有授权标头的情况下发送,然后查看它是否被接受,

        并且在收到HTTP 401响应后,它再使用基本认证头重新发送完全相同的请求,这个可能是基于安全、性能的考虑

         */

        /*clientBuilder.setHttpClientConfigCallback(new RestClientBuilder.HttpClientConfigCallback() {

            @Override

            public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpClientBuilder) {

                // 禁用抢先认证的方式

                httpClientBuilder.disableAuthCaching();

                return httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider);

            }

        });*/

        /* 【8. 配置通信加密】

        配置通信加密,有多种方式:setSSLContext、setSSLSessionStrategy和setConnectionManager(它们的重要性逐渐递增)

         */

        /*KeyStore truststore = KeyStore.getInstance("jks");

        try (InputStream is = Files.newInputStream(keyStorePath)) {

            truststore.load(is, keyStorePass.toCharArray());

        }

        SSLContextBuilder sslBuilder = SSLContexts.custom().loadTrustMaterial(truststore, null);

        final SSLContext sslContext = sslBuilder.build();

        clientBuilder.setHttpClientConfigCallback(new RestClientBuilder.HttpClientConfigCallback() {

            @Override

            public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpClientBuilder) {

                return httpClientBuilder.setSSLContext(sslContext);

            }

        });*/

        // 最后配置好的clientBuilder再build一下即可得到真正的Client

        return clientBuilder.build();

    }

}

实体类:

public class Book {

    private String id;

    private String name;

    private String author;

    public String getAuthor() {

        return author;

    }

    public void setAuthor(String author) {

        this.author = author;

    }

    public String getId() {

        return id;

    }

    public void setId(String id) {

        this.id = id;

    }

    public String getName() {

        return name;

    }

    public void setName(String name) {

        this.name = name;

    }

    @Override

    public String toString() {

        return "Book{" +

                "id=" + id +

                ", name='" + name + '\'' +

                '}';

    }

}

Rest  Controller:

@RestController

@RequestMapping("/book")

public class BookController {

    @Autowired

    private RestClient client;

    /**新增

     * 添加ES对象, Book的ID就是ES中存储的document的ID,所以最好不要为空,自定义生成的ID太浮夸

     *

     * @return ResponseEntity

     * @throws IOException

     */

    @PostMapping("/add")

    public ResponseEntity<String> add(Book book) throws IOException {

        // 构造HTTP请求,第一个参数是请求方法,第二个参数是服务器的端点,host默认是http://localhost:9200,

        // endpoint直接指定为index/type的形式

        Request request = new Request("POST", new StringBuilder("/test_db/book/").

                append(book.getId()).toString());

        // 设置其他一些参数比如美化json

        request.addParameter("pretty", "true");

        String bookString=JSON.toJSONString(book);

        System.out.println(bookString);

        // 设置请求体并指定ContentType,如果不指定默认为APPLICATION_JSON

        request.setEntity(new NStringEntity(bookString, ContentType.APPLICATION_JSON));

        // 发送HTTP请求

        Response response = client.performRequest(request);

        // 获取响应体, id: AWXvzZYWXWr3RnGSLyhH

        String responseBody = EntityUtils.toString(response.getEntity());

        return new ResponseEntity<>(responseBody, HttpStatus.OK);

    }

    /**

     * 根据id获取ES对象

     *

     * @param id

     * @return

     * @throws IOException

     */

    @GetMapping("/getBookById/{id}")

    public ResponseEntity<String> getBookById(@PathVariable("id") String id) {

        Request request = new Request("GET", new StringBuilder("/test_db/book/").

                append(id).toString());

        // 添加json返回优化

        request.addParameter("pretty", "true");

        Response response = null;

        String responseBody = null;

        try {

            // 执行HHTP请求

            response = client.performRequest(request);

            responseBody = EntityUtils.toString(response.getEntity());

        } catch (IOException e) {

            return new ResponseEntity<>("can not found the book by your id", HttpStatus.NOT_FOUND);

        }

        return new ResponseEntity<>(responseBody, HttpStatus.OK);

    }

    /**

     * 根据id更新Book

     *

     * @param id

     * @param book

     * @return

     */

    @PostMapping("/updateBook/{id}")

    public ResponseEntity<String> updateBook(@PathVariable("id") String id, Book book) throws IOException {

        // 构造HTTP请求

        Request request = new Request("POST", new StringBuilder("/test_db/book/").

                append(id).append("/_update").toString());

        request.addParameter("pretty", "true");

        // 将数据丢进去,这里一定要外包一层“doc”,否则内部不能识别

        JSONObject jsonObject = new JSONObject();

        jsonObject.put("doc", book);

        request.setEntity(new NStringEntity(jsonObject.toString(), ContentType.APPLICATION_JSON));

        // 执行HTTP请求

        Response response = client.performRequest(request);

        // 获取返回的内容

        String responseBody = EntityUtils.toString(response.getEntity());

        return new ResponseEntity<>(responseBody, HttpStatus.OK);

    }

    /**

     * 使用脚本更新Book

     * @param id

     * @param

     * @return

     * @throws IOException

     */

    @PostMapping("/update2/{id}")

    public ResponseEntity<String> updateBook2(@PathVariable("id") String id, @RequestParam("name") String name) throws IOException {

        // 构造HTTP请求

        Request request = new Request("POST", new StringBuilder("/test_db/book/").

                append(id).append("/_update").toString());

        request.addParameter("pretty", "true");

        JSONObject jsonObject = new JSONObject();

        // 创建脚本语言,如果是字符变量,必须加单引号

        StringBuilder op1 = new StringBuilder("ctx._source.name=").append("'" + name + "'");

        jsonObject.put("script", op1);

        request.setEntity(new NStringEntity(jsonObject.toString(), ContentType.APPLICATION_JSON));

        // 执行HTTP请求

        Response response = client.performRequest(request);

        // 获取返回的内容

        String responseBody = EntityUtils.toString(response.getEntity());

        return new ResponseEntity<>(responseBody, HttpStatus.OK);

    }

    @PostMapping("/deleteById/{id}")

    public ResponseEntity<String> deleteById(@PathVariable("id") String id) throws IOException {

        Request request = new Request("DELETE", new StringBuilder("/test_db/book/").append(id).toString());

        request.addParameter("pretty", "true");

        // 执行HTTP请求

        Response response = client.performRequest(request);

        // 获取结果

        String responseBody = EntityUtils.toString(response.getEntity());

        return new ResponseEntity<>(responseBody, HttpStatus.OK);

    }

    /**

     * 获取ES对象列表

     *

     * @return

     * @throws IOException

     */

    @GetMapping("/getBookList")

    public ResponseEntity<Object> getBookList(@RequestParam("author") String author) {

        // 构造HTTP请求

        Request request = new Request("POST", new StringBuilder("/test_db/book/_search").toString());

        // 添加json返回优化

        request.addParameter("pretty", "true");

        StringBuilder requestBody = new StringBuilder();

        requestBody.append("{\"size\":10,\"query\":{\"match\":{\"author\":\""+author+"\"}},\"from\": 0,\"_source\": [\"id\", \"name\", \"author\"]}");

        //JSONObject jsonObject = new JSONObject();

       // jsonObject.put("doc", requestBody.toString());

        request.setEntity(new NStringEntity(requestBody.toString(), ContentType.APPLICATION_JSON));

        Response response = null;

        String responseBody = null;

        Object result=null;

        try{

            // 执行HTTP请求

            response = client.performRequest(request);

            // 获取返回的内容

            responseBody = EntityUtils.toString(response.getEntity());

            Map jsonObject = JSON.parseObject(responseBody);

            result =jsonObject.get("hits");

            System.out.println(result);

        } catch (IOException e) {

            e.printStackTrace();

            return new ResponseEntity<>("can not found the book by your id", HttpStatus.NOT_FOUND);

        }

        return new ResponseEntity<>(result, HttpStatus.OK);

    }

springboot-elasticsearch-rest-client.zi

p

springboot集成elk 三:springboot + Elasticsearch Rest-Client的更多相关文章

  1. springboot集成elk 一: springboot + Elasticsearch

    1.ELK介绍 1> Elasticsearch是实时全文搜索和分析引擎, 提供搜集.分析.存储数据三大功能: 是一套开放REST和JAVA API等结构提供高效搜索功能,可扩展的分布式系统. ...

  2. springboot 集成 elk 日志收集功能

    Lilishop 技术栈 官方公众号 & 开源不易,如有帮助请点Star 介绍 官网:https://pickmall.cn Lilishop 是一款Java开发,基于SpringBoot研发 ...

  3. SpringBoot学习笔记(三):SpringBoot集成Mybatis、SpringBoot事务管理、SpringBoot多数据源

    SpringBoot集成Mybatis 第一步我们需要在pom.xml里面引入mybatis相关的jar包 <dependency> <groupId>org.mybatis. ...

  4. 【Elastic-2】SpringBoot整合ELK、SpringBoot写ES

    ELK相关TODO 快速开始文档(https://www.cnblogs.com/lbhym/p/15934416.html) SpringBoot整合ELK ELK接入Kafka(待Kafka快速开 ...

  5. springboot集成elk 四:springboot + Elasticsearch+Jest

    依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spri ...

  6. springboot集成elk实现分布式日志管理

    1.安装elk https://www.cnblogs.com/xuaa/p/10769759.html 2.idea创建springboot项目 File -> New -> Proje ...

  7. springboot集成elk 二:springboot + elk 采集日志

    到logstash-2.0.0\bin下新建文件 logstash.conf input { tcp { mode => "server" host => " ...

  8. springboot集成Guava缓存

    很久没有写博客了,这段时间一直忙于看论文,写论文,简直头大,感觉还是做项目比较舒服,呵呵,闲话不多说,今天学习了下Guava缓存,这跟Redis类似的,但是适用的场景不一样,学习下吧.今天我们主要是s ...

  9. SpringBoot图文教程14—SpringBoot集成EasyExcel「上」

    有天上飞的概念,就要有落地的实现 概念十遍不如代码一遍,朋友,希望你把文中所有的代码案例都敲一遍 先赞后看,养成习惯 SpringBoot 图文教程系列文章目录 SpringBoot图文教程1「概念+ ...

随机推荐

  1. uoj #139

    树链剖分//模板题由于存在换根操作对所有关于节点 u 的修改和查询操作进行分类讨论若 Root 在 u 的子树中,则不处理 u 所在的 Root 的那颗子树否则不会有影响寻找 Root 所在的那颗子树 ...

  2. AtCoder Grand Contest 001 题解

    传送门 \(A\) 咕咕咕 const int N=505; int a[N],n,res; int main(){ scanf("%d",&n); fp(i,1,n< ...

  3. bochs调试命令

    Bochs几条基本指令: 通过物理地址查看内存时,可以不加参数'/nuf': 其中n指定显示的单元数,默认是1: u 指定每个显示单元的大小(b表示字节.h表示字(2字节).w表示双字(4字节)),默 ...

  4. C语言联合

    联合使用关键字union,表示的一种量,只占用一块内存,具体如何占用取决于类型最大的那个.比如int和float会选用float. 联合也可以和结构体结合起来用,也可以赋值,通过.属性名的方式指定初始 ...

  5. centos6和centos7中常用命令区别

    以前一直接触的是centos6,最近因为新项目接触到centos7,发现有些命令还是有差异的(从centos7开始使用systemctl来管理服务和程序,包括了service和chkconfig),现 ...

  6. spring boot 之 如何创建spring boot项目

    创建spring boot的方式有非常多,今天我们使用maven来进行创建spring boot项目,因为maven使用的非常广泛,也很好用,很多IDE也都支持maven. 1 创建maven项目 1 ...

  7. git前期准备

    git小结 设置用户名 git config –global user.name 'itcast' 设置用户名邮箱 git config –global user.email 'itcast' 查看设 ...

  8. 二维背包---P1855 榨取kkksc03

    P1855 榨取kkksc03 题解 二维背包板子题 f[ i ][ j ] 前 n 个物品,花费金钱不超过 i ,花费时间不超过 j 的最大价值 如果每个物品只能选一次,那么就相当于在01背包上多加 ...

  9. keras Model 3 共享的层

    1 入门 2 多个输入和输出 3 共享层 考虑这样的一个问题:我们要判断连个tweet是否来源于同一个人. 首先我们对两个tweet进行处理,然后将处理的结构拼接在一起,之后跟一个逻辑回归,输出这两条 ...

  10. JV默认是如何处理异常

    main函数收到这个问题时,有两种处理方式: a:自己将该问题处理,然后继续运行 b:自己没有针对的处理方式,只有交给调用main的jvm来处理 jvm有一个默认的异常处理机制,就将该异常进行处理. ...