Heroku上的Netty端口

时间:2016-07-05 07:43:32

标签: java heroku netty

我在Heroku上使用Netty SecureChatServer示例。 我使用以下Procfile内容启动服务器:

web:    java -cp target/classes:target/dependency/* SecureChatServer -DPORT=$PORT

按照Heroku文档中的规定,每次启动都会随机分配$ PORT。

部署服务器之后似乎开始正常记录以下内容:

2016-07-05T07:16:04.815889+00:00 heroku[api]: Deploy 1d48bff by [...........]
2016-07-05T07:16:04.815960+00:00 heroku[api]: Release v27 created by [...........]
2016-07-05T07:16:05.032420+00:00 heroku[web.1]: Restarting
2016-07-05T07:16:05.033110+00:00 heroku[web.1]: State changed from up to starting
2016-07-05T07:16:07.437424+00:00 heroku[web.1]: Starting process with command `java -cp target/classes:target/dependency/* SecureChatServer -DPORT=29143`
2016-07-05T07:16:07.859601+00:00 heroku[web.1]: Stopping all processes with SIGTERM
2016-07-05T07:16:09.011911+00:00 app[web.1]: Setting JAVA_TOOL_OPTIONS defaults based on dyno size. Custom settings will override them.
2016-07-05T07:16:09.015289+00:00 app[web.1]: Picked up JAVA_TOOL_OPTIONS: -Xmx350m -Xss512k -Dfile.encoding=UTF-8
2016-07-05T07:16:09.090952+00:00 app[web.1]: Port is: 29143
2016-07-05T07:16:09.810678+00:00 heroku[web.1]: Process exited with status 143
2016-07-05T07:16:09.894360+00:00 app[web.1]: Jul 05, 2016 7:16:09 AM io.netty.handler.logging.LoggingHandler bind
2016-07-05T07:16:09.898395+00:00 app[web.1]: Jul 05, 2016 7:16:09 AM io.netty.handler.logging.LoggingHandler channelActive
2016-07-05T07:16:09.892003+00:00 app[web.1]: INFO: [id: 0x4b5aac8d] REGISTERED
2016-07-05T07:16:09.898405+00:00 app[web.1]: INFO: [id: 0x4b5aac8d, L:/0:0:0:0:0:0:0:0:29143] ACTIVE
2016-07-05T07:16:09.894374+00:00 app[web.1]: INFO: [id: 0x4b5aac8d] BIND: 0.0.0.0/0.0.0.0:29143
2016-07-05T07:16:09.891990+00:00 app[web.1]: Jul 05, 2016 7:16:09 AM io.netty.handler.logging.LoggingHandler channelRegistered
2016-07-05T07:16:10.189332+00:00 app[web.1]: Jul 05, 2016 7:16:10 AM io.netty.handler.logging.LoggingHandler channelRead
2016-07-05T07:16:10.189343+00:00 app[web.1]: INFO: [id: 0x4b5aac8d, L:/0:0:0:0:0:0:0:0:29143] RECEIVED: [id: 0x992ddc07, L:/172.17.81.74:29143 - R:/172.17.81.73:31347]
2016-07-05T07:16:10.190954+00:00 app[web.1]: Jul 05, 2016 7:16:10 AM io.netty.handler.logging.LoggingHandler channelRead
2016-07-05T07:16:10.190970+00:00 app[web.1]: INFO: [id: 0x4b5aac8d, L:/0:0:0:0:0:0:0:0:29143] RECEIVED: [id: 0x1623ff10, L:/172.17.81.74:29143 - R:/172.17.81.73:31352]
2016-07-05T07:16:10.202144+00:00 heroku[web.1]: State changed from starting to up
2016-07-05T07:16:16.107796+00:00 app[web.1]: Jul 05, 2016 7:16:16 AM io.netty.handler.logging.LoggingHandler channelRead
2016-07-05T07:16:16.107806+00:00 app[web.1]: INFO: [id: 0x4b5aac8d, L:/0:0:0:0:0:0:0:0:29143] RECEIVED: [id: 0x668619db, L:/172.17.81.74:29143 - R:/172.17.81.73:32792]

从客户端我尝试使用以下内容连接到服务器:

public final class SecureChatClient {

    static final String HOST = System.getProperty("host", "xxxxxxxxxxxx.herokuapp.com");
    static final int PORT = Integer.parseInt(System.getProperty("port", "29143"));

    public static void main(String[] args) throws Exception {
        // Configure SSL.
        final SslContext sslCtx = SslContextBuilder.forClient()
            .trustManager(InsecureTrustManagerFactory.INSTANCE).build();

        EventLoopGroup group = new NioEventLoopGroup();
        try {
            Bootstrap b = new Bootstrap();
            b.group(group)
             .channel(NioSocketChannel.class)
             .handler(new SecureChatClientInitializer(sslCtx));

            // Start the connection attempt.
            Channel ch = b.connect(HOST, PORT).sync().channel();

            // Read commands from the stdin.
            ChannelFuture lastWriteFuture = null;
            BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
            for (;;) {
                String line = in.readLine();
                if (line == null) {
                    break;
                }

                // Sends the received line to the server.
                lastWriteFuture = ch.writeAndFlush(line + "\r\n");

                // If user typed the 'bye' command, wait until the server closes
                // the connection.
                if ("bye".equals(line.toLowerCase())) {
                    ch.closeFuture().sync();
                    break;
                }
            }

            // Wait until all messages are flushed before closing the channel.
            if (lastWriteFuture != null) {
                lastWriteFuture.sync();
            }
        } finally {
            // The connection is closed automatically on shutdown.
            group.shutdownGracefully();
        }
    }
}

我尝试过端口80,8080,443,29143等。 在443端口我以某种方式连接,但我没有得到预期的欢迎问候。 如果我向端口443发送消息,我会得到以下响应:

HTTP/1.1 400 Bad Request
Connection: close
Server: Cowboy
Date: Tue, 05 Jul 2016 07:37:17 GMT
Content-Length: 0

在29143号港口,我得到:

Exception in thread "main" java.net.ConnectException: Connection refused: no further information: xxxxxxxxxxxx.herokuapp.com/xxx.xx.xxx.x:29143
    at sun.nio.ch.SocketChannelImpl.checkConnect(Native Method)
    at sun.nio.ch.SocketChannelImpl.finishConnect(Unknown Source)
    at io.netty.channel.socket.nio.NioSocketChannel.doFinishConnect(NioSocketChannel.java:330)
    at io.netty.channel.nio.AbstractNioChannel$AbstractNioUnsafe.finishConnect(AbstractNioChannel.java:338)
    at io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:580)
    at io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:504)
    at io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:418)
    at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:390)
    at io.netty.util.concurrent.SingleThreadEventExecutor$5.run(SingleThreadEventExecutor.java:742)
    at io.netty.util.concurrent.DefaultThreadFactory$DefaultRunnableDecorator.run(DefaultThreadFactory.java:145)
    at java.lang.Thread.run(Unknown Source)

1 个答案:

答案 0 :(得分:0)

客户端不会使用$PORT内部值。 Heroku路由器处理从http(s)://<yourapp>.herokuapp.com到该端口的路由流量。所以80/443是正确的。

400响应将来自您的应用程序,这意味着它实际上正在运行,并接收请求。但400表示格式错误的请求。

相关问题