Java源码示例:com.orbitz.consul.model.health.Service

示例1
@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;
}
 
示例2
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;
}
 
示例3
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;
}
 
示例4
private ServiceHealth mockNullServiceAddress() {
    ServiceHealth serviceHealth = mock(ServiceHealth.class);
    Service service = mock(Service.class);

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

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

    return serviceHealth;
}