feat:es配置
parent
e7608cbb20
commit
bfe0341247
20
pom.xml
20
pom.xml
|
|
@ -32,6 +32,8 @@
|
|||
<jwt.version>0.9.1</jwt.version>
|
||||
<mybatisplus.version>3.5.10.1</mybatisplus.version>
|
||||
<hutool.version>5.8.36</hutool.version>
|
||||
<elasticsearch.version>8.17.4</elasticsearch.version>
|
||||
<jakarta.json-api.version>2.1.1</jakarta.json-api.version>
|
||||
<!-- override dependency version -->
|
||||
<tomcat.version>9.0.98</tomcat.version>
|
||||
<logback.version>1.2.13</logback.version>
|
||||
|
|
@ -248,6 +250,24 @@
|
|||
<version>${aliyun-sdk-oss.version}</version>
|
||||
</dependency>
|
||||
|
||||
<!-- ES相关 begin-->
|
||||
<dependency>
|
||||
<groupId>co.elastic.clients</groupId>
|
||||
<artifactId>elasticsearch-java</artifactId>
|
||||
<version>${elasticsearch.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.elasticsearch.client</groupId>
|
||||
<artifactId>elasticsearch-rest-client</artifactId>
|
||||
<version>${elasticsearch.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>jakarta.json</groupId>
|
||||
<artifactId>jakarta.json-api</artifactId>
|
||||
<version>${jakarta.json-api.version}</version>
|
||||
</dependency>
|
||||
<!-- ES相关 end-->
|
||||
|
||||
</dependencies>
|
||||
</dependencyManagement>
|
||||
|
||||
|
|
|
|||
|
|
@ -22,7 +22,14 @@ oss:
|
|||
roleArn: acs:ram::1919425406190533:role/sts-role
|
||||
expiredDuration: 3600
|
||||
tempDir: E:/temp/
|
||||
|
||||
es:
|
||||
#多个用","分割
|
||||
hosts: 49.234.41.39:9222
|
||||
ssl: false
|
||||
username: elastic
|
||||
password: eguMAH-Fg*dPETkgJTkD
|
||||
maxConnTotal: 4
|
||||
maxConnPerRoute: 2
|
||||
# 开发环境配置
|
||||
server:
|
||||
# 服务器的HTTP端口,默认为9310
|
||||
|
|
|
|||
|
|
@ -122,6 +122,12 @@
|
|||
<artifactId>spring-boot-starter-data-redis</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- es -->
|
||||
<dependency>
|
||||
<groupId>co.elastic.clients</groupId>
|
||||
<artifactId>elasticsearch-java</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- pool 对象池 -->
|
||||
<dependency>
|
||||
<groupId>org.apache.commons</groupId>
|
||||
|
|
|
|||
|
|
@ -0,0 +1,30 @@
|
|||
package com.ruoyi.framework.config;
|
||||
|
||||
import cn.hutool.core.bean.BeanUtil;
|
||||
import com.ruoyi.framework.config.properties.EsProperties;
|
||||
import com.ruoyi.framework.es.EsClientWrapper;
|
||||
import com.ruoyi.framework.es.EsConfiguration;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
/**
|
||||
* @author liangyq
|
||||
* @date 2025-03-24
|
||||
*/
|
||||
@Configuration
|
||||
@ConditionalOnClass(EsClientWrapper.class)
|
||||
@ConditionalOnProperty("es.hosts")
|
||||
@EnableConfigurationProperties(EsProperties.class)
|
||||
public class EsAutoConfiguration {
|
||||
|
||||
@Bean
|
||||
@ConditionalOnMissingBean
|
||||
public EsClientWrapper esClient(EsProperties properties) {
|
||||
EsConfiguration config = BeanUtil.toBean(properties, EsConfiguration.class);
|
||||
return new EsClientWrapper(config);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,26 @@
|
|||
package com.ruoyi.framework.config.properties;
|
||||
|
||||
import lombok.Data;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
|
||||
/**
|
||||
* @author liangyq
|
||||
* @date 2025-03-24
|
||||
*/
|
||||
@Data
|
||||
@ConfigurationProperties(prefix = "es")
|
||||
public class EsProperties {
|
||||
|
||||
private String hosts;
|
||||
|
||||
private boolean ssl;
|
||||
|
||||
private String username;
|
||||
|
||||
private String password;
|
||||
|
||||
private int maxConnTotal;
|
||||
|
||||
private int maxConnPerRoute;
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,67 @@
|
|||
package com.ruoyi.framework.es;
|
||||
|
||||
import co.elastic.clients.elasticsearch.ElasticsearchClient;
|
||||
import co.elastic.clients.json.jackson.JacksonJsonpMapper;
|
||||
import co.elastic.clients.transport.ElasticsearchTransport;
|
||||
import co.elastic.clients.transport.rest_client.RestClientTransport;
|
||||
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;
|
||||
|
||||
/**
|
||||
* @author liangyq
|
||||
* @date 2025-03-28 10:36
|
||||
*/
|
||||
@Slf4j
|
||||
public class EsClientWrapper {
|
||||
|
||||
private final RestClient restClient;
|
||||
|
||||
private final ElasticsearchClient esClient;
|
||||
|
||||
public EsClientWrapper(EsConfiguration config) {
|
||||
String[] hosts = config.getHosts().split(",");
|
||||
String scheme = config.isSsl() ? "https" : "http";
|
||||
HttpHost[] httpHosts = new HttpHost[hosts.length];
|
||||
int i = 0;
|
||||
for (String host : hosts) {
|
||||
String[] args = host.split(":");
|
||||
httpHosts[i++] = (new HttpHost(args[0], Integer.parseInt(args[1]), scheme));
|
||||
}
|
||||
RestClientBuilder clientBuilder;
|
||||
if (null != config.getUsername() && null != config.getPassword()) {
|
||||
clientBuilder = RestClient.builder(httpHosts).setHttpClientConfigCallback(httpClientBuilder -> {
|
||||
httpClientBuilder.setMaxConnTotal(config.getMaxConnTotal());
|
||||
httpClientBuilder.setMaxConnPerRoute(config.getMaxConnPerRoute());
|
||||
CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
|
||||
credentialsProvider.setCredentials(AuthScope.ANY,
|
||||
new UsernamePasswordCredentials(config.getUsername(), config.getPassword()));
|
||||
return httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider);
|
||||
});
|
||||
} else {
|
||||
clientBuilder = RestClient.builder(httpHosts).setHttpClientConfigCallback(httpClientBuilder -> {
|
||||
httpClientBuilder.setMaxConnTotal(config.getMaxConnTotal());
|
||||
httpClientBuilder.setMaxConnPerRoute(config.getMaxConnPerRoute());
|
||||
return httpClientBuilder;
|
||||
});
|
||||
}
|
||||
restClient = clientBuilder.build();
|
||||
// 使用 Jackson 映射器创建传输
|
||||
ElasticsearchTransport transport = new RestClientTransport(restClient, new JacksonJsonpMapper());
|
||||
esClient = new ElasticsearchClient(transport);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取es客户端
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public ElasticsearchClient getEsClient() {
|
||||
return esClient;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,24 @@
|
|||
package com.ruoyi.framework.es;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* @author liangyq
|
||||
* @date 2025-03-28 10:36
|
||||
*/
|
||||
@Data
|
||||
public class EsConfiguration {
|
||||
|
||||
private String hosts;
|
||||
|
||||
private boolean ssl;
|
||||
|
||||
private String username;
|
||||
|
||||
private String password;
|
||||
|
||||
private int maxConnTotal;
|
||||
|
||||
private int maxConnPerRoute;
|
||||
|
||||
}
|
||||
Loading…
Reference in New Issue