我正在编写一个服务发现应用程序,它将在zookeeper节点上注册Web应用程序。其中一个属性是服务可用的端口号。在代码中我得到了jetty服务器实例的句柄,但是如何从Jetty Server实例中找到端口号呢?
答案 0 :(得分:0)
走连接器,询问他们注册的主机名(以便了解他们正在侦听的网络接口)和本地端口(他们绑定的端口)。
这是一个你可以开始的模板。
for (Connector connector : server.getConnectors())
{
if (connector instanceof NetworkConnector)
{
// we have a network capable connector
NetworkConnector networkConnector = (NetworkConnector) connector;
// What interface?
String interfaceName = networkConnector.getHost();
// What local port is it bound to?
int localPort = networkConnector.getLocalPort();
// What is the declared protocol default for this connector?
String defaultProtocol = networkConnector.getDefaultConnectionFactory().getProtocol();
// What other features does this connector handle?
for (ConnectionFactory connectionFactory : networkConnector.getConnectionFactories())
{
// List of protocols handled by this specific connection factory for this specific connector
connectionFactory.getProtocols();
if (connectionFactory instanceof SslConnectionFactory)
{
// this can handle TLS/SSL based connections
}
if (connectionFactory instanceof HttpConnectionFactory)
{
// this can handle http protocols
// get the http specific configuration
HttpConfiguration httpConfig = ((HttpConnectionFactory) connectionFactory).getHttpConfiguration();
// what port is recognized as secure
httpConfig.getSecurePort();
// what scheme is recognized as secure
httpConfig.getSecureScheme();
}
if (connectionFactory instanceof HTTP2ServerConnectionFactory)
{
// can handle encrypted http/2 protocols (and alpn features)
}
if (connectionFactory instanceof HTTP2CServerConnectionFactory)
{
// this can handle http/2's special clear-text "h2c" protocol (no alpn features)
}
}
}
}