SpringBoot整合Elasticsearch7.5


由於項目要重構成微服務架構,閑來無事做下技術調研,看了網上好多資料頭都暈,各種試還是沒偷懶成功,所以潛下心來自已搞,不廢話一起來玩。

版本信息

  • SpringBoot: 2.2.3.BUILD-SNAPSHOT
  • Elasticsearch: 7.5.1
  • JDK: 1.8

一、安裝Elasticsearch服務以及新建SpringBoot項目

安裝Elasticsearch

  • 下載地址: https://www.elastic.co/downloads/elasticsearch

  • 選擇版本7.5.1, 我選擇在windows版,方便用先裝本地,你可選擇其他環境版本

  • 解壓elasticsearch-7.5.1-windows-x86_64.zip文件

  • 啟動服務:運行bin\elasticsearch.bat文件即可

  • 檢驗:訪問http://localhost:9200看到ES的服務信息,安裝成功

    ps: 也可再安裝個kibana工具玩,這個可以直接對ES上的數據進行CRUD,有界面的哦。這個后面再介紹

新建SpringBoot項目

網上一大堆,這里就不說了,新建時注意選擇版本為 2.2.3.BUILD-SNAPSHOT

二、集成Elasticsearch

1、設置Elasticsearch版本

<elasticsearch.version>7.5.1</elasticsearch.version>

2、添加Elasticsearch包依賴

<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
	<groupId>org.mybatis.spring.boot</groupId>
	<artifactId>mybatis-spring-boot-starter</artifactId>
	<version>1.3.2</version>
</dependency>
<!-- elasticsearch配置 -->
<dependency>
	<groupId>org.elasticsearch.client</groupId>
	<artifactId>elasticsearch-rest-high-level-client</artifactId>
	<version>${elasticsearch.version}</version>
</dependency>
<dependency>
	<groupId>org.elasticsearch</groupId>
	<artifactId>elasticsearch</artifactId>
	<version>${elasticsearch.version}</version>
</dependency>

<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-test</artifactId>
	<scope>test</scope>
	<exclusions>
		<exclusion>
			<groupId>org.junit.vintage</groupId>
			<artifactId>junit-vintage-engine</artifactId>
		</exclusion>
	</exclusions>
</dependency>

<!-- https://mvnrepository.com/artifact/com.google.code.gson/gson -->
<dependency>
	<groupId>com.google.code.gson</groupId>
	<artifactId>gson</artifactId>
	<version>2.8.6</version>
</dependency>
<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-configuration-processor</artifactId>
	<optional>true</optional>
</dependency>

三、配置並連接Elasticsearch服務器

1、修改application.yml文件,添加如下配置

server:
  port: 8082
elasticsearch:
  host: localhost
  port: 9200

這個就不作解決了,大家都能看懂

2、新建Elasticsearch配置類EsConfig,並生成RestHighLevelClient類

RestHighLevelClient類此類用於連接和操作Elasticsearch服務端數據

ps: 此處配置依賴以下配置, 並且類上的注解一定要再添加個@Component注解

<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-configuration-processor</artifactId>
   <optional>true</optional>
</dependency>

EsConfig類:

@Component
@ConfigurationProperties(prefix = "elasticsearch")
public class EsConfig {
    private String host;
    private Integer port;

    @Bean(destroyMethod = "close")
    public RestHighLevelClient client() {
        return new RestHighLevelClient(RestClient.builder(
                new HttpHost(host, port, "http")
        ));
    }

    public String getHost() {
        return host;
    }

    public void setHost(String host) {
        this.host = host;
    }

    public Integer getPort() {
        return port;
    }

    public void setPort(Integer port) {
        this.port = port;
    }
}

四、測試看效果

1、創建HelloWorldController類

@RestController
public class HelloWorldController {
    @Resource
    private RestHighLevelClient restHighLevelClient;
    private String indexName = "indexname";
    
    @RequestMapping(value = "hello")
    public String hello() {
        
        Map<String, Object> query = new HashMap<>();

        try {
            Goods goods = new Goods(1L,"Gary1", "gggggggggggggggggggggggggg", "one");
            // 注意indexId是唯一的
            String indexId = "test001";
            add(goods, indexId);

            goods = new Goods(2L,"Gary2", "kkkkkkkkkkkkkkkkkkkk", "two");
            // 注意indexId是唯一的
            indexId = "test002";
            add(goods, indexId);
            
            query = this.query("test002");

        } catch (IOException e) {
            e.printStackTrace();
        }
        return "Hello World!" + query.toString();
    }
    
   private boolean add(Goods goods, String indexId) throws IOException {
        String json = new Gson().toJson(goods);
        IndexRequest request = new IndexRequest(indexName).id(indexId).source(json, XContentType.JSON);
        restHighLevelClient.index(request, RequestOptions.DEFAULT);

        return true;
    }
    
    private Map<String, Object> query(String indexId) throws IOException {
        GetRequest request = new GetRequest(indexName, indexId);
        GetResponse response = restHighLevelClient.get(request, RequestOptions.DEFAULT);
        
        return response.getSource();
    }
}

2、添加Goods類

public class Goods {
    public Goods(Long id, String name, String description){
        this.id = id;
        this.name = name;
        this.description = description;
    }

    private Long id;

    private String name;

    private String description;
    ……
get and set此處略,自己添加
}

3、啟動服務查看結果

地址:http://localhost:8082/hello


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM