Parcourir la source

增加SDK的修改

Airhead il y a 8 ans
Parent
commit
050281c8cf

+ 42 - 11
sdk/java/hos-client/src/main/java/com/yihu/hos/client/BrokerServerClient.java

@ -1,18 +1,16 @@
package com.yihu.hos.client;
import org.apache.http.Consts;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
/**
 * @created Airhead 2016/8/1.
@ -24,28 +22,61 @@ public class BrokerServerClient {
        this.host = host;
    }
    public Object invokeSync() {
    public Response invokeSync(Request request) {
        try {
            String brokerServer = getBrokerServer();
            CloseableHttpClient httpclient = HttpClients.createDefault();
            HttpPost httpPost = new HttpPost(brokerServer + "/gateway");
            CloseableHttpResponse response = null;
            try {
                response = httpclient.execute(httpPost);
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                assert response != null;
                response.close();
                httpclient.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
    public Object invokeAsync() {
        return null;
    }
    private String getBrokerServer() throws IOException {
    private String getBrokerServer() throws Exception {
        CloseableHttpClient httpclient = HttpClients.createDefault();
        HttpGet httpGet = new HttpGet(host + ":10135/brokerServer");
        CloseableHttpResponse response = null;
        try {
            response = httpclient.execute(httpGet);
            if (response.getStatusLine().getStatusCode() != 200) {
                throw new Exception("Can not connect the server.");
            }
            HttpEntity entity = response.getEntity();
            String body = EntityUtils.toString(entity, "UTF-8");
            ObjectMapper objectMapper = new ObjectMapper();
            JsonNode node = objectMapper.readTree(body);
            String hostAddress = node.path("hostAddress").asText();
            String port = node.path("port").asText();
            return "http://" + hostAddress + ":" + port;
        } catch (IOException e) {
            e.printStackTrace();
        }
        finally {
        } finally {
            assert response != null;
            response.close();
            httpclient.close();
        }
        throw new Exception("Can not connect the server.");
    }
}

+ 7 - 0
sdk/java/hos-client/src/main/java/com/yihu/hos/client/Request.java

@ -0,0 +1,7 @@
package com.yihu.hos.client;
/**
 * @created Airhead 2016/8/5.
 */
public class Request {
}

+ 7 - 0
sdk/java/hos-client/src/main/java/com/yihu/hos/client/Response.java

@ -0,0 +1,7 @@
package com.yihu.hos.client;
/**
 * @created Airhead 2016/8/5.
 */
public class Response {
}