当前位置: 首页 > news >正文

微服务框架 SpringCloud微服务架构 21 RestClient 操作文档 21.1 新增文档

微服务框架

【SpringCloud+RabbitMQ+Docker+Redis+搜索+分布式,系统详解springcloud微服务技术栈课程|黑马程序员Java微服务】

SpringCloud微服务架构

文章目录

      • 微服务框架
      • SpringCloud微服务架构
      • 21 RestClient 操作文档
        • 21.1 新增文档
          • 21.1.1 新增文档

21 RestClient 操作文档

21.1 新增文档

21.1.1 新增文档

利用JavaRestClient实现文档的CRUD

去数据库查询酒店数据,导入到hotel索引库,实现酒店数据的CRUD。

基本步骤如下:

  1. 初始化JavaRestClient

  2. 利用JavaRestClient新增酒店数据

  3. 利用JavaRestClient根据id查询酒店数据

  4. 利用JavaRestClient删除酒店数据

  5. 利用JavaRestClient修改酒店数据

【初始化就可以跳过了】

新建一个测试类,实现文档相关操作,并且完成JavaRestClient的初始化

在这里插入图片描述

在这里插入图片描述

步骤2:添加酒店数据到索引库

先查询酒店数据,然后给这条数据创建倒排索引,即可完成添加:

在这里插入图片描述

直接试试

先弄一个和索引库结构一致的实体类

package cn.itcast.hotel.pojo;

import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * ClassName: HotelDoc
 * date: 2022/11/1 10:51
 *
 * @author DingJiaxiong
 */

@Data
@NoArgsConstructor
public class HotelDoc {
    private Long id;
    private String name;
    private String address;
    private Integer price;
    private Integer score;
    private String brand;
    private String city;
    private String starName;
    private String business;
    private String location;
    private String pic;

    public HotelDoc(Hotel hotel) {
        this.id = hotel.getId();
        this.name = hotel.getName();
        this.address = hotel.getAddress();
        this.price = hotel.getPrice();
        this.score = hotel.getScore();
        this.brand = hotel.getBrand();
        this.city = hotel.getCity();
        this.starName = hotel.getStarName();
        this.business = hotel.getBusiness();
        this.location = hotel.getLatitude() + ", " + hotel.getLongitude();
        this.pic = hotel.getPic();
    }
}

在这里插入图片描述

业务层接口

package cn.itcast.hotel.service;

import cn.itcast.hotel.pojo.Hotel;
import com.baomidou.mybatisplus.extension.service.IService;

public interface IHotelService extends IService<Hotel> {
}

来一个新的测试类

package cn.itcast.hotel;

import cn.itcast.hotel.pojo.Hotel;
import cn.itcast.hotel.pojo.HotelDoc;
import cn.itcast.hotel.service.IHotelService;
import cn.itcast.hotel.service.impl.HotelService;
import com.alibaba.fastjson.JSON;
import org.apache.http.HttpHost;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.io.IOException;

/**
 * ClassName: HotelDocumentTest
 * date: 2022/11/1 10:45
 *
 * @author DingJiaxiong
 */

@SpringBootTest
public class HotelDocumentTest {

    @Autowired
    private IHotelService hotelService;

    private RestHighLevelClient client;

    // 添加文档
    @Test
    void testAddDocument() throws IOException {
        //根据id 查询酒店数据
        Hotel hotel = hotelService.getById(61083L);
        //转换为文档类型
        HotelDoc hotelDoc = new HotelDoc(hotel);

        //1. 准备Request 对象
        IndexRequest request = new IndexRequest("hotel").id(hotel.getId().toString());
        //2. 准备Json 文档
        request.source(JSON.toJSONString(hotelDoc), XContentType.JSON);

        //3. 发送请求
        client.index(request , RequestOptions.DEFAULT);
    }


    @BeforeEach
    void setUp() { //在每个测试方法开始前都完成初始化工程
        this.client = new RestHighLevelClient(RestClient.builder(
                HttpHost.create("http://1.13.4.44:9200")
        ));
    }

    @AfterEach
    void tearDown() throws IOException { //每次都自动销毁
        this.client.close();
    }
}

直接运行测试方法

在这里插入图片描述

我giao, 我红了,换一个驱动

在这里插入图片描述

再试一次

在这里插入图片描述

又红了,我再看看

在这里插入图片描述

行吧,数据库密码写错了…

再试一次,又红了…url 错了,我真的会喊救命

在这里插入图片描述

在这里插入图片描述

再试一次

在这里插入图片描述

绿了

kibana 验证一下

GET /hotel/_doc/61083

在这里插入图片描述

OK, 没毛病

【这就是添加文档】

相关文章:

  • APP自动化测试系列之Appium介绍及运行原理
  • Nginx学习笔记
  • 蜣螂优化算法Dung beetle optimizer附matlab代码
  • 03-SpringBoot进阶
  • 英文Paper写作怎么确实合适的介词?
  • 代码提速100倍,怎么实现的?
  • 【C++】C++11新特性
  • 【面试】EntityFrameworkLinq面试题答案
  • 宝塔部署node项目
  • 配对交易之统计套利配对:协整(cointegration)
  • [附源码]Python计算机毕业设计Django演唱会门票售卖系统
  • [附源码]计算机毕业设计快转二手品牌包在线交易系统Springboot程序
  • 一个支持 CRUD 快速开发的 Web 框架,用着太爽
  • 2小时开发《点球射门游戏》,动画演示思路(上),代码已开源
  • 【Opencv实战】颜色测试:你是色盲/色弱吗?从零带你了解色彩与原理,简单的颜色识别小程序,感动.jpg(全是干货)
  • windows服务开发
  • leetcode-每日一题-二进制表示中质数个计算置位(简单,popcount算法)
  • 利用styleSheet,避免js手动频繁修改样式
  • 火爆的超级人工智能ChatGPT,唯独鄙视中国人
  • [附源码]Python计算机毕业设计Django医院挂号住院管理系统
  • 电加热油锅炉工作原理_电加热导油
  • 大型电蒸汽锅炉_工业电阻炉
  • 燃气蒸汽锅炉的分类_大连生物质蒸汽锅炉
  • 天津市维修锅炉_锅炉汽化处理方法
  • 蒸汽汽锅炉厂家_延安锅炉厂家
  • 山西热水锅炉厂家_酒店热水 锅炉
  • 蒸汽锅炉生产厂家_燃油蒸汽发生器
  • 燃煤锅炉烧热水_张家口 淘汰取缔燃煤锅炉
  • 生物质锅炉_炉
  • 锅炉天然气_天燃气热风炉