文章目录
1. SpringBoot 项目
首先在 pom.xml 中添加 ES 客户端依赖,这里 SpringBoot 使用的是 2.5.3 版本,默认会引入 ES 7.12.1 版本客户端,所以这里没有写版本号:
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
</dependency>
在 application.properties 配置文件中添加相关 ES 集群连接信息:
#ES集群1连接
elasticsearch.connect1.hosts=192.168.50.16:9200,192.168.50.17:9200,192.168.50.18:9200
elasticsearch.connect1.username=test
elasticsearch.connect1.password=12345
#ES集群2连接
elasticsearch.connect2.hosts=192.168.50.40:9200
elasticsearch.connect2.username=test
elasticsearch.connect2.password=12345
提供一个配置类:
import lombok.Getter;
import lombok.Setter;
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.impl.client.BasicCredentialsProvider;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* ES集群配置
*/
@Setter
@Configuration
@ConfigurationProperties(prefix = "elasticsearch")
public class ElasticsearchConfig {
public static final String ES_CLIENT_1 = "es_client_1";
public static final String ES_CLIENT_2 = "es_client_2";
private Connect connect1;
private Connect connect2;
/**
* 客户端1
*/
@Bean(name = ES_CLIENT_1, destroyMethod = "close")
public RestHighLevelClient restHighLevelClient1() {
return getRestHighLevelClient(connect1);
}
/**
* 客户端2
*/
@Bean(name = ES_CLIENT_2, destroyMethod = "close")
public RestHighLevelClient restHighLevelClient2() {
return getRestHighLevelClient(connect2);
}
private RestHighLevelClient getRestHighLevelClient(Connect connect) {
String[] hosts = connect.getHosts().split(",");
HttpHost[] httpHosts = new HttpHost[hosts.length];
for (int i = 0; i < hosts.length; i++) {
httpHosts[i] = HttpHost.create(hosts[i]);
}
RestClientBuilder restClientBuilder = RestClient.builder(httpHosts);
//配置身份验证
final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(connect.getUsername(), connect.getPassword()));
restClientBuilder.setHttpClientConfigCallback(httpClientBuilder -> httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider));
return new RestHighLevelClient(restClientBuilder);
}
@Setter
@Getter
public static class Connect {
private String hosts;
private String username;
private String password;
}
}
使用方法示例:
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
@Slf4j
@Service
public class SearchServiceImpl implements SearchService {
@Autowired
@Qualifier(ElasticsearchConfig.ES_CLIENT_1)
private RestHighLevelClient esClient1;
@Autowired
@Qualifier(ElasticsearchConfig.ES_CLIENT_2)
private RestHighLevelClient esClient2;
}
2. 普通 Maven 项目
提供一个 elasticsearch.properties 配置文件,添加相关 ES 集群连接信息:
#新闻ES集群
elasticsearch.news.hosts=192.168.50.16:9200,192.168.50.17:9200,192.168.50.18:9200
elasticsearch.news.username=test
elasticsearch.news.password=12345
#社交ES集群
elasticsearch.social.hosts=192.168.50.40:9200
elasticsearch.social.username=test
elasticsearch.social.password=12345
提供一个配置类:
import com.gtcom.constant.Constants;
import com.gtcom.constant.Separators;
import lombok.Getter;
import lombok.Setter;
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.impl.client.BasicCredentialsProvider;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;
import java.io.IOException;
import java.util.Properties;
/**
* ES集群配置
*/
@Slf4j
public class ElasticsearchConfig {
@Setter
private static Connect newsConnect = new Connect();
@Setter
private static Connect socialConnect = new Connect();
private static volatile RestHighLevelClient newsEsClient;
private static volatile RestHighLevelClient socialEsClient;
private static final byte[] NEWS_LOCK = new byte[Constants.ZERO];
private static final byte[] SOCIAL_LOCK = new byte[Constants.ZERO];
static {
Properties props = new Properties();
try {
props.load(ElasticsearchConfig.class.getResourceAsStream("/elasticsearch.properties"));
} catch (IOException e) {
log.error("load elasticsearch config properties exception", e);
}
String newsHost = props.getProperty("elasticsearch.news.hosts");
String newsUserName = props.getProperty("elasticsearch.news.username");
String newsPassword = props.getProperty("elasticsearch.news.password");
newsConnect.setHosts(newsHost);
newsConnect.setUsername(newsUserName);
newsConnect.setPassword(newsPassword);
newsEsClient = init(newsConnect);
String socialHost = props.getProperty("elasticsearch.social.hosts");
String socialUserName = props.getProperty("elasticsearch.social.username");
String socialPassword = props.getProperty("elasticsearch.social.password");
socialConnect.setHosts(socialHost);
socialConnect.setUsername(socialUserName);
socialConnect.setPassword(socialPassword);
socialEsClient = init(socialConnect);
}
/**
* 初始化客户端
*/
private static RestHighLevelClient init(Connect connect) {
String[] hosts = connect.getHosts().split(Separators.COMMA);
HttpHost[] httpHosts = new HttpHost[hosts.length];
for (int i = Constants.ZERO; i < hosts.length; i++) {
httpHosts[i] = HttpHost.create(hosts[i]);
}
RestClientBuilder restClientBuilder = RestClient.builder(httpHosts);
//配置身份验证
final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(connect.getUsername(), connect.getPassword()));
restClientBuilder.setHttpClientConfigCallback(httpClientBuilder -> httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider));
return new RestHighLevelClient(restClientBuilder);
}
@Setter
@Getter
public static class Connect {
private String hosts;
private String username;
private String password;
}
/**
* 获取新闻ES集群客户端
*/
public static RestHighLevelClient getNewsEsClient() {
if (newsEsClient == null) {
synchronized (NEWS_LOCK) {
if (newsEsClient == null) {
newsEsClient = init(newsConnect);
return newsEsClient;
}
}
}
return newsEsClient;
}
/**
* 获取社交ES集群客户端
*/
public static RestHighLevelClient getSocialEsClient() {
if (socialEsClient == null) {
synchronized (SOCIAL_LOCK) {
if (socialEsClient == null) {
socialEsClient = init(socialConnect);
return socialEsClient;
}
}
}
return socialEsClient;
}
}
使用的话可以直接通过配置类中提供的单例方法获取客户端:
RestHighLevelClient newsEsClient = ElasticsearchConfig.getNewsEsClient();
RestHighLevelClient socialEsClient = ElasticsearchConfig.getSocialEsClient();