es与springboot整合,Kibana 版本下载

kibana与es版本号要保持一致:

https://artifacts.elastic.co/downloads/kibana/kibana-6.2.1-windows-x86_64.zip

https://artifacts.elastic.co/downloads/kibana/kibana-5.6.11-linux-x86_64.tar.gz

es在与springboot整合的时候经常报找不到es的节点的问题。有以下两步操作

network.host: 0.0.0.0
transport.tcp.port: 9300
http.port: 9200

将网络设置访问为都可以。

最关键的点是要查看

spring-data-elasticsearch包下引入的是es的哪个版本,
需要将这两个版本对应上,包括kibana版本都需要同一个版本号

引入的pom

<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-elasticsearch</artifactId>
</dependency>
<dependency>
    <groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.2</version>
</dependency>


controller:
@RestController
@RequestMapping("/es")
public class ElasticSearchController {

@Autowired
private EmployeeRepository er;

//增加
@RequestMapping("/add/{id}")
public String add(@PathVariable("id")String id){

Employee employee=new Employee();
employee.setId(id);
employee.setFirstName("Y.S.K");
employee.setLastName("~");
employee.setAge(26);
employee.setAbout("");
er.save(employee);

System.err.println("add a obj");

return "success";
}

//删除
@RequestMapping("/delete")
public String delete(){
Employee employee=new Employee();
employee.setId("1");
er.delete(employee);

return "success";
}

//局部更新
@RequestMapping("/update")
public String update(){

Employee employee=er.queryEmployeeById("1");
employee.setFirstName("哈哈");
er.save(employee);

System.err.println("update a obj");

return "success";
}

//查询
@RequestMapping("/query/{id}")
public Employee query(@PathVariable("id")String id){

Employee accountInfo=er.queryEmployeeById(id);
System.err.println(new Gson().toJson(accountInfo));

return accountInfo;
}

}
EmployeeRepository:
@Component
public interface EmployeeRepository extends ElasticsearchRepository<Employee,String> {
Employee queryEmployeeById(String id);
}
实体类:
@Data
@Document(indexName = "megacorp",type = "employee")
public class Employee {
@Id
private String id;
private String firstName;
private String lastName;

private Integer age = 0;
private String about;
}

yml:
spring:
data:
elasticsearch:
cluster-name: elasticsearch
cluster-nodes: 192.168.6.22:9300
repositories:
enabled: true
扫描二维码关注公众号,回复: 6901534 查看本文章

猜你喜欢

转载自www.cnblogs.com/woshixiangshang/p/11267950.html