com.orbitz.consul.model.health.Service Java Examples

The following examples show how to use com.orbitz.consul.model.health.Service. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ConsulDiscoveryConfigSource.java    From pragmatic-microservices-lab with MIT License 5 votes vote down vote up
@Override
public String getValue(String propertyName) {
    if (propertyName.startsWith(CONFIG_DISCOVERY_SERVICE_PREFIX)) {
        Consul consul = ConsulClient.build();
        String[] serviceNameAndProp = propertyName.split("\\.");
        if (serviceNameAndProp.length == 4) {
            String serviceName = serviceNameAndProp[2];
            String serviceProp = serviceNameAndProp[3];
            if (serviceProp.equals("url")) {
                // propertyName is "discovery.service.NAME.url"
                // result is a url that doesn't end with / and optionally includes context root
                List<ServiceHealth> services = consul.healthClient()
                        .getHealthyServiceInstances(serviceName).getResponse();
                if (!services.isEmpty()) {
                    int randomIndex = new Random().nextInt(services.size());
                    Service selectedService = services.get(randomIndex).getService();
                    String contextRoot = selectedService.getMeta().getOrDefault(ConsulClient.KEY_CTX_ROOT, "");
                    if ( !contextRoot.isEmpty() && !contextRoot.startsWith("/")) {
                        contextRoot = "/" + contextRoot;
                    }
                    StringBuilder resultBuilder = new StringBuilder();
                    resultBuilder = resultBuilder.append("http://").append(selectedService.getAddress())
                            .append(":").append(selectedService.getPort())
                            .append(contextRoot);
                    return resultBuilder.toString();
                }
            }
        }
    }
    return null;
}
 
Example #2
Source File: ConsulCoordinatorTest.java    From skywalking with Apache License 2.0 5 votes vote down vote up
private ServiceHealth mockNotSelfService() {
    ServiceHealth serviceHealth = mock(ServiceHealth.class);
    Service service = mock(Service.class);

    when(service.getAddress()).thenReturn(remoteAddress.getHost());
    when(service.getPort()).thenReturn(remoteAddress.getPort());

    when(serviceHealth.getService()).thenReturn(service);

    return serviceHealth;
}
 
Example #3
Source File: ConsulCoordinatorTest.java    From skywalking with Apache License 2.0 5 votes vote down vote up
private ServiceHealth mockSelfService() {
    ServiceHealth serviceHealth = mock(ServiceHealth.class);
    Service service = mock(Service.class);

    when(service.getAddress()).thenReturn(selfRemoteAddress.getHost());
    when(service.getPort()).thenReturn(selfRemoteAddress.getPort());

    when(serviceHealth.getService()).thenReturn(service);

    return serviceHealth;
}
 
Example #4
Source File: ConsulCoordinatorTest.java    From skywalking with Apache License 2.0 3 votes vote down vote up
private ServiceHealth mockNullServiceAddress() {
    ServiceHealth serviceHealth = mock(ServiceHealth.class);
    Service service = mock(Service.class);

    when(serviceHealth.getService()).thenReturn(service);

    when(service.getAddress()).thenReturn("");

    return serviceHealth;
}