int registerResult =
- HttpClientUtil.httpPostWithJSON(consulRegisterurl, JacksonJsonUtil.beanToJson(agentService));
+ HttpClientUtil.httpPutWithJSON(consulRegisterurl, JacksonJsonUtil.beanToJson(agentService));
return registerResult;
String consulDelurl = (new StringBuilder().append("http://").append(ConfigUtil.getInstance().getConsulAddress())
.append(DiscoverUtil.CONSUL_AGENT_URL).append("/deregister/").append(serviceId)).toString();
- int delResult = HttpClientUtil.httpPostWithJSON(consulDelurl, "");
+ int delResult = HttpClientUtil.httpPutWithJSON(consulDelurl, "");
return delResult;
}
int registerResult =
- HttpClientUtil.httpPostWithJSON(consulRegisterurl, JacksonJsonUtil.beanToJson(catalogNode));
+ HttpClientUtil.httpPutWithJSON(consulRegisterurl, JacksonJsonUtil.beanToJson(catalogNode));
return registerResult;
String nodeJson =
"{\"Node\": \"" + DiscoverUtil.EXTERNAL_NODE_NAME + "\",\"ServiceID\": \"" + serviceId + "\"}";
- int delResult = HttpClientUtil.httpPostWithJSON(consulDelurl, nodeJson);
+ int delResult = HttpClientUtil.httpPutWithJSON(consulDelurl, nodeJson);
return delResult;
}
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
+import org.apache.http.client.methods.HttpPut;
import org.apache.http.client.utils.URLEncodedUtils;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
private static final Logger logger = LoggerFactory.getLogger(HttpClientUtil.class);
+ public static int httpPutWithJSON(String url, String params) {
+ int result = 0;
+ CloseableHttpClient httpClient = HttpClients.createDefault();
+ HttpPut httpPut = new HttpPut(url);
+ httpPut.addHeader("Content-type", "application/json; charset=utf-8");
+ httpPut.setHeader("Accept", "application/json");
+ httpPut.setEntity(new StringEntity(params, Charset.forName("UTF-8")));
+ try {
+ CloseableHttpResponse res = httpClient.execute(httpPut);
+ result = res.getStatusLine().getStatusCode();
+ if (res.getStatusLine().getStatusCode() != 200) {
+ logger.error(String.valueOf(result));
+ }
+ res.close();
+ } catch (IOException e) {
+ String errorMsg = url + ":httpPutWithJSON connect faild";
+ } finally {
+ try {
+ httpClient.close();
+ } catch (IOException e) {
+ String errorMsg = url + ":close httpClient faild";
+ }
+ }
+
+ return result;
+
+ }
+
public static int httpPostWithJSON(String url, String params) {
int result = 0;
CloseableHttpClient httpClient = HttpClients.createDefault();
private void mockDelete() {
String serviceJson = "{\"Node\": \"externalService\",\"ServiceID\": \"_test_10.74.56.36_5656\"}";
- PowerMockito.when(HttpClientUtil.httpPostWithJSON(mockdeltUrl, serviceJson)).thenReturn(200);
+ PowerMockito.when(HttpClientUtil.httpPutWithJSON(mockdeltUrl, serviceJson)).thenReturn(200);
}
private void mockDelete4agent() {
- PowerMockito.when(HttpClientUtil.httpPostWithJSON(
+ PowerMockito.when(HttpClientUtil.httpPutWithJSON(
"http://127.0.0.1:8500/v1/agent/service/deregister/_test_10.74.56.36_5656", ""))
.thenReturn(200);
PowerMockito.mockStatic(HttpClientUtil.class);
String serviceJson =
"{\"Node\":\"externalService\",\"Address\":\"127.0.0.1\",\"Service\":{\"ID\":\"_test_10.74.44.1_10080\",\"Service\":\"test\",\"Tags\":[\"\\\"base\\\":{\\\"protocol\\\":\\\"REST\\\",\\\"status\\\":\\\"1\\\",\\\"enable_ssl\\\":\\\"false\\\",\\\"is_manual\\\":\\\"true\\\",\\\"url\\\":\\\"/api/test/v1\\\",\\\"version\\\":\\\"v1\\\"}\",\"\\\"labels\\\":{\\\"visualRange\\\":\\\"1\\\"}\"],\"Address\":\"10.74.44.1\",\"Port\":10080}}";
- PowerMockito.when(HttpClientUtil.httpPostWithJSON(mockPostUrl, serviceJson)).thenReturn(200);
+ PowerMockito.when(HttpClientUtil.httpPutWithJSON(mockPostUrl, serviceJson)).thenReturn(200);
}