赞
踩
背景:阿里云服务器,搭建Docker,部署ElasticSearch镜像,本地SpringBoot链接ES,实现增删改查
安装Docker:https://blog.csdn.net/BinziD/article/details/111224764
Docker安装ElasticSearch:https://blog.csdn.net/BinziD/article/details/111314796
1. 配置文件(配置项不能错)
- spring.data.elasticsearch.cluster-name=es-log
- spring.data.elasticsearch.cluster-nodes=39.99.48.28:9300
- spring.data.elasticsearch.repositories.enabled=true
2. pom.xml文件
- <!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
- <dependency>
- <groupId>com.alibaba</groupId>
- <artifactId>fastjson</artifactId>
- <version>1.2.62</version>
- </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>
- <version>2.0.2.RELEASE</version>
- </dependency>
- <dependency>
- <groupId>org.projectlombok</groupId>
- <artifactId>lombok</artifactId>
- <version>1.18.8</version>
- </dependency>
3. Entity
- package org.big.entity;
-
- import org.springframework.data.annotation.Id;
- import org.springframework.data.elasticsearch.annotations.Document;
- import org.springframework.data.elasticsearch.annotations.Field;
-
- import lombok.Getter;
- import lombok.Setter;
- import lombok.ToString;
-
- @Getter
- @Setter
- @ToString
- @Document(indexName = "company",type = "employee", shards = 1,replicas = 0, refreshInterval = "-1")
- public class Employee {
- @Id
- private String id;
- @Field
- private String firstName;
- @Field
- private String lastName;
- @Field
- private Integer age = 0;
- @Field
- private String about;
- }
4. Controller层
- package org.big.controller;
-
- import org.big.entity.Employee;
- import org.big.repository.EmployeeRepository;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RestController;
-
- import com.alibaba.fastjson.JSON;
-
- @RestController
- //@RequestMapping("es")
- public class EmployeeController {
- @Autowired
- private EmployeeRepository employeeRepository;
-
- /**
- * 添加
- * @return
- */
- @RequestMapping("add")
- public String add() {
- Employee employee = new Employee();
- employee.setId("1");
- employee.setFirstName("xuxu");
- employee.setLastName("zh");
- employee.setAge(26);
- employee.setAbout("i am in peking");
- employeeRepository.save(employee);
- System.err.println("add a obj");
- return "success";
- }
-
- /**
- * 删除
- * @return
- */
- @RequestMapping("delete")
- public String delete() {
- Employee employee = employeeRepository.queryEmployeeById("1");
- employeeRepository.delete(employee);
- return "success";
- }
-
- /**
- * 局部更新
- * @return
- */
- @RequestMapping("update")
- public String update() {
- Employee employee = employeeRepository.queryEmployeeById("1");
- employee.setFirstName("哈哈");
- employeeRepository.save(employee);
- System.err.println("update a obj");
- return "success";
- }
- /**
- * 查询
- * @return
- */
- @RequestMapping("query")
- public Employee query() {
- Employee accountInfo = employeeRepository.queryEmployeeById("1");
- System.err.println(JSON.toJSONString(accountInfo));
- return accountInfo;
- }
- }
5. Repository层
- package org.big.repository;
-
- import org.big.entity.Employee;
- import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
- import org.springframework.stereotype.Component;
- @Component
- public interface EmployeeRepository extends ElasticsearchRepository<Employee, String> {
- /**
- * 通过Id查询Employee对象
- * @param id
- * @return
- */
- Employee queryEmployeeById(String id);
- }
6.访问:http://localhost:8080/add
7. 问题:org.elasticsearch.client.transport.NoNodeAvailableException
解决:端口须指定为9300
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。