Search in sources :

Example 51 with ServiceInstance

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));
}
Also used : ServiceInstance(org.springframework.cloud.client.ServiceInstance) EurekaServiceInstance(org.springframework.cloud.netflix.eureka.EurekaDiscoveryClient.EurekaServiceInstance) Collections.emptyList(java.util.Collections.emptyList) Collections.singletonList(java.util.Collections.singletonList) List(java.util.List) Arrays.asList(java.util.Arrays.asList) Test(org.junit.Test)

Example 52 with ServiceInstance

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);
}
Also used : ServiceInstance(org.springframework.cloud.client.ServiceInstance) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 53 with ServiceInstance

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());
}
Also used : ServiceInstance(org.springframework.cloud.client.ServiceInstance) URI(java.net.URI)

Example 54 with ServiceInstance

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;
}
Also used : ServiceInstance(org.springframework.cloud.client.ServiceInstance) URI(java.net.URI)

Example 55 with ServiceInstance

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();
}
Also used : Random(java.util.Random) HelloRequest(com.demo.protocol.HelloRequest) ServiceInstance(org.springframework.cloud.client.ServiceInstance) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

ServiceInstance (org.springframework.cloud.client.ServiceInstance)73 DefaultServiceInstance (org.springframework.cloud.client.DefaultServiceInstance)25 Test (org.junit.Test)24 HashMap (java.util.HashMap)12 URI (java.net.URI)11 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)11 Registration (de.codecentric.boot.admin.server.domain.values.Registration)10 ArrayList (java.util.ArrayList)10 Test (org.junit.jupiter.api.Test)10 Application (de.codecentric.boot.admin.model.Application)7 Map (java.util.Map)7 List (java.util.List)6 RibbonServer (org.springframework.cloud.netflix.ribbon.RibbonLoadBalancerClient.RibbonServer)6 IClientConfig (com.netflix.client.config.IClientConfig)5 Random (java.util.Random)4 Collections (java.util.Collections)3 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)3 MonitorProperties (com.alibaba.druid.admin.config.MonitorProperties)2 ServiceNode (com.alibaba.druid.admin.model.ServiceNode)2 ConnectionResult (com.alibaba.druid.admin.model.dto.ConnectionResult)2