访问阿里云es建库或者服务启动总会报None of the configured nodes are available,历史服务采用transportclient,官方不推荐,但是老服务改造成本太大.代码如下
@Bean
public Client client() {
Settings esSettings = Settings.builder()
.put("cluster.name", EsClusterName)
.put("xpack.security.user", esUsername + ":" + esPassword)
.build();
TransportClient tclient = new PreBuiltXPackTransportClient(esSettings);
try {
String[] nodes = EsHosts.split(",");
for (String node : nodes) {
node = node.trim();
if (node.length() > 0) {//跳过为空的node(当开头、结尾有逗号或多个连续逗号时会出现空node)
String[] hostPort = node.split(":");
int port = 9300;
if (hostPort.length == 2) {
port = Integer.parseInt(hostPort[1]);
}
tclient.addTransportAddress(new InetSocketTransportAddress(
InetAddress.getByName(hostPort[0]), port));
}
}
}catch (UnknownHostException e) {
log.error(e.getMessage(),e);
}
return tclient;
}
@Bean
public BulkProcessor getBulkProcessor() {
BulkProcessor bulkProcessor = null;
bulkProcessor = BulkProcessor.builder(client(),
new BulkProcessor.Listener() {
@Override
public void beforeBulk(long executionId, BulkRequest request) {
}
@Override
public void afterBulk(long executionId, BulkRequest request, BulkResponse response) {
log.info( "提交" + response.getItems().length + "个文档,用时"
+ response.getTookInMillis() + "MS" + (response.hasFailures() ? " 有文档提交失败!" : ""));
}
@Override
public void afterBulk(long executionId, BulkRequest request, Throwable failure) {
//提交结束且失败时调用
log.error( " 有文档提交失败!after failure=" + failure.getCause() == null ? failure.getMessage() : failure.getCause().toString());
}
})
.setBulkActions(bulkActions)
.setBulkSize(new ByteSizeValue(10, ByteSizeUnit.MB))
.setFlushInterval(TimeValue.timeValueSeconds(30))
.setConcurrentRequests(1)
.build();
return bulkProcessor;
}
题主好,建议采用官方推荐方法,https://www.aliyun.com/jiaocheng/topic_66838_1.html.
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。