@Test public void registerInstance(@Injectable HttpTransport httpTransport) throws ServiceCombException, IOException { final int expectedCode = 200; Response response = new Response(); response.setStatusCode(expectedCode); String responseString = "{\"instanceId\": \"22222\"}"; response.setContent(responseString); MicroserviceInstance microserviceInstance = new MicroserviceInstance(); microserviceInstance.setServiceId("111111"); new Expectations() { { Deencapsulation.newUninitializedInstance(DefaultHttpTransport.class); result = httpTransport; httpTransport.sendPostRequest(anyString, (HttpEntity) any); result = response; } }; ServiceCombClient serviceCombClient = new ServiceCombClient(url, httpTransport); String actual = serviceCombClient.registerInstance(microserviceInstance); Assert.assertEquals("22222", actual); }
public String registerInstance(MicroserviceInstance microserviceInstance) throws ServiceCombException { Response response = null; try { ObjectMapper objectMapper = new ObjectMapper(); objectMapper.configure(SerializationFeature.WRAP_ROOT_VALUE, true); String content = objectMapper.writeValueAsString(microserviceInstance); StringEntity stringEntity = new StringEntity(content, "utf-8"); String formatUrl = buildURI( "/registry/microservices/" + microserviceInstance.getServiceId() + "/instances"); response = httpTransport .sendPostRequest(formatUrl, stringEntity); if (response.getStatusCode() == HttpStatus.SC_OK) { LOGGER.info(response.getContent()); HashMap<String, String> result = objectMapper .readValue(response.getContent(), HashMap.class); return result.get("instanceId"); } else { throw new RemoteOperationException( "read response failed. url:" + formatUrl + "status:" + response.getStatusCode() + "; message:" + response.getStatusMessage() + "; content:" + response .getContent()); } } catch (URISyntaxException e) { throw new RemoteOperationException("build url failed.", e); } catch (IOException e) { throw new RemoteOperationException( response == null ? "read response failed. " : "read response failed. " + response, e); } }
ServiceCombClient { public String registerInstance(MicroserviceInstance microserviceInstance) throws ServiceCombException { Response response = null; try { ObjectMapper objectMapper = new ObjectMapper(); objectMapper.configure(SerializationFeature.WRAP_ROOT_VALUE, true); String content = objectMapper.writeValueAsString(microserviceInstance); StringEntity stringEntity = new StringEntity(content, "utf-8"); String formatUrl = buildURI( "/registry/microservices/" + microserviceInstance.getServiceId() + "/instances"); response = httpTransport .sendPostRequest(formatUrl, stringEntity); if (response.getStatusCode() == HttpStatus.SC_OK) { LOGGER.info(response.getContent()); HashMap<String, String> result = objectMapper .readValue(response.getContent(), HashMap.class); return result.get("instanceId"); } else { throw new RemoteOperationException( "read response failed. url:" + formatUrl + "status:" + response.getStatusCode() + "; message:" + response.getStatusMessage() + "; content:" + response .getContent()); } } catch (URISyntaxException e) { throw new RemoteOperationException("build url failed.", e); } catch (IOException e) { throw new RemoteOperationException( response == null ? "read response failed. " : "read response failed. " + response, e); } } }
ServiceCombClient { public String registerInstance(MicroserviceInstance microserviceInstance) throws ServiceCombException { Response response = null; try { ObjectMapper objectMapper = new ObjectMapper(); objectMapper.configure(SerializationFeature.WRAP_ROOT_VALUE, true); String content = objectMapper.writeValueAsString(microserviceInstance); StringEntity stringEntity = new StringEntity(content, "utf-8"); String formatUrl = buildURI( "/registry/microservices/" + microserviceInstance.getServiceId() + "/instances"); response = httpTransport .sendPostRequest(formatUrl, stringEntity); if (response.getStatusCode() == HttpStatus.SC_OK) { LOGGER.info(response.getContent()); HashMap<String, String> result = objectMapper .readValue(response.getContent(), HashMap.class); return result.get("instanceId"); } else { throw new RemoteOperationException( "read response failed. url:" + formatUrl + "status:" + response.getStatusCode() + "; message:" + response.getStatusMessage() + "; content:" + response .getContent()); } } catch (URISyntaxException e) { throw new RemoteOperationException("build url failed.", e); } catch (IOException e) { throw new RemoteOperationException( response == null ? "read response failed. " : "read response failed. " + response, e); } } ServiceCombClient(String urls, HttpTransport httpTransport); }
ServiceCombClient { public String registerInstance(MicroserviceInstance microserviceInstance) throws ServiceCombException { Response response = null; try { ObjectMapper objectMapper = new ObjectMapper(); objectMapper.configure(SerializationFeature.WRAP_ROOT_VALUE, true); String content = objectMapper.writeValueAsString(microserviceInstance); StringEntity stringEntity = new StringEntity(content, "utf-8"); String formatUrl = buildURI( "/registry/microservices/" + microserviceInstance.getServiceId() + "/instances"); response = httpTransport .sendPostRequest(formatUrl, stringEntity); if (response.getStatusCode() == HttpStatus.SC_OK) { LOGGER.info(response.getContent()); HashMap<String, String> result = objectMapper .readValue(response.getContent(), HashMap.class); return result.get("instanceId"); } else { throw new RemoteOperationException( "read response failed. url:" + formatUrl + "status:" + response.getStatusCode() + "; message:" + response.getStatusMessage() + "; content:" + response .getContent()); } } catch (URISyntaxException e) { throw new RemoteOperationException("build url failed.", e); } catch (IOException e) { throw new RemoteOperationException( response == null ? "read response failed. " : "read response failed. " + response, e); } } ServiceCombClient(String urls, HttpTransport httpTransport); void autoDiscovery(boolean autoDiscovery); MicroserviceInstancesResponse getServiceCenterInstances(); String registerMicroservice(Microservice microservice); String getServiceId(Microservice microservice); SchemaResponse getSchemas(String serviceId); String registerInstance(MicroserviceInstance microserviceInstance); boolean deRegisterInstance(String serviceId, String instanceId); List<ServiceInstance> getInstances(Microservice microservice, String revision); MicroserviceInstanceSingleResponse getInstance(String serviceId, String instanceId); HeardBeatStatus heartbeat(HeartbeatRequest heartbeatRequest); boolean updateInstanceStatus(String serviceId, String instanceId, String status); MicroserviceResponse getServices(); boolean registerSchema(String microserviceId, String schemaId, String schemaContent); static String calcSchemaSummary(String schemaContent); void toggle(); String getUrl(); }
ServiceCombClient { public String registerInstance(MicroserviceInstance microserviceInstance) throws ServiceCombException { Response response = null; try { ObjectMapper objectMapper = new ObjectMapper(); objectMapper.configure(SerializationFeature.WRAP_ROOT_VALUE, true); String content = objectMapper.writeValueAsString(microserviceInstance); StringEntity stringEntity = new StringEntity(content, "utf-8"); String formatUrl = buildURI( "/registry/microservices/" + microserviceInstance.getServiceId() + "/instances"); response = httpTransport .sendPostRequest(formatUrl, stringEntity); if (response.getStatusCode() == HttpStatus.SC_OK) { LOGGER.info(response.getContent()); HashMap<String, String> result = objectMapper .readValue(response.getContent(), HashMap.class); return result.get("instanceId"); } else { throw new RemoteOperationException( "read response failed. url:" + formatUrl + "status:" + response.getStatusCode() + "; message:" + response.getStatusMessage() + "; content:" + response .getContent()); } } catch (URISyntaxException e) { throw new RemoteOperationException("build url failed.", e); } catch (IOException e) { throw new RemoteOperationException( response == null ? "read response failed. " : "read response failed. " + response, e); } } ServiceCombClient(String urls, HttpTransport httpTransport); void autoDiscovery(boolean autoDiscovery); MicroserviceInstancesResponse getServiceCenterInstances(); String registerMicroservice(Microservice microservice); String getServiceId(Microservice microservice); SchemaResponse getSchemas(String serviceId); String registerInstance(MicroserviceInstance microserviceInstance); boolean deRegisterInstance(String serviceId, String instanceId); List<ServiceInstance> getInstances(Microservice microservice, String revision); MicroserviceInstanceSingleResponse getInstance(String serviceId, String instanceId); HeardBeatStatus heartbeat(HeartbeatRequest heartbeatRequest); boolean updateInstanceStatus(String serviceId, String instanceId, String status); MicroserviceResponse getServices(); boolean registerSchema(String microserviceId, String schemaId, String schemaContent); static String calcSchemaSummary(String schemaContent); void toggle(); String getUrl(); static String INSTANCE_STATUS; static String ZONE; }