将简单的服务器代码部署到Heroku [英] Deploy simple server code to Heroku

查看:112
本文介绍了将简单的服务器代码部署到Heroku的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

1-主机是什么在这种情况下,我已经尝试了应用程序链接,就好像它的主机一样,但它会抛出错误,这里是我的示例服务器代码。

  public class DateServer {

/ **运行服务器。 * /
public static void main(String [] args)throws IOException {
ServerSocket listener = new ServerSocket(6780);
尝试{
while(true){
Socket socket = listener.accept();
尝试{
PrintWriter out = new PrintWriter(socket.getOutputStream(),true);
out.println(new Date()。toString());
} finally {
socket.close();
}
}
} finally {
listener.close();





$ p这里是我的客户代码

  public class DateClient {

/ **将客户端作为应用程序运行。首先显示一个对话框,询问运行日期服务器的主机的IP地址或主机名,然后连接到它并显示它所服务的日期。 * /
public static void main(String [] args)throws IOException {
//我用我的serverAddress是我的外部IP地址
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上, a href =https://devcenter.heroku.com/articles/dynos#web-dynos =noreferrer>绑定到$ PORT环境变量中提供的HTTP端口。鉴于此,上述应用程序代码中的两个主要问题是:1)您绑定到硬编码端口( 6780 ); 2)您的应用程序使用TCP而不是HTTP。如教程所示,使用类似Jetty的东西来完成与您的应用程序相同的HTTP,并使用 System.getenv(PORT)绑定到正确的端口,如下所示:

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

public class HelloWorld extends HttpServlet {

@Override
protected void doGet(HttpServletRequest req,HttpServletResponse resp)
抛出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();
}
}


I recently visited heroku.com site and tried to deploy my first java program there , I actually had a good start using their java deployment tutorial, and had it run ok. now I have a server code which I need to deploy there , I tried to follow the example but I had some question in mind like,

1- what will be the host in this case , I already tried the app link as if its the host but it throws errors ,

here is my sample server code

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();
        }
    }
}

here is my client code

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);
    }
}

I followed this tutorial https://devcenter.heroku.com/articles/java at their site to upload my server code is there something else I need to do ?!

thanks in advance

解决方案

On Heroku, your application must bind to the HTTP port provided in the $PORT environment variable. Given this, the two major problems in your application code above are 1) you are binding to a hardcoded port (6780) and 2) your application is using TCP instead of HTTP. As shown in the tutorial, use something like Jetty to accomplish the HTTP equivalent of your application and use System.getenv("PORT") to bind to the right port, like this:

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();   
    }
}

这篇关于将简单的服务器代码部署到Heroku的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆