Skip to main content

dawdler-es-core

Module Introduction​

The ES plugin, a connection pool built by wrapping elasticsearch-java with pool2.

1. Adding the Dependency to pom​

<groupId>club.dawdler</groupId>
<artifactId>dawdler-es-core</artifactId>

2. properties File Description​

# ES host list, separated by commas; each entry is host:port, port can be omitted and defaults to 9200 when omitted
hosts=192.168.1.111:9200,192.168.1.112:9200,192.168.1.113:9200

# Username; if not set, leave it empty
username=myUsername

# Password; if not set, leave it empty
password=myPassword

# The following parameters need to be configured after enabling HTTPS connections

# Certificate path; the certificate file must be in the project's classPath
keystorePath=my.jks (or my.p12)

# Certificate password
keystorePassword=myPassword

# Whether to trust self-signed certificates (self-signed certificates outside the trust store are also allowed) Default false, recommended only for test environments
# When false, the trust store works as usual, and certificates outside the trust store always go through the JVM default certificate chain verification
trustSelfSigned=false

# Whether to verify that the hostname matches the certificate Default true, recommended to disable only in test environments
verifyHostname=true

# Timeout settings (unit: milliseconds; default values are consistent with elasticsearch-java Rest5Client; set to -1 or 0 to leave them unset and use the client's default behavior)

# Timeout for obtaining a connection from the HTTP connection pool Default 30000
connectionRequestTimeout=30000

# Timeout for establishing a connection Default 1000
connectTimeout=1000

# Response timeout Default 30000
socketTimeout=30000

# Connection pool settings

# Maximum number of connections
pool.maxTotal=20

# Minimum number of idle connections
pool.minIdle=2

# Maximum number of idle connections
pool.maxIdle=8

# Maximum wait time (unit: milliseconds) (default: 30000); when the pool is exhausted, throws an exception on timeout instead of blocking the calling thread forever
pool.maxWaitMillis=10000

# Whether to validate a connection when it is borrowed Default false
pool.testOnBorrow=false

# Whether to validate a connection when it is created Default false
pool.testOnCreate=false

# Whether to validate a connection when it is returned Default false
pool.testOnReturn=false

The configuration file supports multiple environments, a unified configuration center, encryption, and variable substitution; see multi-environment configuration.

3. EsInjector Annotation​

Used to inject EsOperator; the methods of EsOperator refer to the official documentation of the ElasticsearchClient class.

The value of the EsInjector annotation takes fileName as the configuration file name (excluding the .properties suffix).

For details, see:

dawdler-server-plug-es provides injection support on the dawdler-server side.

dawdler-client-plug-es provides injection support on the web side.

4. Usage Outside the dawdler Architecture​

// Obtain the factory instance by calling the getInstance method of ElasticSearchClientFactory

public static ElasticSearchClientFactory getInstance(String fileName) throws Exception;

// fileName is the configuration file name excluding the .properties suffix.

// Call the getElasticSearchClient method on the factory instance to obtain an ElasticSearchClient object, then call the getElasticsearchClient method of the ElasticSearchClient object to obtain an ElasticsearchClient object for operating on ES. When the call is finished, call the close method of ElasticSearchClient to return the object to the connection pool.

// For example: if fileName is myEs, a configuration file myEs.properties needs to be created in the project's classPath.

Note: ElasticSearchClientFactory does not need to be closed manually when running on the client or server side; dawdler closes the related resources automatically.

When used outside the dawdler architecture, you need to call ElasticSearchClientFactory.shutdownAll(); to release resources.