Java Code Examples for org.springframework.cloud.client.discovery.DiscoveryClient#getInstances()
The following examples show how to use
org.springframework.cloud.client.discovery.DiscoveryClient#getInstances() .
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: Utils.java From WeEvent with Apache License 2.0 | 5 votes |
public static String getUrlFromDiscovery(DiscoveryClient discoveryClient, String serviceId) { List<ServiceInstance> serviceInstances = discoveryClient.getInstances(serviceId); if (serviceInstances.isEmpty()) { if (processorServiceId.equals(serviceId)) { return processorServiceUrl; } else { return brokerServiceUrl; } } ServiceInstance serviceInstance = serviceInstances.get(new Random().nextInt(serviceInstances.size())); return serviceInstance.getUri().toString(); }
Example 2
Source File: KubernetesDiscoveryClientTest.java From spring-cloud-kubernetes with Apache License 2.0 | 5 votes |
@Test public void getInstancesShouldBeAbleToHandleEndpointsSingleAddress() { mockServer.expect().get().withPath("/api/v1/namespaces/test/endpoints/endpoint") .andReturn(200, new EndpointsBuilder().withNewMetadata() .withName("endpoint").endMetadata().addNewSubset().addNewAddress() .withIp("ip1").withNewTargetRef().withUid("uid1").endTargetRef() .endAddress().addNewPort("http", 80, "TCP").endSubset().build()) .once(); mockServer.expect().get().withPath("/api/v1/services/endpoint") .andReturn(200, new ServiceBuilder().withNewMetadata() .withName("endpoint").withLabels(new HashMap<String, String>() { { put("l", "v"); } }).endMetadata().build()) .always(); final KubernetesDiscoveryProperties properties = new KubernetesDiscoveryProperties(); final DiscoveryClient discoveryClient = new KubernetesDiscoveryClient(mockClient, properties, KubernetesClient::services, new DefaultIsServicePortSecureResolver(properties)); final List<ServiceInstance> instances = discoveryClient.getInstances("endpoint"); assertThat(instances).hasSize(1) .filteredOn(s -> s.getHost().equals("ip1") && !s.isSecure()).hasSize(1) .filteredOn(s -> s.getInstanceId().equals("uid1")).hasSize(1); }
Example 3
Source File: KubernetesDiscoveryClientTest.java From spring-cloud-kubernetes with Apache License 2.0 | 5 votes |
@Test public void getInstancesShouldBeAbleToHandleEndpointsSingleAddressAndMultiplePorts() { mockServer.expect().get().withPath("/api/v1/namespaces/test/endpoints/endpoint") .andReturn(200, new EndpointsBuilder().withNewMetadata() .withName("endpoint").endMetadata().addNewSubset().addNewAddress() .withIp("ip1").withNewTargetRef().withUid("uid").endTargetRef() .endAddress().addNewPort("mgmt", 9000, "TCP") .addNewPort("http", 80, "TCP").endSubset().build()) .once(); mockServer.expect().get().withPath("/api/v1/namespaces/test/services/endpoint") .andReturn(200, new ServiceBuilder().withNewMetadata() .withName("endpoint").withLabels(new HashMap<String, String>() { { put("l", "v"); } }).endMetadata().build()) .always(); final KubernetesDiscoveryProperties properties = new KubernetesDiscoveryProperties(); properties.setPrimaryPortName("http"); final DiscoveryClient discoveryClient = new KubernetesDiscoveryClient(mockClient, properties, KubernetesClient::services, new DefaultIsServicePortSecureResolver(properties)); final List<ServiceInstance> instances = discoveryClient.getInstances("endpoint"); assertThat(instances).hasSize(1) .filteredOn(s -> s.getHost().equals("ip1") && !s.isSecure()).hasSize(1) .filteredOn(s -> s.getInstanceId().equals("uid")).hasSize(1) .filteredOn(s -> 80 == s.getPort()).hasSize(1); }
Example 4
Source File: KubernetesDiscoveryClientTest.java From spring-cloud-kubernetes with Apache License 2.0 | 5 votes |
@Test public void getInstancesShouldBeAbleToHandleEndpointsMultipleAddresses() { mockServer.expect().get().withPath("/api/v1/namespaces/test/endpoints/endpoint") .andReturn(200, new EndpointsBuilder().withNewMetadata() .withName("endpoint").endMetadata().addNewSubset().addNewAddress() .withIp("ip1").endAddress().addNewAddress().withIp("ip2") .endAddress().addNewPort("https", 443, "TCP").endSubset().build()) .once(); mockServer.expect().get().withPath("/api/v1/namespaces/test/services/endpoint") .andReturn(200, new ServiceBuilder().withNewMetadata() .withName("endpoint").withLabels(new HashMap<String, String>() { { put("l", "v"); } }).endMetadata().build()) .always(); final KubernetesDiscoveryProperties properties = new KubernetesDiscoveryProperties(); final DiscoveryClient discoveryClient = new KubernetesDiscoveryClient(mockClient, properties, KubernetesClient::services, new DefaultIsServicePortSecureResolver(properties)); final List<ServiceInstance> instances = discoveryClient.getInstances("endpoint"); assertThat(instances).hasSize(2).filteredOn(ServiceInstance::isSecure) .extracting(ServiceInstance::getHost).containsOnly("ip1", "ip2"); }
Example 5
Source File: ZookeeperDiscoveryWithDependenciesIntegrationTests.java From spring-cloud-zookeeper with Apache License 2.0 | 5 votes |
@Test public void should_find_a_collaborator_via_discovery_client() { // // given: final DiscoveryClient discoveryClient = this.discoveryClient; List<ServiceInstance> instances = discoveryClient.getInstances("someAlias"); final ServiceInstance instance = instances.get(0); // expect: await().until(() -> callingServiceViaUrlOnBeansEndpointIsNotEmpty(instance)); }
Example 6
Source File: CompositeDiscoveryClient.java From spring-cloud-commons with Apache License 2.0 | 5 votes |
@Override public List<ServiceInstance> getInstances(String serviceId) { if (this.discoveryClients != null) { for (DiscoveryClient discoveryClient : this.discoveryClients) { List<ServiceInstance> instances = discoveryClient.getInstances(serviceId); if (instances != null && !instances.isEmpty()) { return instances; } } } return Collections.emptyList(); }
Example 7
Source File: KubernetesDiscoveryClientTest.java From spring-cloud-kubernetes with Apache License 2.0 | 4 votes |
@Test public void getInstancesShouldBeAbleToHandleEndpointsFromMultipleNamespaces() { Endpoints endPoints1 = new EndpointsBuilder().withNewMetadata() .withName("endpoint").withNamespace("test").endMetadata().addNewSubset() .addNewAddress().withIp("ip1").withNewTargetRef().withUid("uid1") .endTargetRef().endAddress().addNewPort("http", 80, "TCP").endSubset() .build(); Endpoints endpoints2 = new EndpointsBuilder().withNewMetadata() .withName("endpoint").withNamespace("test2").endMetadata().addNewSubset() .addNewAddress().withIp("ip2").withNewTargetRef().withUid("uid2") .endTargetRef().endAddress().addNewPort("http", 80, "TCP").endSubset() .build(); List<Endpoints> endpointsList = new ArrayList<>(); endpointsList.add(endPoints1); endpointsList.add(endpoints2); EndpointsList endpoints = new EndpointsList(); endpoints.setItems(endpointsList); mockServer.expect().get() .withPath("/api/v1/endpoints?fieldSelector=metadata.name%3Dendpoint") .andReturn(200, endpoints).once(); mockServer.expect().get().withPath("/api/v1/namespaces/test/endpoints/endpoint") .andReturn(200, endPoints1).once(); mockServer.expect().get().withPath("/api/v1/namespaces/test2/endpoints/endpoint") .andReturn(200, endpoints2).once(); Service service1 = new ServiceBuilder().withNewMetadata().withName("endpoint") .withNamespace("test").withLabels(new HashMap<String, String>() { { put("l", "v"); } }).endMetadata().build(); Service service2 = new ServiceBuilder().withNewMetadata().withName("endpoint") .withNamespace("test2").withLabels(new HashMap<String, String>() { { put("l", "v"); } }).endMetadata().build(); List<Service> servicesList = new ArrayList<>(); servicesList.add(service1); servicesList.add(service2); ServiceList services = new ServiceList(); services.setItems(servicesList); mockServer.expect().get() .withPath("/api/v1/services?fieldSelector=metadata.name%3Dendpoint") .andReturn(200, services).once(); mockServer.expect().get().withPath("/api/v1/namespaces/test/services/endpoint") .andReturn(200, service1).always(); mockServer.expect().get().withPath("/api/v1/namespaces/test2/services/endpoint") .andReturn(200, service2).always(); final KubernetesDiscoveryProperties properties = new KubernetesDiscoveryProperties(); properties.setAllNamespaces(true); final DiscoveryClient discoveryClient = new KubernetesDiscoveryClient(mockClient, properties, KubernetesClient::services, new DefaultIsServicePortSecureResolver(properties)); final List<ServiceInstance> instances = discoveryClient.getInstances("endpoint"); assertThat(instances).hasSize(2); assertThat(instances).filteredOn(s -> s.getHost().equals("ip1") && !s.isSecure()) .hasSize(1); assertThat(instances).filteredOn(s -> s.getHost().equals("ip2") && !s.isSecure()) .hasSize(1); assertThat(instances).filteredOn(s -> s.getInstanceId().equals("uid1")) .hasSize(1); assertThat(instances).filteredOn(s -> s.getInstanceId().equals("uid2")) .hasSize(1); }