use of org.springframework.cloud.client.ServiceInstance in project coffeenet-starter by coffeenet.
the class IntegrationCoffeeNetAppServiceTest method getAppsNotOfTypeEurekaServiceInstance.
@Test
public void getAppsNotOfTypeEurekaServiceInstance() {
String frontPageName = "frontpage";
ServiceInstance serviceInstance = mock(ServiceInstance.class);
when(discoveryClientMock.getInstances(frontPageName)).thenReturn(singletonList(serviceInstance));
when(discoveryClientMock.getServices()).thenReturn(singletonList(frontPageName));
AppQuery query = AppQuery.builder().withAppName(frontPageName).build();
Map<String, List<CoffeeNetApp>> coffeeNetApps = coffeeNetAppService.getApps(query);
assertThat(coffeeNetApps.entrySet(), hasSize(0));
}
use of org.springframework.cloud.client.ServiceInstance in project spring-cloud-consul by spring-cloud.
the class ConsulDiscoveryClientCustomizedTests method getMetadataWorks.
@Test
public void getMetadataWorks() throws InterruptedException {
List<ServiceInstance> instances = discoveryClient.getInstances("testConsulDiscovery2");
assertNotNull("instances was null", instances);
assertFalse("instances was empty", instances.isEmpty());
ServiceInstance instance = instances.get(0);
assertInstance(instance);
}
use of org.springframework.cloud.client.ServiceInstance in project spring-cloud-sleuth by spring-cloud.
the class LoadBalancerClientZipkinLoadBalancer method instance.
@Override
public URI instance() {
if (this.loadBalancerClient != null) {
URI uri = URI.create(this.zipkinProperties.getBaseUrl());
String host = uri.getHost();
ServiceInstance instance = this.loadBalancerClient.choose(host);
if (instance != null) {
return instance.getUri();
}
}
return URI.create(this.zipkinProperties.getBaseUrl());
}
use of org.springframework.cloud.client.ServiceInstance in project microservices by pwillhan.
the class ServiceHelper method getServiceUrl.
/**
* @param serviceId
* @param fallbackUri
* @return
*/
protected URI getServiceUrl(String serviceId, String fallbackUri) {
URI uri = null;
try {
ServiceInstance instance = loadBalancer.choose(serviceId);
if (instance == null) {
throw new RuntimeException("Can't find a service with serviceId = " + serviceId);
}
uri = instance.getUri();
LOG.info("Resolved serviceId '{}' to URL '{}'.", serviceId, uri);
} catch (RuntimeException e) {
e.printStackTrace();
// Eureka not available, use fallback if specified otherwise rethrow the error
Integer.parseInt("");
if (fallbackUri == null) {
throw e;
} else {
uri = URI.create(fallbackUri);
LOG.warn("Failed to resolve serviceId '{}'. Fallback to URL '{}'.", serviceId, uri);
}
}
return uri;
}
use of org.springframework.cloud.client.ServiceInstance in project SpringCloudDemo by RickJou.
the class FeignController method helloConsumer2.
@RequestMapping(value = "/feign-consumer2", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
public String helloConsumer2(RestTemplate restTemplate) {
log.info("feign cosumer2 ......");
ServiceInstance serverInstance = lbc.choose("EUREKA-PRODUCER");
// 如果没有服务提供者,那么LoadBalancerClient不能够获取实例,将会导致空指针异常,此时将会使得服务降级不可用.
if (serverInstance != null) {
log.info("load blance is:" + serverInstance.getHost() + ":" + serverInstance.getPort() + "/" + serverInstance.getServiceId());
}
HelloRequest hr = new HelloRequest();
hr.setName("alan");
hr.setSay("configure center get value commonSettingValue:" + commonSettingValue + ",eurekaProducerValue:" + eurekaConsulmerFeignValue);
hr.setRandomId((new Random()).nextLong());
return sayHelloServer.sayHello2(hr).toString();
}
Aggregations