mirror of
https://github.com/alibaba/canal.git
synced 2026-11-04 00:30:15 +08:00
修复当host中存在冒号(比如https:)抛UnknownHostException问题 (#3223)
* 修复当host中存在冒号(比如https:)抛UnknownHostException问题 * 更优雅地解析url
This commit is contained in:
@@ -2,7 +2,10 @@ package com.alibaba.otter.canal.client.adapter.es6x.support;
|
|||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.net.InetAddress;
|
import java.net.InetAddress;
|
||||||
|
import java.net.URI;
|
||||||
|
import java.net.URISyntaxException;
|
||||||
import java.net.UnknownHostException;
|
import java.net.UnknownHostException;
|
||||||
|
import java.util.Arrays;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
import org.apache.commons.lang.StringUtils;
|
import org.apache.commons.lang.StringUtils;
|
||||||
@@ -78,14 +81,7 @@ public class ESConnection {
|
|||||||
Integer.parseInt(host.substring(i + 1))));
|
Integer.parseInt(host.substring(i + 1))));
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
HttpHost[] httpHosts = new HttpHost[hosts.length];
|
HttpHost[] httpHosts = Arrays.stream(hosts).map(this::createHttpHost).toArray(HttpHost[]::new);
|
||||||
for (int i = 0; i < hosts.length; i++) {
|
|
||||||
String host = hosts[i];
|
|
||||||
int j = host.indexOf(":");
|
|
||||||
HttpHost httpHost = new HttpHost(InetAddress.getByName(host.substring(0, j)),
|
|
||||||
Integer.parseInt(host.substring(j + 1)));
|
|
||||||
httpHosts[i] = httpHost;
|
|
||||||
}
|
|
||||||
RestClientBuilder restClientBuilder = RestClient.builder(httpHosts);
|
RestClientBuilder restClientBuilder = RestClient.builder(httpHosts);
|
||||||
String nameAndPwd = properties.get("security.auth");
|
String nameAndPwd = properties.get("security.auth");
|
||||||
if (StringUtils.isNotEmpty(nameAndPwd) && nameAndPwd.contains(":")) {
|
if (StringUtils.isNotEmpty(nameAndPwd) && nameAndPwd.contains(":")) {
|
||||||
@@ -507,4 +503,17 @@ public class ESConnection {
|
|||||||
public void setRestHighLevelClient(RestHighLevelClient restHighLevelClient) {
|
public void setRestHighLevelClient(RestHighLevelClient restHighLevelClient) {
|
||||||
this.restHighLevelClient = restHighLevelClient;
|
this.restHighLevelClient = restHighLevelClient;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private HttpHost createHttpHost(String uriStr) {
|
||||||
|
URI uri = URI.create(uriStr);
|
||||||
|
if (!org.springframework.util.StringUtils.hasLength(uri.getUserInfo())) {
|
||||||
|
return HttpHost.create(uri.toString());
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
return HttpHost.create(new URI(uri.getScheme(), null, uri.getHost(), uri.getPort(), uri.getPath(),
|
||||||
|
uri.getQuery(), uri.getFragment()).toString());
|
||||||
|
} catch (URISyntaxException ex) {
|
||||||
|
throw new IllegalStateException(ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,7 +2,10 @@ package com.alibaba.otter.canal.client.adapter.es7x.support;
|
|||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.net.InetAddress;
|
import java.net.InetAddress;
|
||||||
|
import java.net.URI;
|
||||||
|
import java.net.URISyntaxException;
|
||||||
import java.net.UnknownHostException;
|
import java.net.UnknownHostException;
|
||||||
|
import java.util.Arrays;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
import org.apache.commons.lang.StringUtils;
|
import org.apache.commons.lang.StringUtils;
|
||||||
@@ -77,14 +80,7 @@ public class ESConnection {
|
|||||||
Integer.parseInt(host.substring(i + 1))));
|
Integer.parseInt(host.substring(i + 1))));
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
HttpHost[] httpHosts = new HttpHost[hosts.length];
|
HttpHost[] httpHosts = Arrays.stream(hosts).map(this::createHttpHost).toArray(HttpHost[]::new);
|
||||||
for (int i = 0; i < hosts.length; i++) {
|
|
||||||
String host = hosts[i];
|
|
||||||
int j = host.indexOf(":");
|
|
||||||
HttpHost httpHost = new HttpHost(InetAddress.getByName(host.substring(0, j)),
|
|
||||||
Integer.parseInt(host.substring(j + 1)));
|
|
||||||
httpHosts[i] = httpHost;
|
|
||||||
}
|
|
||||||
RestClientBuilder restClientBuilder = RestClient.builder(httpHosts);
|
RestClientBuilder restClientBuilder = RestClient.builder(httpHosts);
|
||||||
String nameAndPwd = properties.get("security.auth");
|
String nameAndPwd = properties.get("security.auth");
|
||||||
if (StringUtils.isNotEmpty(nameAndPwd) && nameAndPwd.contains(":")) {
|
if (StringUtils.isNotEmpty(nameAndPwd) && nameAndPwd.contains(":")) {
|
||||||
@@ -501,4 +497,17 @@ public class ESConnection {
|
|||||||
public void setRestHighLevelClient(RestHighLevelClient restHighLevelClient) {
|
public void setRestHighLevelClient(RestHighLevelClient restHighLevelClient) {
|
||||||
this.restHighLevelClient = restHighLevelClient;
|
this.restHighLevelClient = restHighLevelClient;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private HttpHost createHttpHost(String uriStr) {
|
||||||
|
URI uri = URI.create(uriStr);
|
||||||
|
if (!org.springframework.util.StringUtils.hasLength(uri.getUserInfo())) {
|
||||||
|
return HttpHost.create(uri.toString());
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
return HttpHost.create(new URI(uri.getScheme(), null, uri.getHost(), uri.getPort(), uri.getPath(),
|
||||||
|
uri.getQuery(), uri.getFragment()).toString());
|
||||||
|
} catch (URISyntaxException ex) {
|
||||||
|
throw new IllegalStateException(ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user