java - 将简单的服务器代码部署到 Heroku

标签 java sockets heroku

我最近访问了 heroku.com 网站并尝试在那里部署我的第一个 java 程序,实际上我使用他们的 java 部署教程有了一个良好的开端,并且运行正常。现在我有一个服务器代码,我需要在那里部署,我试着按照这个例子,但我有一些问题,比如,

1- 在这种情况下主机是什么,我已经尝试过应用程序链接,就好像它是主机一样,但它会抛出错误,

这是我的示例服务器代码

public class DateServer {

    /** Runs the server. */
    public static void main(String[] args) throws IOException {
        ServerSocket listener = new ServerSocket(6780);
        try {
            while (true) {
                Socket socket = listener.accept();
                try {
                    PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
                    out.println(new Date().toString());
                } finally {
                    socket.close();
                }
            }
        } finally {
            listener.close();
        }
    }
}

这是我的客户端代码

public class DateClient {

    /** Runs the client as an application. First it displays a dialog box asking for the IP address or hostname of a host running the date server, then connects to it and displays the date that it serves. */
    public static void main(String[] args) throws IOException {
        //I used my serverAddress is my external ip address 
        Socket s = new Socket(serverAddress, 6780);
        BufferedReader input = new BufferedReader(new InputStreamReader(s.getInputStream()));
        String answer = input.readLine();
        JOptionPane.showMessageDialog(null, answer);
        System.exit(0);
    }
}

我遵循了本教程 https://devcenter.heroku.com/articles/java在他们的网站上上传我的服务器代码还有什么我需要做的吗?!

提前致谢

最佳答案

在 Heroku 上,您的应用程序必须 bind to the HTTP port provided in the $PORT environment variable .鉴于此,上述应用程序代码中的两个主要问题是 1) 您绑定(bind)到硬编码端口 (6780) 和 2) 您的应用程序使用 TCP 而不是 HTTP。如图tutorial ,使用 Jetty 之类的东西来完成应用程序的 HTTP 等价物,并使用 System.getenv("PORT") 绑定(bind)到正确的端口,如下所示:

import java.util.Date;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.*;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.servlet.*;

public class HelloWorld extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        resp.getWriter().print(new Date().toString());
    }

    public static void main(String[] args) throws Exception{
        Server server = new Server(Integer.valueOf(System.getenv("PORT")));
        ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
        context.setContextPath("/");
        server.setHandler(context);
        context.addServlet(new ServletHolder(new HelloWorld()),"/*");
        server.start();
        server.join();   
    }
}

关于java - 将简单的服务器代码部署到 Heroku,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16574085/

相关文章:

java - 如何通过响应导出PDFBox(pdf文件)?

java - Eclipse 搜索按钮中的 AST

java - 从 Excel 工作表中读取数据

java - 定制/扩展Spring对shiro的@Async支持

perl - 如何在 Perl 中测试 TCP 套接字状态?

delphi - 一次发送多于 2 行的问题

java - 通过单个套接字进行多个 HTTP POST

ios - 解析服务器 + strip 连接 - iOS

python - Heroku 应用程序有 0 Dynos

node.js - 未找到 Heroku 应用程序