【问题标题】:Invalid Http Response for Gemfire?Gemfire 的 Http 响应无效?
【发布时间】:2019-03-03 18:38:28
【问题描述】:

enter image description here public class GemfireTest 扩展 SecurityManager {

            public static void main(String[] args) throws NameResolutionException, TypeMismatchException, QueryInvocationTargetException, FunctionDomainException, IOException {

                System.setProperty("gemfire.locators", "localhost[8091]");
                Properties properties = new Properties();

             ServerLauncher serverLauncher = new ServerLauncher.Builder()

                        .setMemberName("server1")
                        .setServerPort(40404)
                        .set("start-locator", "localhost[8091]")
            .build();
                serverLauncher.start();
                System.out.println(serverLauncher.status());
String restapi ="http://localhost:8091/gemfire-api/v1/";
             //   URLConnection urlConnection = new URL(restapi).openConnection();
                try {
                    URL obj = new URL(restapi);
                    HttpURLConnection con = (HttpURLConnection) obj.openConnection();
                    con.setRequestMethod("GET");
                    con.setRequestProperty("accept","application/json");
                    int responseCode = con.getResponseCode();
                    System.out.println(responseCode);
                   // if (responseCode == HttpURLConnection.HTTP_OK) { // success
                        BufferedReader in = new BufferedReader(new InputStreamReader(
                                con.getInputStream()));
                        String inputLine;
                        StringBuffer response = new StringBuffer();

                        while ((inputLine = in.readLine()) != null) {
                            response.append(inputLine);
                        }
                        in.close();

                        // print result
                        System.out.println("reason"+response.toString());

DESKTOP-MRCV2EH[40404] 上的 C:\Users\xxx\IdeaProjects\Gemfire 中的服务器,因为 server1 当前在线。 进程 ID:2640 正常运行时间:7 秒 Geode版本:9.5.1 Java版本:1.8.0_171 日志文件:C:\Users\xxx\IdeaProjects\Gemfire\server1.log JVM 参数:-Dgemfire.enable-cluster-configuration=true -Dgemfire.load-cluster-configuration-from-dir=false -Dgemfire.jmx-manager-bind-address=localhost -Djava.rmi.server.hostname=localhost - Dgemfire.launcher.registerSignalHandlers=true -Djava.awt.headless=true -javaagent:C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2017.3.2\lib\idea_rt.jar=54053:C:\Program Files\JetBrains\ IntelliJ IDEA 社区版 2017.3.2\bin -Dfile.encoding=UTF-8

获取响应代码:-1,无效的 Http 响应。

我该如何解决这个问题?

【问题讨论】:

    标签: java http gemfire


    【解决方案1】:

    我已经在本地测试了您的源代码(加上一/两个小修改),一切似乎都运行良好:

    public static void main(String[] args) throws IOException {
        Integer httpServicePort = 8080;
        ServerLauncher serverLauncher = 
            new ServerLauncher.Builder()
            .set("log-file", "")
            .set("http-service-port", httpServicePort.toString())   
            .set("start-dev-rest-api", "true")
            .set("http-service-bind-address", "localhost")
            .setPdxReadSerialized(true)
            .build();
    
        serverLauncher.start();
        System.out.println("REST server successfully started, press any key to stop it...");
        String restapi ="http://localhost:" + httpServicePort + "/gemfire-api/v1/";
    
        try {
            URL obj = new URL(restapi);
            HttpURLConnection con = (HttpURLConnection) obj.openConnection();
            con.setRequestMethod("GET");
            con.setRequestProperty("accept","application/json");
            int responseCode = con.getResponseCode();
            System.out.println("Response Code: " + responseCode);
    
             if (responseCode == HttpURLConnection.HTTP_OK) { // success
                   BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
                   String inputLine;
                   StringBuffer response = new StringBuffer();
                   while ((inputLine = in.readLine()) != null) {
                       response.append(inputLine);
                   }
    
                   in.close();
                   System.out.println("Response: " + response.toString());
             }
        } catch (Exception exception) {
            exception.printStackTrace();
        }
    
        System.in.read();
        serverLauncher.stop();
        System.exit(0);
    }
    

    运行上述程序会打印以下内容:

    REST server successfully started, press any key to stop it...
    Response Code: 200
    Response: {  "regions" : [ ]}
    

    您是否正确设置了GEODE_HOME 环境变量?执行curl 命令时使用的port 与您通过http-service-port 属性配置的数字是否相同? 干杯。

    【讨论】:

    【解决方案2】:

    您的源代码中似乎缺少GEODE_HOME 环境变量,服务器在启动期间需要它来查找启动REST API 所需的库,特别是install-dir/tools/Extensions/geode-web-api-n.n.n.war。你可以在Setup and Configuration找到更多细节。

    希望这会有所帮助。干杯。

    【讨论】:

    • Http 服务如你所说成功启动,在线程“main”java.io.IOException 中仍然出现异常:无效的Http 响应
    • 嘿Sarv,这看起来不像是GemFire 错误...您是否尝试过产品随附的示例?您可以在Sample REST Applications 找到它们。干杯。
    • 我无法理解为什么服务器返回 -1 作为响应代码。 @胡安·拉莫斯
    • 您是否按照Setup and Configuration 中突出显示的步骤进行操作,curl -i http://localhost:selectedPort/geode/v1 的输出是什么?。
    • 连接端口失败,连接被拒绝@Juan Ramos
    猜你喜欢
    • 1970-01-01
    • 2021-09-12
    • 1970-01-01
    • 2015-06-09
    • 1970-01-01
    • 2019-05-18
    • 1970-01-01
    • 2016-05-04
    • 1970-01-01
    相关资源
    最近更新 更多