如何使用HttpServer类将端口8000暴露给远程访问?

huangapple go评论104阅读模式
英文:

How to expose port 8000 to remote access using HttpServer class?

问题

以下是翻译好的内容:

我创建了这个小型 API 来在一个遗留系统上提供一些 JSON 数据。我不能向其中添加 Spring 或任何库,所以我认为这可能是一个简单的方法。尽管在本地使用 curl localhost:8000/jms/health 可以正常工作,但是当我尝试远程访问时,连接被拒绝了。运行 netstat 命令返回了以下结果:

[userName@machineIp ~]$ sudo netstat -nlpt | grep 8000
tcp        0      0 127.0.0.1:8000              0.0.0.0:*                   LISTEN      25638/java

这是相关的类代码:

package somePackage;

import com.sun.net.httpserver.HttpServer;
import org.slf4j.Logger;
import java.io.IOException;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.Executors;

public class HealthHttp {

    public static void init(Logger log) throws IOException {
        HttpServer server = HttpServer.create(new InetSocketAddress("localhost", 8000), 0);
        server.createContext("/jms/health", http -> {

            String body = "{\"hello\":\"world\"}"; // 任何有效的 JSON 数据

            byte[] bytes = body.getBytes(StandardCharsets.UTF_8);

            http.getResponseHeaders().set("Content-Type", "application/json; charset=" + StandardCharsets.UTF_8);

            http.sendResponseHeaders(200, bytes.length);
            OutputStream output = http.getResponseBody();
            output.write(bytes);
            output.flush();
            output.close();
        });
        server.setExecutor(Executors.newFixedThreadPool(1));
        server.start();
        log.info("Http health server initialized");
    }
}

您如何修复这个问题呢?

英文:

I made this tiny api to provide some json on a legacy system. I cannot add Spring or any library to it and thought this would be a simple way. Although it works locally using curl localhost:8000/jms/health, when I try it remotely the connection is refused. Running netstat returned me this:

[userName@machineIp ~]$ sudo netstat -nlpt | grep 8000
tcp        0      0 127.0.0.1:8000              0.0.0.0:*                   LISTEN      25638/java

And this is the class:

package somePackage;

import com.sun.net.httpserver.HttpServer;

import org.slf4j.Logger;

import java.io.IOException;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.Executors;

public class HealthHttp {

    public static void init(Logger log) throws IOException {
        HttpServer server = HttpServer.create(new InetSocketAddress("localhost", 8000), 0);
        server.createContext("/jms/health", http -> {

            String body = "{\"hello\":\"world\"}"; // any valid json

            byte[] bytes = body.getBytes(StandardCharsets.UTF_8);

            http.getResponseHeaders().set("Content-Type", "application/json; charset=" + StandardCharsets.UTF_8);

            http.sendResponseHeaders(200, bytes.length);
            OutputStream output = http.getResponseBody();
            output.write(bytes);
            output.flush();
            output.close();
        });
        server.setExecutor(Executors.newFixedThreadPool(1));
        server.start();
        log.info("Http health server initialized");
    }
}

How can I fix this?

答案1

得分: 0

根据问题的评论建议,将localhost更改为0.0.0.0允许来自所有外部地址和回环地址的连接。

英文:

As suggested at the question's comments, changing localhost to 0.0.0.0 allowed connections from all external addresses and the loopback

huangapple
  • 本文由 发表于 2020年5月5日 05:06:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/61601603.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定