enter image description here公共类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社区版2017.3.2 \ lib \ idea_rt.jar = 54053:C:\ Program Files \ JetBrains \ IntelliJ IDEA社区版2017.3.2 \ bin -Dfile.encoding = UTF-8
获取响应代码-1,无效的Http响应。
我该如何解决这个问题?
答案 0 :(得分:0)
似乎您的源代码中缺少GEODE_HOME
环境变量,服务器在启动过程中需要此变量以查找启动REST API所需的库,特别是{{1} }。您可以在Setup and Configuration中找到有关此问题的更多详细信息。
希望这会有所帮助。干杯。
答案 1 :(得分:0)
我已经在本地测试了您的源代码(加上一个/两个小的修改),并且一切似乎都很好:
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
环境变量?执行port
命令时使用的curl
呢?与您通过{{1}配置的数字相同吗? }属性?
干杯。