Skip to content

Commit

Permalink
添加es 示例demo工程
Browse files Browse the repository at this point in the history
  • Loading branch information
liuyueyi committed Feb 25, 2022
1 parent 24b9147 commit fd3bae1
Show file tree
Hide file tree
Showing 8 changed files with 220 additions and 0 deletions.
4 changes: 4 additions & 0 deletions spring-boot/142-search-es/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.*
target/*
*.iml
!.gitignore
30 changes: 30 additions & 0 deletions spring-boot/142-search-es/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>spring-boot</artifactId>
<groupId>com.git.hui.boot</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>142-search-es</artifactId>

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

<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
</dependency>
</dependencies>

</project>
48 changes: 48 additions & 0 deletions spring-boot/142-search-es/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# es实例demo

## es基本case

权限验证开启

修改配置文件 `config/elasticsearch.yml`,添加下面的配置

```yaml
xpack.security.enabled: true
xpack.security.authc.accept_default_password: false
```
启动es服务
```bash
bin/elasticsearch
```

生成密码

```bash
# 执行完毕之后输入密码, 比如测试的密码都是 test123 (生产环境不要这么干)
bin/elasticsearch-setup-passwords interactive
```

es的交互,主要使用 `Basic Auth` 方式进行身份校验,简单来讲,就是在请求头中,添加

```bash
Authorization: Basic ZWxhc3RpYzp0ZXN0MTIz
```

## 数据准备

```json
POST first-index/_doc
{
"message": "GET /search HTTP/1.1 200 1070000",
"user": {
"id": "kimchy",
"name": "YiHui"
},
"hobby": [
"java",
"python"
]
}
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.git.hui.boot.search.es;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
* @author yihui
* @date 2022/1/7
*/
@SpringBootApplication
public class Application {

public Application(EsTest esTest) throws Exception {
esTest.testGet();
}

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

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package com.git.hui.boot.search.es;

import lombok.Getter;
import org.apache.http.HttpHost;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Getter
@Configuration
public class ElasticsearchConfiguration {

@Value("${elasticsearch.host}")
private String host;

@Value("${elasticsearch.port}")
private int port;

@Value("${elasticsearch.connTimeout}")
private int connTimeout;

@Value("${elasticsearch.socketTimeout}")
private int socketTimeout;

@Value("${elasticsearch.connectionRequestTimeout}")
private int connectionRequestTimeout;

@Value("${elasticsearch.user}")
private String user;

@Value("${elasticsearch.pwd}")
private String pwd;

@Bean(destroyMethod = "close", name = "client")
public RestHighLevelClient initRestClient() {
RestClientBuilder builder = RestClient.builder(new HttpHost(host, port))
.setRequestConfigCallback(requestConfigBuilder -> requestConfigBuilder
.setConnectTimeout(connTimeout)
.setSocketTimeout(socketTimeout)
.setConnectionRequestTimeout(connectionRequestTimeout));
return new RestHighLevelClient(builder);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package com.git.hui.boot.search.es;

import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Base64Utils;

import java.util.Map;


/**
* @author yihui
* @date 2022/1/7
*/
@Service
public class EsTest {
@Autowired
private RestHighLevelClient client;

private static String auth = "Basic ZWxhc3RpYzp0ZXN0MTIz";

public EsTest(ElasticsearchConfiguration elasticsearchConfiguration) {
auth = Base64Utils.encodeToString((elasticsearchConfiguration.getUser() + ":" + elasticsearchConfiguration.getPwd()).getBytes());
auth = "Basic " + auth;
}

public void testGet() throws Exception {
GetRequest getRequest = new GetRequest("first-index", "_doc", "gvarh3gBF9fSFsHNuO49");
RequestOptions.Builder requestOptions = RequestOptions.DEFAULT.toBuilder();
requestOptions.addHeader("Authorization", auth);
GetResponse getResponse = client.get(getRequest, requestOptions.build());
if (getResponse.isExists()) {
String sourceAsString = getResponse.getSourceAsString();
System.out.println(sourceAsString);
} else {
System.out.println("no string!");
}
}

public Map<String, Object> searchDataById(String index, String type, String id, String fields) {
try {
GetRequest getRequest = new GetRequest(index, type, id);
RequestOptions.Builder requestOptions = RequestOptions.DEFAULT.toBuilder();
requestOptions.addHeader("Authorization", auth);
GetResponse getResponse = client.get(getRequest, requestOptions.build());
return getResponse.getSource();
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
}
15 changes: 15 additions & 0 deletions spring-boot/142-search-es/src/main/resources/application.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
spring:
elasticsearch:
rest:
uris: http://localhost:9200
username: elastic
password: test123

elasticsearch:
host: localhost
port: 9200
user: elastic
pwd: test123
connTimeout: 3000
socketTimeout: 5000
connectionRequestTimeout: 500
1 change: 1 addition & 0 deletions spring-boot/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@
<module>411-zookeeper-distributelock</module>
<module>420-prometheus-basic</module>
<module>421-prometheus-micro</module>
<module>142-search-es</module>

<module>430-email</module>

Expand Down

0 comments on commit fd3bae1

Please sign in to comment.