Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Q
qk_backend
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
quanku
qk_backend
Commits
04113729
Commit
04113729
authored
Jul 26, 2023
by
shiyu
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
es config
parent
df08597c
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
0 additions
and
99 deletions
+0
-99
ch-core/src/main/java/com/wwdz/ch/core/config/ElasticsearchConfig.java
...ain/java/com/wwdz/ch/core/config/ElasticsearchConfig.java
+0
-99
No files found.
ch-core/src/main/java/com/wwdz/ch/core/config/ElasticsearchConfig.java
deleted
100644 → 0
View file @
df08597c
package
com.wwdz.ch.core.config
;
import
co.elastic.clients.elasticsearch.ElasticsearchAsyncClient
;
import
co.elastic.clients.elasticsearch.ElasticsearchClient
;
import
co.elastic.clients.json.jackson.JacksonJsonpMapper
;
import
co.elastic.clients.transport.TransportUtils
;
import
co.elastic.clients.transport.rest_client.RestClientTransport
;
import
org.apache.http.HttpHost
;
import
org.apache.http.auth.AuthScope
;
import
org.apache.http.auth.UsernamePasswordCredentials
;
import
org.apache.http.impl.client.BasicCredentialsProvider
;
import
org.elasticsearch.client.RestClient
;
import
org.springframework.beans.factory.annotation.Value
;
import
org.springframework.context.annotation.Bean
;
import
org.springframework.stereotype.Component
;
import
org.springframework.util.StringUtils
;
import
javax.net.ssl.SSLContext
;
import
java.io.File
;
import
java.io.IOException
;
@Component
public
class
ElasticsearchConfig
{
/**
* 多个ip用逗号隔开
*/
@Value
(
"${spring.elasticsearch.jest.uris}"
)
private
String
elasticsearchUris
;
/**
* 同步方式
* @return
*/
@Bean
public
ElasticsearchClient
elasticsearchClient1
()
{
HttpHost
[]
httpHosts
=
toHttpHost
();
RestClient
restClient
=
RestClient
.
builder
(
httpHosts
).
build
();
RestClientTransport
transport
=
new
RestClientTransport
(
restClient
,
new
JacksonJsonpMapper
());
return
new
ElasticsearchClient
(
transport
);
}
// @Bean
public
ElasticsearchClient
elasticsearchClient
()
{
try
{
File
certFile
=
new
File
(
"/path/to/http_ca.crt"
);
SSLContext
sslContext
=
TransportUtils
.
sslContextFromHttpCaCrt
(
certFile
);
BasicCredentialsProvider
credsProv
=
new
BasicCredentialsProvider
();
/* credsProv.setCredentials(
AuthScope.ANY, new UsernamePasswordCredentials(login, password)
);*/
HttpHost
[]
httpHosts
=
toHttpHost
();
RestClient
restClient
=
RestClient
.
builder
(
httpHosts
)
.
setHttpClientConfigCallback
(
hc
->
hc
.
setSSLContext
(
sslContext
)
.
setDefaultCredentialsProvider
(
credsProv
)
).
build
();
RestClientTransport
transport
=
new
RestClientTransport
(
restClient
,
new
JacksonJsonpMapper
());
return
new
ElasticsearchClient
(
transport
);
}
catch
(
IOException
e
)
{
throw
new
RuntimeException
(
e
);
}
}
/**
* 异步方式
* @return
*/
@Bean
public
ElasticsearchAsyncClient
elasticsearchAsyncClient
()
{
HttpHost
[]
httpHosts
=
toHttpHost
();
RestClient
restClient
=
RestClient
.
builder
(
httpHosts
).
build
();
RestClientTransport
transport
=
new
RestClientTransport
(
restClient
,
new
JacksonJsonpMapper
());
return
new
ElasticsearchAsyncClient
(
transport
);
}
/**
* 解析配置的字符串hosts,转为HttpHost对象数组
*
* @return
*/
private
HttpHost
[]
toHttpHost
()
{
if
(!
StringUtils
.
hasLength
(
elasticsearchUris
))
{
throw
new
RuntimeException
(
"invalid elasticsearch configuration. elasticsearch.hosts不能为空!"
);
}
// 多个IP逗号隔开
String
[]
hostArray
=
elasticsearchUris
.
split
(
","
);
HttpHost
[]
httpHosts
=
new
HttpHost
[
hostArray
.
length
];
HttpHost
httpHost
;
for
(
int
i
=
0
;
i
<
hostArray
.
length
;
i
++)
{
String
[]
strings
=
hostArray
[
i
].
split
(
":"
);
httpHost
=
new
HttpHost
(
strings
[
0
],
Integer
.
parseInt
(
strings
[
1
]),
"http"
);
httpHosts
[
i
]
=
httpHost
;
}
return
httpHosts
;
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment