ElasticSearch —— 整合springboot(二)

首先保證elasticsearch、kibana 跑起來之後,再初始化一個springboot基礎空項目,能跑起來就可以了。

 

pom.xml文件:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
   <modelVersion>4.0.0</modelVersion>
   <parent>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-parent</artifactId>
      <version>2.2.6.RELEASE</version>
      <relativePath/> <!-- lookup parent from repository -->
   </parent>
   <groupId>com.example</groupId>
   <artifactId>test_three</artifactId>
   <version>0.0.1-SNAPSHOT</version>
   <name>test_three</name>
   <description>Demo project for Spring Boot</description>

   <properties>
      <java.version>1.8</java.version>
   </properties>

   <dependencies>
      <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-web</artifactId>
      </dependency>


      <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-data-elasticsearch -->
      <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
      </dependency>


      <!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
      <dependency>
         <groupId>org.projectlombok</groupId>
         <artifactId>lombok</artifactId>
         <version>1.18.10</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>
   </dependencies>

   <build>
      <plugins>
         <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
         </plugin>
      </plugins>
   </build>

</project> 

application.yml文件:

spring:
    data:
        elasticsearch:
            #集羣名稱
            cluster-name: myes
            #地址
            cluster-nodes: localhost:9300

 

package com.example.test_three.entity;

import lombok.Data;
import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.Document;

/**
 * 周前梅
 * 2020/5/6
 */
@Document(indexName = "mymayikt",type = "user")
@Data
public class UserEntity {

    @Id
    private String id ;

    private String name ;

    private Integer age ;

    private Integer sex ;
}
package com.example.test_three.repository;

import com.example.test_three.entity.UserEntity;
import org.springframework.data.repository.CrudRepository;

/**
 * 周前梅
 * 2020/5/6
 */
public interface UserDao extends CrudRepository<UserEntity,String>{
}

 

package com.example.test_three.controller;

import com.example.test_three.entity.UserEntity;
import com.example.test_three.repository.UserDao;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Optional;

/**
 * 周前梅
 * 2020/5/6
 */
@RestController
public class ESController {

    @Resource
    private UserDao userDao ;

    //添加文檔
    @RequestMapping(value = "/addUser")
    public UserEntity addUser(@RequestBody UserEntity userEntity) {
        return userDao.save(userEntity);
    }

    //查詢文檔
    @RequestMapping(value = "/findOne")
    public Optional<UserEntity> findOne(String id){
        return userDao.findById(id) ;
    }
}
package com.example.test_three;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.data.elasticsearch.repository.config.EnableElasticsearchRepositories;

@SpringBootApplication
@EnableElasticsearchRepositories(basePackages = "com.example.test_three.repository")
public class TestThreeApplication {

   public static void main(String[] args) {
      SpringApplication.run(TestThreeApplication.class, args);
   }

}

項目跑起來之後就可以測試了,我們通常都是使用postman測試接口的

備註一下:9300與9200的區別

    9300端口:ES節點之間通訊使用  。是TCP協議端口號,ES集羣之間通訊端口號

    9200端口:ES節點  和 外部通訊使用。是端口號,暴露 ES  RESTFul 接口端口號

       

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章