配置nginx
location /elastic/ {
rewrite ^/elastic/(.*)$ /$1 break;
proxy_pass http://172.21.31.2:9200;
proxy_read_timeout 300s;
proxy_set_header Host $host:$server_port;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_http_version 1.1;
}
配置pom文件
<elasticsearch-rest-high-level-client.version>7.17.10</elasticsearch-rest-high-level-client.version>
<elasticsearch.version>7.17.10</elasticsearch.version>
<!-- Elasticsearch REST High Level -->
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>${elasticsearch-rest-high-level-client.version}</version>
</dependency>
<!-- elasticsearch -->
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>${elasticsearch.version}</version>
</dependency>
配置文件
elasticsearch:
host: 域名
prefix: 前缀
port: -1
protocol: http
username: username
password: password
创建RestHighLevelClient连接
import cn.hutool.extra.spring.SpringUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpHost;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.nio.client.HttpAsyncClientBuilder;
import org.elasticsearch.client.Node;
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.stereotype.Component;
@Component
@Slf4j
public class ElasticsearchConfig {
/**
* ES请求地址
*/
@Value("${elasticsearch.host}")
private String host;
/**
* ES请求地址
*/
@Value("${elasticsearch.prefix}")
private String prefix;
/**
* 端口
*/
@Value("${elasticsearch.port}")
private int port;
/**
* 协议
*/
@Value("${elasticsearch.protocol}")
private String protocol;
/**
* 账号
*/
@Value("${elasticsearch.username}")
private String username;
/**
* 密码
*/
@Value("${elasticsearch.password}")
private String password;
@Value("${spring.profiles.active}")
private String active;
@Bean(name = "restHighLevelClientPre")
public RestHighLevelClient getRestHighLevelClientPre() {
// 创建身份凭证
final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(username, password));
// 生产环境可以通过局域网的方式进行连接
if("prod".equals(SpringUtil.getActiveProfile())) {
RestClientBuilder restClientBuilder = RestClient.builder(new HttpHost(host, port, protocol));
return new RestHighLevelClient(restClientBuilder
.setHttpClientConfigCallback(auth -> auth.setDefaultCredentialsProvider(credentialsProvider)));
}
// 创建远程连接
RestHighLevelClient restClient = new RestHighLevelClient(RestClient.builder(
HttpHost.create(host)).setPathPrefix(prefix)
.setHttpClientConfigCallback(
httpAsyncClientBuilder -> {
RequestConfig.Builder requestConfigBuilder = RequestConfig.custom()
.setConnectTimeout(10 * 60 * 1000)
.setSocketTimeout(10 * 60 * 1000);
httpAsyncClientBuilder.setDefaultRequestConfig(requestConfigBuilder.build());
return httpAsyncClientBuilder;
}
)
.setFailureListener(new RestClient.FailureListener() {
@Override
public void onFailure(Node node){
log.info(node.toString() );
}
})
.setHttpClientConfigCallback(auth -> auth.setDefaultCredentialsProvider(credentialsProvider)));
return restClient;
}
}