Skip to content

Commit 2449a6e

Browse files
committed
新增es 配置
1 parent 2fa422e commit 2449a6e

File tree

3 files changed

+69
-2
lines changed

3 files changed

+69
-2
lines changed

springboot-elasticsearch/pom.xml

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,30 @@
4646
<artifactId>mysql-connector-java</artifactId>
4747
<version>5.1.40</version>
4848
</dependency>
49-
5049
<dependency>
5150
<groupId>org.apache.commons</groupId>
5251
<artifactId>commons-lang3</artifactId>
5352
<version>3.4</version>
5453
</dependency>
54+
<!--es-->
55+
<dependency>
56+
<groupId>com.sun.jna</groupId>
57+
<artifactId>jna</artifactId>
58+
<version>3.0.9</version>
59+
</dependency>
60+
61+
<dependency>
62+
<groupId>org.elasticsearch.client</groupId>
63+
<artifactId>rest</artifactId>
64+
<version>5.5.3</version>
65+
</dependency>
66+
67+
<dependency>
68+
<groupId>com.alibaba</groupId>
69+
<artifactId>fastjson</artifactId>
70+
<version>1.2.40</version>
71+
</dependency>
72+
5573
</dependencies>
5674

5775
</project>
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package cn.abel.config;
2+
3+
import org.apache.http.HttpHost;
4+
import org.apache.http.auth.AuthScope;
5+
import org.apache.http.auth.UsernamePasswordCredentials;
6+
import org.apache.http.client.CredentialsProvider;
7+
import org.apache.http.impl.client.BasicCredentialsProvider;
8+
import org.apache.http.impl.nio.client.HttpAsyncClientBuilder;
9+
import org.elasticsearch.client.RestClient;
10+
import org.elasticsearch.client.RestClientBuilder;
11+
import org.springframework.beans.factory.annotation.Value;
12+
import org.springframework.context.annotation.Bean;
13+
import org.springframework.context.annotation.Configuration;
14+
15+
/**
16+
* @author yangyibo
17+
* @time 2019/4/2
18+
*/
19+
@Configuration
20+
public class ESRestClientConfig {
21+
@Value("${elasticsearch.userName}")
22+
private String userName;
23+
@Value("${elasticsearch.password}")
24+
private String password;
25+
@Value("${elasticsearch.rest.hostNames}")
26+
private String hostName;
27+
@Value("${elasticsearch.rest.port}")
28+
private Integer port;
29+
30+
@Bean(destroyMethod = "close")
31+
public RestClient getRestClient() {
32+
final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
33+
credentialsProvider.setCredentials(AuthScope.ANY,
34+
new UsernamePasswordCredentials(userName, password));
35+
RestClientBuilder restClientBuilder = RestClient.builder(new HttpHost(hostName, port));
36+
//配置身份验证
37+
restClientBuilder.setHttpClientConfigCallback(new RestClientBuilder.HttpClientConfigCallback() {
38+
@Override
39+
public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpClientBuilder) {
40+
return httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider);
41+
}
42+
});
43+
return restClientBuilder.build();
44+
}
45+
}

springboot-elasticsearch/src/main/resources/local/application.properties

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,4 +56,8 @@ pagehelper.reasonable=true
5656
pagehelper.supportMethodsArguments=true
5757
pagehelper.params=count=countSql
5858

59-
59+
#es\u670D\u52A1\u5668\u5730\u5740\u914D\u7F6E
60+
elasticsearch.new.userName=
61+
elasticsearch.new.password=
62+
elasticsearch.rest.new.hostNames=127.0.0.1
63+
elasticsearch.rest.new.port=9200

0 commit comments

Comments
 (0)