首先澄清环境
jdk1.8
elasticsearch 5.5.3
idea 2018
创建时间 2019/1/5
前提:要保证es是可以正常使用的 在kibana或者head中正常查询
elk 安装请转到 https://mp.csdn.net/postedit/85798493
上图 目录结构图
1.pom文件
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.ebuy.cloud</groupId>
<artifactId>service-elasticsearch</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>service-elasticsearch</name>
<description>Demo project for Spring Boot</description>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<elasticsearch.version>5.5.3</elasticsearch.version>
<log4j2.version>2.6.2</log4j2.version>
<fastjson.version>1.2.31</fastjson.version>
<commons.lang3.version>3.4</commons.lang3.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>${elasticsearch.version}</version>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>transport</artifactId>
<version>${elasticsearch.version}</version>
</dependency>
<!--fastJSON-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>${fastjson.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
2.客户端类
/**
* 〈一句话功能简述〉<br>
* 〈客户端类〉
*
* @author Administrator
* @create 2019/1/2 0002
*/
package com.ebuy.cloud.serviceelasticsearch.config;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.transport.client.PreBuiltTransportClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.net.InetAddress;
import java.net.UnknownHostException;
/**
* 〈一句话功能简述〉<br>
* 〈客户端类〉
*
* @author geyf
* @create 2019/1/2 0002
*/
@Configuration
public class TransportClient {
private static Logger logger = LoggerFactory.getLogger(TransportClient.class);
@Bean
public PreBuiltTransportClient client()throws UnknownHostException {
// 这里调用的是tcp端口默认为9300 一般未修改 9200为http端口无法使用
InetSocketTransportAddress node=new InetSocketTransportAddress(InetAddress.getByName("192.168.71.146"),9300);
// 修改es 集群名称对应配置文件中的cluster.name
Settings settings= Settings.builder().put("cluster.name","ebuy").build();
PreBuiltTransportClient client=new PreBuiltTransportClient(settings);
client.addTransportAddress(node);
return client;
}
}
3.controller
/**
* 〈一句话功能简述〉<br>
* 〈查询服务〉
*
* @author Administrator
* @create 2019/1/2 0002
*/
package com.ebuy.cloud.serviceelasticsearch.controller;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.action.get.GetResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
/**
* 〈查询服务〉
* @author Administrator
* @create 2019/1/2 0002
*/
@RestController
public class SearchRestController {
@Autowired
private TransportClient client;
@GetMapping("/index")
public String index(){return "index";}
@GetMapping("/get/product/id")
@ResponseBody
public ResponseEntity get (@RequestParam(name="id") String id){
if(id.isEmpty()) {
return new ResponseEntity(HttpStatus.FAILED_DEPENDENCY);
}
// product 为index名称 job为type名称 传入id值
GetResponse response=this.client.prepareGet("product","job",id).get();
if(!response.isExists()) {
return new ResponseEntity(HttpStatus.NOT_FOUND);
}
return new ResponseEntity(response.getSource(),HttpStatus.OK);
}
}
4.application.properties
# Elasticsearch
elasticsearch.cluster.name=ebuy
#elasticsearch.ip=192.168.71.146
#elasticsearch.port=9200
elasticsearch.pool=5
server.port=8081
5.启动类
package com.ebuy.cloud.serviceelasticsearch;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class ServiceElasticsearchApplication {
private static Logger logger = LoggerFactory.getLogger(ServiceElasticsearchApplication.class);
public static void main(String[] args) {
SpringApplication.run(ServiceElasticsearchApplication.class, args);
logger.info("=============== Elasticsearch 启动成功 ==============");
}
}
com.ebuy.cloud.serviceelasticsearch.constants.HttpStatusEnum
com.ebuy.cloud.serviceelasticsearch.constants.ResponseVo
为两个工具类可以根据自己实际情况进行编写 无关紧要
启动项目 便可执行成功