1.struts2初始化
private RestHighLevelClient client = getClient();
public RestHighLevelClient getClient() {
int num = EsConfiguration.hostList.size();
HttpHost[] hosts = new HttpHost[num];
for (int i = 0; i < num; i++) {
hosts[i] = EsConfiguration.hostList.get(i);
}
this.client = new RestHighLevelClient(RestClient.builder(hosts));
return client;
}
EsConfiguration.java
private static String hosts = "127.0.0.1,192.168.0.111"; // 测试地址,多个用,隔开
private static int port = 9200;
private static String schema = "http"; // 使用的协议
static ArrayList<HttpHost> hostList = null;
static {
hostList = new ArrayList<>();
String[] hostStrs = hosts.split(",");
for (String host : hostStrs) {
hostList.add(new HttpHost(host, port, schema));
}
}
public static ArrayList<HttpHost> getHostList() {
return hostList;
}
public static void setHostList(ArrayList<HttpHost> hostList) {
EsConfiguration.hostList = hostList;
}
public RestHighLevelClient client() {
RestClientBuilder builder = RestClient.builder(hostList.toArray(new HttpHost[0]));
// 异步httpclient连接延时配置
// builder.setRequestConfigCallback(new RequestConfigCallback() {
// @Override
// public Builder customizeRequestConfig(Builder requestConfigBuilder) {
// requestConfigBuilder.setConnectTimeout(connectTimeOut);
// requestConfigBuilder.setSocketTimeout(socketTimeOut);
// requestConfigBuilder.setConnectionRequestTimeout(connectionRequestTimeOut);
// return requestConfigBuilder;
// }
// });
// //异步httpclient连接数配置
// builder.setHttpClientConfigCallback(new HttpClientConfigCallback() {
// @Override
// public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpClientBuilder) {
// httpClientBuilder.setMaxConnTotal(maxConnectNum);
// httpClientBuilder.setMaxConnPerRoute(maxConnectPerRoute);
// return httpClientBuilder;
// }
// });
RestHighLevelClient client = new RestHighLevelClient(builder);
return client;
}
2.SpringMvc初始化
@Autowired
private RestHighLevelClient client;
3.需要的jar包
3.1 Maven项目
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>7.2.0</version>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>7.2.0</version>
<exclusions>
<exclusion>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
</exclusion>
</exclusions>
</dependency>
3.2 具体jar包列表
以上,如果可以帮到你,留个赞让我知道好吗?