我正在使用ElasticSearch版本6.6.0(在远程VM上运行的单节点群集)。我想使用tcp端口9300将Java Transport Client连接到远程ES。我成功打开了可从我的机器访问的tcp端口(使用telnet测试到9200和9300端口)。也可以在http://:9200 /上获得“弹性详细信息”。
我不知所措,无法解决为何传输客户端无法连接到群集以进行索引/查询的迷思。
elasticsearch.yml看起来像这样:
public static class ServiceCollectionExtension
{
private static string XmlCommentsFilePath
{
get
{
var basePath = PlatformServices.Default.Application.ApplicationBasePath;
var fileName = Assembly.GetEntryAssembly().GetName().Name + ".xml";
return Path.Combine(basePath, fileName);
}
}
public static void AddMySwagger(
this IServiceCollection services,
ApiVersion defaultApiVersion,
Func<ApiVersionDescription, Info> info,
string authority = null,
Dictionary<string, string> scopes = null)
{
services.AddMvcCore().AddVersionedApiExplorer(o => o.GroupNameFormat = "'v'VVV");
services.AddApiVersioning(o =>
{
o.ReportApiVersions = true;
o.AssumeDefaultVersionWhenUnspecified = true;
o.DefaultApiVersion = defaultApiVersion;
});
services.AddSwaggerGen(
options =>
{
var provider = services.BuildServiceProvider()
.GetRequiredService<IApiVersionDescriptionProvider>();
foreach (var description in provider.ApiVersionDescriptions)
{
if (!description.IsDeprecated)
options.SwaggerDoc(description.GroupName, info(description));
}
options.OperationFilter<DefaultValues>();
options.IncludeXmlComments(XmlCommentsFilePath);
if (!string.IsNullOrEmpty(authority))
{
options.AddSecurityDefinition("jwt", new ApiKeyScheme()
{
Description = "JWT Authorization header using the Bearer scheme. Example: \"Bearer {token}\"",
Name = "Authorization",
In = "header",
Type = "apiKey"
});
//options.AddSecurityDefinition("oauth2", new OAuth2Scheme
//{
// Flow = "implicit",
// AuthorizationUrl = $"{authority}/connect/authorize",
// Scopes = scopes ?? new Dictionary<string, string>()
//});
options.OperationFilter<AuthorizeCheckOperationFilter>(scopes?.Select(_ => _.Key).ToList() ?? new List<string>());
}
});
}
class AuthorizeCheckOperationFilter : IOperationFilter
{
private readonly IEnumerable<string> _scopes;
public AuthorizeCheckOperationFilter(IEnumerable<string> scopes)
{
_scopes = scopes;
}
public void Apply(Operation operation, OperationFilterContext context)
{
var hasAuthorize = context.ApiDescription.ControllerAttributes().OfType<AuthorizeAttribute>().Any() ||
context.ApiDescription.ActionAttributes().OfType<AuthorizeAttribute>().Any();
if (hasAuthorize)
{
operation.Responses.Add("401", new Response { Description = "Unauthorized" });
operation.Responses.Add("403", new Response { Description = "Forbidden" });
operation.Security = new List<IDictionary<string, IEnumerable<string>>> {
new Dictionary<string, IEnumerable<string>>
{
//{"oauth2", _scopes},
{"jwt", _scopes }
}
};
}
}
}
}
services.AddMySwagger(
new ApiVersion(1, 0),
__description => new Info { Title = $"API v{__description.ApiVersion}", Version = __description.ApiVersion.ToString() },
Configuration.GetValue<string>("Authentication:Authority"),
new Dictionary<string, string> { { Configuration.GetValue<string>("Authentication:Scope"), "Partnership API" } }
);
从本地计算机到9300和9200的telnet也可以使用。
pom.xml指向正确的ES版本:
cluster.name: test-elastic-surabhi
node.name: ${HOSTNAME}
network.host: ${HOSTNAME}
action.auto_create_index: .monitoring*,.watches,.triggered_watches,.watcher-history*,.ml*
以下是从传输客户端连接到TCP的代码:
{
"name" : "test-elastic-surabhi-1",
"cluster_name" : "test-elastic-surabhi",
"cluster_uuid" : "c-w5p531Szqsv_up3OS_Uw",
"version" : {
"number" : "6.6.0",
"build_flavor" : "default",
"build_type" : "tar",
"build_hash" : "a9861f4",
"build_date" : "2019-01-24T11:27:09.439740Z",
"build_snapshot" : false,
"lucene_version" : "7.6.0",
"minimum_wire_compatibility_version" : "5.6.0",
"minimum_index_compatibility_version" : "5.0.0"
},
"tagline" : "You Know, for Search"
}
但是从Java代码中使用的传输客户端中,我可以看到异常
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>6.6.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.elasticsearch.client/transport -->
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>transport</artifactId>
<version>6.6.0</version>
</dependency>
Settings settings = Settings.builder().put("cluster.name", DataStoreSettings.ES_CLUSTER_NAME)
.put("client.transport.sniff", true).build();
esTransportClient = new PreBuiltTransportClient(settings);
for (String eachHostPort : DataStoreSettings.ES_HOST) {
LOGGER.info("adding [{}] to TransportClient ... ", eachHostPort);
String[] hostPortTokens = eachHostPort.split(":");
if (hostPortTokens.length < 2)
throw new Exception(
"ERROR: bad ElasticSearch host:port configuration - wrong format: " + eachHostPort);
int port = 9300; // default ES port
try {
port = Integer.parseInt(hostPortTokens[1].trim());
} catch (Exception e) {
LOGGER.error("ERROR parsing port from the ES config [{}]- using default port 9300",
eachHostPort);
}
esTransportClient.addTransportAddress(
new TransportAddress(InetAddress.getByName(hostPortTokens[0].trim()), port));
}
2019-02-14 22:31:23,599 TRACE [TransportLogger]: an exception occurred formatting a WRITE trace message
java.io.EOFException: tried to read: 105 bytes but only 27 remaining
at org.elasticsearch.common.bytes.BytesReferenceStreamInput.ensureCanReadBytes(BytesReferenceStreamInput.java:121) ~[elasticsearch-6.6.0.jar:6.6.0]
at org.elasticsearch.common.bytes.BytesReference$MarkSupportingStreamInputWrapper.ensureCanReadBytes(BytesReference.java:283) ~[elasticsearch-6.6.0.jar:6.6.0]
at org.elasticsearch.common.io.stream.StreamInput.readArraySize(StreamInput.java:1026) ~[elasticsearch-6.6.0.jar:6.6.0]
at org.elasticsearch.common.io.stream.StreamInput.readString(StreamInput.java:374) ~[elasticsearch-6.6.0.jar:6.6.0]
at org.elasticsearch.common.io.stream.StreamInput.readStringArray(StreamInput.java:469) ~[elasticsearch-6.6.0.jar:6.6.0]
at org.elasticsearch.transport.TransportLogger.format(TransportLogger.java:101) ~[elasticsearch-6.6.0.jar:6.6.0]
at org.elasticsearch.transport.TransportLogger.logOutboundMessage(TransportLogger.java:55) ~[elasticsearch-6.6.0.jar:6.6.0]
at org.elasticsearch.transport.TcpTransport.internalSendMessage(TcpTransport.java:758) ~[elasticsearch-6.6.0.jar:6.6.0]
at org.elasticsearch.transport.TcpTransport.sendRequestToChannel(TcpTransport.java:744) ~[elasticsearch-6.6.0.jar:6.6.0]
at org.elasticsearch.transport.TcpTransport.lambda$new$0(TcpTransport.java:174) ~[elasticsearch-6.6.0.jar:6.6.0]
at org.elasticsearch.transport.TransportHandshaker.sendHandshake(TransportHandshaker.java:74) ~[elasticsearch-6.6.0.jar:6.6.0]
at org.elasticsearch.transport.TcpTransport.executeHandshake(TcpTransport.java:1312) ~[elasticsearch-6.6.0.jar:6.6.0]
at org.elasticsearch.transport.TcpTransport$ChannelsConnectedListener.onResponse(TcpTransport.java:1533) ~[elasticsearch-6.6.0.jar:6.6.0]
at org.elasticsearch.transport.TcpTransport$ChannelsConnectedListener.onResponse(TcpTransport.java:1510) ~[elasticsearch-6.6.0.jar:6.6.0]
Caused by: io.netty.channel.ConnectTimeoutException: connection timed out: <private_ip>/<private_ip>:9300
远程弹性单节点群集的日志也将打印:
2019-02-14 22:31:54,590 DEBUG [TransportClientNodesService]: failed to connect to discovered node [{test-elastic-surabhi-1}{F_we0jg4Q7uoQ3uOaDvv-w}{kfqF3aRcQ7mWAxhXv9R_uQ}{<private_ip>}{<private_ip>:9300}{ml.machine_memory=7673036800, ml.max_open_jobs=20, xpack.installed=true, ml.enabled=true}]
org.elasticsearch.transport.ConnectTransportException: [test-elastic-surabhi-1][<private_ip>:9300] connect_timeout[30s]
at org.elasticsearch.transport.TcpTransport$ChannelsConnectedListener.onTimeout(TcpTransport.java:1576) ~[elasticsearch-6.6.0.jar:6.6.0]
at org.elasticsearch.common.util.concurrent.ThreadContext$ContextPreservingRunnable.run(ThreadContext.java:660) ~[elasticsearch-6.6.0.jar:6.6.0]
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) ~[?:1.8.0_191]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) ~[?:1.8.0_191]
at java.lang.Thread.run(Thread.java:748) ~[?:1.8.0_191]