@Test public void getInstances(@Injectable HttpTransport httpTransport) throws ServiceCombException, IOException { final int expectedCode = 200; Response response = new Response(); response.setStatusCode(expectedCode); String responseString = "{\n" + " \"instances\": [\n" + " {\n" + " \"instanceId\": \"1\",\n" + " \"serviceId\": \"2\",\n" + " \"version\": \"0.1\",\n" + " \"hostName\": \"string\",\n" + " \"endpoints\": [\n" + " \"string\"\n" + " ],\n" + " \"status\": \"UP\",\n" + " \"dataCenterInfo\": {\n" + " \"name\": \"string\",\n" + " \"region\": \"string\",\n" + " \"availableZone\": \"string\"\n" + " },\n" + " \"timestamp\": \"string\",\n" + " \"modTimestamp\": \"string\"\n" + " }\n" + " ]\n" + "}"; response.setContent(responseString); Microservice microservice = new Microservice(); new Expectations() { { Deencapsulation.newUninitializedInstance(DefaultHttpTransport.class); result = httpTransport; httpTransport.sendGetRequest(anyString, (Map<String, String>) any); result = response; } }; ServiceCombClient serviceCombClient = new ServiceCombClient(url, httpTransport); List<ServiceInstance> actual = serviceCombClient.getInstances(microservice, null); Assert.assertEquals(1, actual.size()); }
public List<ServiceInstance> getInstances(Microservice microservice, String revision) { List<ServiceInstance> instanceList = new ArrayList<>(); Response response = null; try { Map<String, String> heades = Maps.newHashMap(); String CONSUMER_HEADER = "X-ConsumerId"; heades.put(CONSUMER_HEADER, RegisterCache.getServiceID()); response = httpTransport .sendGetRequest( addParam2URI(buildURI("/registry/instances"), null, revision, microservice), heades); if (response.getStatusCode() == HttpStatus.SC_OK) { Microservice result = JsonUtils.OBJ_MAPPER .readValue(response.getContent(), Microservice.class); if (result == null || result.getInstances() == null) { return instanceList; } String REVISION_HEADER = "X-Resource-Revision"; if (!StringUtils.isEmpty(response.getHeader(REVISION_HEADER))) { MicroserviceHandler.serviceRevision.put( microservice.getServiceName(), response.getHeader(REVISION_HEADER)); } MicroserviceCache.initInsList(result.getInstances(), microservice.getServiceName()); for (MicroserviceInstance instance : result.getInstances()) { int port; String host; if (!instance.getEndpoints().isEmpty()) { String endpoint = instance.getEndpoints().get(0); URI endpointURIBuilder = new URIBuilder(endpoint).build(); port = endpointURIBuilder.getPort(); host = endpointURIBuilder.getHost(); } else { LOGGER.warn( "read response failed. status:" + response.getStatusCode() + "; message:" + response .getStatusMessage() + "; content:" + response.getContent()); return null; } Map<String, String> map = new HashMap<>(); map.put(INSTANCE_STATUS, instance.getStatus().name()); if (instance.getDataCenterInfo() != null) { map.put(ZONE, instance.getDataCenterInfo().getZone()); } instanceList.add( new DefaultServiceInstance(instance.getInstanceId(), instance.getServiceName(), host, port, false, map)); } return instanceList; } else if (response.getStatusCode() != HttpStatus.SC_NOT_MODIFIED) { LOGGER.debug( "read response failed. status:" + response.getStatusCode() + "; message:" + response .getStatusMessage() + "; content:" + response.getContent()); } } catch (URISyntaxException e) { LOGGER.warn("build url failed.", e); } catch (IOException e) { LOGGER.warn("read response failed. " + response); } catch (RemoteServerUnavailableException e) { LOGGER.warn("get instances failed.", e); } return null; }
ServiceCombClient { public List<ServiceInstance> getInstances(Microservice microservice, String revision) { List<ServiceInstance> instanceList = new ArrayList<>(); Response response = null; try { Map<String, String> heades = Maps.newHashMap(); String CONSUMER_HEADER = "X-ConsumerId"; heades.put(CONSUMER_HEADER, RegisterCache.getServiceID()); response = httpTransport .sendGetRequest( addParam2URI(buildURI("/registry/instances"), null, revision, microservice), heades); if (response.getStatusCode() == HttpStatus.SC_OK) { Microservice result = JsonUtils.OBJ_MAPPER .readValue(response.getContent(), Microservice.class); if (result == null || result.getInstances() == null) { return instanceList; } String REVISION_HEADER = "X-Resource-Revision"; if (!StringUtils.isEmpty(response.getHeader(REVISION_HEADER))) { MicroserviceHandler.serviceRevision.put( microservice.getServiceName(), response.getHeader(REVISION_HEADER)); } MicroserviceCache.initInsList(result.getInstances(), microservice.getServiceName()); for (MicroserviceInstance instance : result.getInstances()) { int port; String host; if (!instance.getEndpoints().isEmpty()) { String endpoint = instance.getEndpoints().get(0); URI endpointURIBuilder = new URIBuilder(endpoint).build(); port = endpointURIBuilder.getPort(); host = endpointURIBuilder.getHost(); } else { LOGGER.warn( "read response failed. status:" + response.getStatusCode() + "; message:" + response .getStatusMessage() + "; content:" + response.getContent()); return null; } Map<String, String> map = new HashMap<>(); map.put(INSTANCE_STATUS, instance.getStatus().name()); if (instance.getDataCenterInfo() != null) { map.put(ZONE, instance.getDataCenterInfo().getZone()); } instanceList.add( new DefaultServiceInstance(instance.getInstanceId(), instance.getServiceName(), host, port, false, map)); } return instanceList; } else if (response.getStatusCode() != HttpStatus.SC_NOT_MODIFIED) { LOGGER.debug( "read response failed. status:" + response.getStatusCode() + "; message:" + response .getStatusMessage() + "; content:" + response.getContent()); } } catch (URISyntaxException e) { LOGGER.warn("build url failed.", e); } catch (IOException e) { LOGGER.warn("read response failed. " + response); } catch (RemoteServerUnavailableException e) { LOGGER.warn("get instances failed.", e); } return null; } }
ServiceCombClient { public List<ServiceInstance> getInstances(Microservice microservice, String revision) { List<ServiceInstance> instanceList = new ArrayList<>(); Response response = null; try { Map<String, String> heades = Maps.newHashMap(); String CONSUMER_HEADER = "X-ConsumerId"; heades.put(CONSUMER_HEADER, RegisterCache.getServiceID()); response = httpTransport .sendGetRequest( addParam2URI(buildURI("/registry/instances"), null, revision, microservice), heades); if (response.getStatusCode() == HttpStatus.SC_OK) { Microservice result = JsonUtils.OBJ_MAPPER .readValue(response.getContent(), Microservice.class); if (result == null || result.getInstances() == null) { return instanceList; } String REVISION_HEADER = "X-Resource-Revision"; if (!StringUtils.isEmpty(response.getHeader(REVISION_HEADER))) { MicroserviceHandler.serviceRevision.put( microservice.getServiceName(), response.getHeader(REVISION_HEADER)); } MicroserviceCache.initInsList(result.getInstances(), microservice.getServiceName()); for (MicroserviceInstance instance : result.getInstances()) { int port; String host; if (!instance.getEndpoints().isEmpty()) { String endpoint = instance.getEndpoints().get(0); URI endpointURIBuilder = new URIBuilder(endpoint).build(); port = endpointURIBuilder.getPort(); host = endpointURIBuilder.getHost(); } else { LOGGER.warn( "read response failed. status:" + response.getStatusCode() + "; message:" + response .getStatusMessage() + "; content:" + response.getContent()); return null; } Map<String, String> map = new HashMap<>(); map.put(INSTANCE_STATUS, instance.getStatus().name()); if (instance.getDataCenterInfo() != null) { map.put(ZONE, instance.getDataCenterInfo().getZone()); } instanceList.add( new DefaultServiceInstance(instance.getInstanceId(), instance.getServiceName(), host, port, false, map)); } return instanceList; } else if (response.getStatusCode() != HttpStatus.SC_NOT_MODIFIED) { LOGGER.debug( "read response failed. status:" + response.getStatusCode() + "; message:" + response .getStatusMessage() + "; content:" + response.getContent()); } } catch (URISyntaxException e) { LOGGER.warn("build url failed.", e); } catch (IOException e) { LOGGER.warn("read response failed. " + response); } catch (RemoteServerUnavailableException e) { LOGGER.warn("get instances failed.", e); } return null; } ServiceCombClient(String urls, HttpTransport httpTransport); }
ServiceCombClient { public List<ServiceInstance> getInstances(Microservice microservice, String revision) { List<ServiceInstance> instanceList = new ArrayList<>(); Response response = null; try { Map<String, String> heades = Maps.newHashMap(); String CONSUMER_HEADER = "X-ConsumerId"; heades.put(CONSUMER_HEADER, RegisterCache.getServiceID()); response = httpTransport .sendGetRequest( addParam2URI(buildURI("/registry/instances"), null, revision, microservice), heades); if (response.getStatusCode() == HttpStatus.SC_OK) { Microservice result = JsonUtils.OBJ_MAPPER .readValue(response.getContent(), Microservice.class); if (result == null || result.getInstances() == null) { return instanceList; } String REVISION_HEADER = "X-Resource-Revision"; if (!StringUtils.isEmpty(response.getHeader(REVISION_HEADER))) { MicroserviceHandler.serviceRevision.put( microservice.getServiceName(), response.getHeader(REVISION_HEADER)); } MicroserviceCache.initInsList(result.getInstances(), microservice.getServiceName()); for (MicroserviceInstance instance : result.getInstances()) { int port; String host; if (!instance.getEndpoints().isEmpty()) { String endpoint = instance.getEndpoints().get(0); URI endpointURIBuilder = new URIBuilder(endpoint).build(); port = endpointURIBuilder.getPort(); host = endpointURIBuilder.getHost(); } else { LOGGER.warn( "read response failed. status:" + response.getStatusCode() + "; message:" + response .getStatusMessage() + "; content:" + response.getContent()); return null; } Map<String, String> map = new HashMap<>(); map.put(INSTANCE_STATUS, instance.getStatus().name()); if (instance.getDataCenterInfo() != null) { map.put(ZONE, instance.getDataCenterInfo().getZone()); } instanceList.add( new DefaultServiceInstance(instance.getInstanceId(), instance.getServiceName(), host, port, false, map)); } return instanceList; } else if (response.getStatusCode() != HttpStatus.SC_NOT_MODIFIED) { LOGGER.debug( "read response failed. status:" + response.getStatusCode() + "; message:" + response .getStatusMessage() + "; content:" + response.getContent()); } } catch (URISyntaxException e) { LOGGER.warn("build url failed.", e); } catch (IOException e) { LOGGER.warn("read response failed. " + response); } catch (RemoteServerUnavailableException e) { LOGGER.warn("get instances failed.", e); } return null; } 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 List<ServiceInstance> getInstances(Microservice microservice, String revision) { List<ServiceInstance> instanceList = new ArrayList<>(); Response response = null; try { Map<String, String> heades = Maps.newHashMap(); String CONSUMER_HEADER = "X-ConsumerId"; heades.put(CONSUMER_HEADER, RegisterCache.getServiceID()); response = httpTransport .sendGetRequest( addParam2URI(buildURI("/registry/instances"), null, revision, microservice), heades); if (response.getStatusCode() == HttpStatus.SC_OK) { Microservice result = JsonUtils.OBJ_MAPPER .readValue(response.getContent(), Microservice.class); if (result == null || result.getInstances() == null) { return instanceList; } String REVISION_HEADER = "X-Resource-Revision"; if (!StringUtils.isEmpty(response.getHeader(REVISION_HEADER))) { MicroserviceHandler.serviceRevision.put( microservice.getServiceName(), response.getHeader(REVISION_HEADER)); } MicroserviceCache.initInsList(result.getInstances(), microservice.getServiceName()); for (MicroserviceInstance instance : result.getInstances()) { int port; String host; if (!instance.getEndpoints().isEmpty()) { String endpoint = instance.getEndpoints().get(0); URI endpointURIBuilder = new URIBuilder(endpoint).build(); port = endpointURIBuilder.getPort(); host = endpointURIBuilder.getHost(); } else { LOGGER.warn( "read response failed. status:" + response.getStatusCode() + "; message:" + response .getStatusMessage() + "; content:" + response.getContent()); return null; } Map<String, String> map = new HashMap<>(); map.put(INSTANCE_STATUS, instance.getStatus().name()); if (instance.getDataCenterInfo() != null) { map.put(ZONE, instance.getDataCenterInfo().getZone()); } instanceList.add( new DefaultServiceInstance(instance.getInstanceId(), instance.getServiceName(), host, port, false, map)); } return instanceList; } else if (response.getStatusCode() != HttpStatus.SC_NOT_MODIFIED) { LOGGER.debug( "read response failed. status:" + response.getStatusCode() + "; message:" + response .getStatusMessage() + "; content:" + response.getContent()); } } catch (URISyntaxException e) { LOGGER.warn("build url failed.", e); } catch (IOException e) { LOGGER.warn("read response failed. " + response); } catch (RemoteServerUnavailableException e) { LOGGER.warn("get instances failed.", e); } return null; } 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; }