Java Code Examples for org.springframework.cloud.client.discovery.ReactiveDiscoveryClient

The following examples show how to use org.springframework.cloud.client.discovery.ReactiveDiscoveryClient. These examples are extracted from open source projects. 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
@Test
public void shouldReturnEmptyFluxWhenServiceHasNoSubsets(
		@Client KubernetesClient kubernetesClient,
		@Server KubernetesServer kubernetesServer) {
	kubernetesServer.expect().get().withPath("/api/v1/namespaces/test/services")
			.andReturn(200,
					new ServiceListBuilder().addNewItem().withNewMetadata()
							.withName("existing-service")
							.withLabels(new HashMap<String, String>() {
								{
									put("label", "value");
								}
							}).endMetadata().endItem().build())
			.once();

	KubernetesDiscoveryProperties properties = new KubernetesDiscoveryProperties();
	ReactiveDiscoveryClient client = new KubernetesReactiveDiscoveryClient(
			kubernetesClient, properties, KubernetesClient::services);
	Flux<ServiceInstance> instances = client.getInstances("existing-service");
	StepVerifier.create(instances).expectNextCount(0).expectComplete().verify();
}
 
Example 2
@Bean
public ReactiveDiscoveryClient reactiveDiscoveryClient() {
	return new ReactiveDiscoveryClient() {
		@Override
		public String description() {
			return SERVICE_ID;
		}

		@Override
		public Flux<ServiceInstance> getInstances(String serviceId) {
			return Flux.just(instance("1host", false),
					instance("2host-secure", true));
		}

		@Override
		public Flux<String> getServices() {
			return Flux.just(SERVICE_ID);
		}
	};
}
 
Example 3
@Bean
ServiceInstanceListSupplier supplier(ConfigurableApplicationContext context,
		ReactiveDiscoveryClient discoveryClient,
		LoadBalancerProperties loadBalancerProperties,
		WebClient.Builder webClientBuilder) {
	DiscoveryClientServiceInstanceListSupplier firstDelegate = new DiscoveryClientServiceInstanceListSupplier(
			discoveryClient, context.getEnvironment());
	HealthCheckServiceInstanceListSupplier delegate = new TestHealthCheckServiceInstanceListSupplier(
			firstDelegate, loadBalancerProperties.getHealthCheck(),
			webClientBuilder.build());
	delegate.afterPropertiesSet();
	ObjectProvider<LoadBalancerCacheManager> cacheManagerProvider = context
			.getBeanProvider(LoadBalancerCacheManager.class);
	return new CachingServiceInstanceListSupplier(delegate,
			cacheManagerProvider.getIfAvailable());
}
 
Example 4
private Mono<Health> doHealthCheck() {
	// @formatter:off
	return Mono.justOrEmpty(this.discoveryClient)
			.flatMapMany(ReactiveDiscoveryClient::getServices)
			.collectList()
			.defaultIfEmpty(emptyList())
			.map(services -> {
				ReactiveDiscoveryClient client = this.discoveryClient;
				String description = (this.properties.isIncludeDescription())
						? client.description() : "";
				return Health.status(new Status("UP", description))
						.withDetail("services", services).build();
			})
			.onErrorResume(exception -> {
				this.log.error("Error", exception);
				return Mono.just(Health.down().withException(exception).build());
			});
	// @formatter:on
}
 
Example 5
@Test
public void shouldUseDefaults() {
	this.contextRunner.run((context) -> {
		ReactiveDiscoveryClient client = context
				.getBean(ReactiveDiscoveryClient.class);
		assertThat(client).isNotNull();
		assertThat(client.getOrder())
				.isEqualTo(ReactiveDiscoveryClient.DEFAULT_ORDER);
		InetUtils inet = context.getBean(InetUtils.class);
		assertThat(inet).isNotNull();
		SimpleReactiveDiscoveryProperties properties = context
				.getBean(SimpleReactiveDiscoveryProperties.class);
		assertThat(properties).isNotNull();
		assertThat(properties.getLocal().getServiceId()).isEqualTo("application");
		assertThat(properties.getLocal().getHost())
				.isEqualTo(inet.findFirstNonLoopbackHostInfo().getHostname());
		assertThat(properties.getLocal().getPort()).isEqualTo(8080);
	});
}
 
Example 6
@Test
public void shouldUseCustomConfiguration() {
	this.contextRunner.withUserConfiguration(Configuration.class)
			.withPropertyValues("spring.application.name=my-service",
					"spring.cloud.discovery.client.simple.order=1",
					"server.port=8443")
			.run((context) -> {
				ReactiveDiscoveryClient client = context
						.getBean(ReactiveDiscoveryClient.class);
				assertThat(client).isNotNull();
				assertThat(client.getOrder()).isEqualTo(1);
				InetUtils inet = context.getBean(InetUtils.class);
				assertThat(inet).isNotNull();
				SimpleReactiveDiscoveryProperties properties = context
						.getBean(SimpleReactiveDiscoveryProperties.class);
				assertThat(properties).isNotNull();
				assertThat(properties.getLocal().getServiceId())
						.isEqualTo("my-service");
				assertThat(properties.getLocal().getHost())
						.isEqualTo(inet.findFirstNonLoopbackHostInfo().getHostname());
				assertThat(properties.getLocal().getPort()).isEqualTo(8443);
			});
}
 
Example 7
@Bean
ReactiveDiscoveryClient discoveryClient() {
	return new ReactiveDiscoveryClient() {
		@Override
		public String description() {
			return "Reactive Test Discovery Client";
		}

		@Override
		public Flux<ServiceInstance> getInstances(String serviceId) {
			return Flux.empty();
		}

		@Override
		public Flux<String> getServices() {
			return Flux.empty();
		}
	};
}
 
Example 8
@Test
public void shouldUseDnsDiscovery() {
	contextRunner
			.withConfiguration(AutoConfigurations
					.of(ReactiveCommonsClientAutoConfiguration.class))
			.withPropertyValues("spring.cloud.cloudfoundry.discovery.use-dns=true",
					"spring.cloud.cloudfoundry.discovery.use-container-ip=true")
			.run(context -> {
				assertThat(context)
						.hasSingleBean(CloudFoundryReactiveHeartbeatSender.class);
				assertThat(context).hasSingleBean(ReactiveDiscoveryClient.class);
				assertThat(context).hasBean("dnsBasedReactiveDiscoveryClient");
				assertThat(context)
						.hasSingleBean(ReactiveDiscoveryClientHealthIndicator.class);
			});
}
 
Example 9
@Test
public void shouldUseAppServiceDiscovery() {
	contextRunner
			.withConfiguration(AutoConfigurations
					.of(ReactiveCommonsClientAutoConfiguration.class))
			.withPropertyValues("spring.cloud.cloudfoundry.discovery.use-dns=true",
					"spring.cloud.cloudfoundry.discovery.use-container-ip=false")
			.run(context -> {
				assertThat(context)
						.hasSingleBean(CloudFoundryReactiveHeartbeatSender.class);
				assertThat(context).hasSingleBean(ReactiveDiscoveryClient.class);
				assertThat(context).hasBean("appServiceReactiveDiscoveryClient");
				assertThat(context)
						.hasSingleBean(ReactiveDiscoveryClientHealthIndicator.class);
			});
}
 
Example 10
@Test
public void shouldUseCustomServiceDiscovery() {
	contextRunner
			.withConfiguration(AutoConfigurations
					.of(ReactiveCommonsClientAutoConfiguration.class))
			.withUserConfiguration(
					CustomCloudFoundryReactiveDiscoveryClientConfiguration.class)
			.run(context -> {
				assertThat(context)
						.hasSingleBean(CloudFoundryReactiveHeartbeatSender.class);
				assertThat(context).getBeans(ReactiveDiscoveryClient.class)
						.hasSize(2);
				assertThat(context).hasBean("nativeCloudFoundryDiscoveryClient");
				assertThat(context)
						.hasSingleBean(ReactiveDiscoveryClientHealthIndicator.class);
			});
}
 
Example 11
@Bean
public ReactiveDiscoveryClient customDiscoveryClient() {
	return new ReactiveDiscoveryClient() {
		@Override
		public String description() {
			return "Custom Reactive Discovery Client";
		}

		@Override
		public Flux<ServiceInstance> getInstances(String serviceId) {
			return Flux.empty();
		}

		@Override
		public Flux<String> getServices() {
			return Flux.empty();
		}
	};
}
 
Example 12
@Test
public void shouldWorkWithDefaults() {
	contextRunner.run(context -> {
		assertThat(context).hasSingleBean(ReactiveDiscoveryClient.class);
		assertThat(context)
				.hasSingleBean(ReactiveDiscoveryClientHealthIndicator.class);
	});
}
 
Example 13
@Test
public void shouldNotHaveDiscoveryClientWhenDiscoveryDisabled() {
	contextRunner.withPropertyValues("spring.cloud.discovery.enabled=false")
			.run(context -> {
				assertThat(context)
						.doesNotHaveBean("kubernetesReactiveDiscoveryClient");
				assertThat(context).doesNotHaveBean(ReactiveDiscoveryClient.class);
				assertThat(context).doesNotHaveBean(
						ReactiveDiscoveryClientHealthIndicator.class);
			});
}
 
Example 14
@Test
public void shouldNotHaveDiscoveryClientWhenReactiveDiscoveryDisabled() {
	contextRunner.withPropertyValues("spring.cloud.discovery.reactive.enabled=false")
			.run(context -> {
				assertThat(context)
						.doesNotHaveBean("kubernetesReactiveDiscoveryClient");
				assertThat(context).doesNotHaveBean(ReactiveDiscoveryClient.class);
				assertThat(context).doesNotHaveBean(
						ReactiveDiscoveryClientHealthIndicator.class);
			});
}
 
Example 15
@Test
public void shouldNotHaveDiscoveryClientWhenKubernetesDisabled() {
	contextRunner.withPropertyValues("spring.cloud.kubernetes.enabled=false")
			.run(context -> {
				assertThat(context)
						.doesNotHaveBean("kubernetesReactiveDiscoveryClient");
				assertThat(context).doesNotHaveBean(ReactiveDiscoveryClient.class);
				assertThat(context).doesNotHaveBean(
						ReactiveDiscoveryClientHealthIndicator.class);
			});
}
 
Example 16
@Test
public void shouldNotHaveDiscoveryClientWhenKubernetesDiscoveryDisabled() {
	contextRunner
			.withPropertyValues("spring.cloud.kubernetes.discovery.enabled=false")
			.run(context -> {
				assertThat(context)
						.doesNotHaveBean("kubernetesReactiveDiscoveryClient");
				assertThat(context).doesNotHaveBean(ReactiveDiscoveryClient.class);
				assertThat(context).doesNotHaveBean(
						ReactiveDiscoveryClientHealthIndicator.class);
			});
}
 
Example 17
@Test
public void worksWithoutWebflux() {
	contextRunner
			.withClassLoader(
					new FilteredClassLoader("org.springframework.web.reactive"))
			.run(context -> {
				assertThat(context).doesNotHaveBean(ReactiveDiscoveryClient.class);
				assertThat(context).doesNotHaveBean(
						ReactiveDiscoveryClientHealthIndicator.class);
			});
}
 
Example 18
@Test
public void worksWithoutActuator() {
	contextRunner
			.withClassLoader(
					new FilteredClassLoader("org.springframework.boot.actuate"))
			.run(context -> {
				assertThat(context).hasSingleBean(ReactiveDiscoveryClient.class);
				assertThat(context).doesNotHaveBean(
						ReactiveDiscoveryClientHealthIndicator.class);
			});
}
 
Example 19
@Test
public void verifyDefaults(@Client KubernetesClient kubernetesClient) {
	KubernetesDiscoveryProperties properties = new KubernetesDiscoveryProperties();
	ReactiveDiscoveryClient client = new KubernetesReactiveDiscoveryClient(
			kubernetesClient, properties, KubernetesClient::services);
	assertThat(client.description())
			.isEqualTo("Kubernetes Reactive Discovery Client");
	assertThat(client.getOrder()).isEqualTo(ReactiveDiscoveryClient.DEFAULT_ORDER);
}
 
Example 20
@Test
public void shouldReturnEmptyFluxForNonExistingService(
		@Client KubernetesClient kubernetesClient) {
	KubernetesDiscoveryProperties properties = new KubernetesDiscoveryProperties();
	ReactiveDiscoveryClient client = new KubernetesReactiveDiscoveryClient(
			kubernetesClient, properties, KubernetesClient::services);
	Flux<ServiceInstance> instances = client.getInstances("nonexistent-service");
	StepVerifier.create(instances).expectNextCount(0).expectComplete().verify();
}
 
Example 21
@Bean
@ConditionalOnProperty(name = "spring.cloud.gateway.discovery.locator.enabled")
public DiscoveryClientRouteDefinitionLocator discoveryClientRouteDefinitionLocator(
		ReactiveDiscoveryClient discoveryClient,
		DiscoveryLocatorProperties properties) {
	return new DiscoveryClientRouteDefinitionLocator(discoveryClient, properties);
}
 
Example 22
private void whenInstance(ReactiveDiscoveryClient discoveryClient,
		String serviceId, Map<String, String> metadata) {
	DefaultServiceInstance instance1 = new DefaultServiceInstance(
			serviceId + "8001", serviceId, "localhost", 8001, false, metadata);
	when(discoveryClient.getInstances(serviceId))
			.thenReturn(Flux.just(instance1));
}
 
Example 23
@Test
public void shouldWorkWithDefaults() {
	contextRunner.run(context -> {
		assertThat(context).hasSingleBean(ReactiveDiscoveryClient.class);
		assertThat(context).hasSingleBean(
				ReactiveDiscoveryClientHealthIndicator.class);
	});
}
 
Example 24
@Test
public void shouldNotHaveDiscoveryClientWhenDiscoveryDisabled() {
	contextRunner.withPropertyValues("spring.cloud.discovery.enabled=false")
			.run(context -> {
				assertThat(context).doesNotHaveBean("zookeeperReactiveDiscoveryClient");
				assertThat(context).doesNotHaveBean(ReactiveDiscoveryClient.class);
				assertThat(context).doesNotHaveBean(
						ReactiveDiscoveryClientHealthIndicator.class);
			});
}
 
Example 25
@Test
public void shouldNotHaveDiscoveryClientWhenReactiveDiscoveryDisabled() {
	contextRunner.withPropertyValues("spring.cloud.discovery.reactive.enabled=false")
			.run(context -> {
				assertThat(context).doesNotHaveBean("zookeeperReactiveDiscoveryClient");
				assertThat(context).doesNotHaveBean(ReactiveDiscoveryClient.class);
				assertThat(context).doesNotHaveBean(
						ReactiveDiscoveryClientHealthIndicator.class);
			});
}
 
Example 26
@Test
public void shouldNotHaveDiscoveryClientWhenCloudFoundryDiscoveryDisabled() {
	contextRunner
			.withPropertyValues("spring.cloud.zookeeper.discovery.enabled=false")
			.run(context -> {
				assertThat(context).doesNotHaveBean("zookeeperReactiveDiscoveryClient");
				assertThat(context).doesNotHaveBean(ReactiveDiscoveryClient.class);
				assertThat(context).doesNotHaveBean(
						ReactiveDiscoveryClientHealthIndicator.class);
			});
}
 
Example 27
@Test
public void worksWithoutWebflux() {
	contextRunner
			.withClassLoader(
					new FilteredClassLoader("org.springframework.web.reactive"))
			.run(context -> {
				assertThat(context).doesNotHaveBean(ReactiveDiscoveryClient.class);
				assertThat(context).doesNotHaveBean(
						ReactiveDiscoveryClientHealthIndicator.class);
			});
}
 
Example 28
@Test
public void worksWithoutActuator() {
	contextRunner
			.withClassLoader(
					new FilteredClassLoader("org.springframework.boot.actuate"))
			.run(context -> {
				assertThat(context).hasSingleBean(ReactiveDiscoveryClient.class);
				assertThat(context).doesNotHaveBean(
						ReactiveDiscoveryClientHealthIndicator.class);
			});
}
 
Example 29
@Bean
@ConditionalOnBean(ReactiveDiscoveryClient.class)
@ConditionalOnMissingBean
@ConditionalOnProperty(value = "spring.cloud.loadbalancer.configurations",
		havingValue = "default", matchIfMissing = true)
public ServiceInstanceListSupplier discoveryClientServiceInstanceListSupplier(
		ConfigurableApplicationContext context) {
	return ServiceInstanceListSupplier.builder().withDiscoveryClient()
			.withCaching().build(context);
}
 
Example 30
@Bean
@ConditionalOnBean(ReactiveDiscoveryClient.class)
@ConditionalOnMissingBean
@ConditionalOnProperty(value = "spring.cloud.loadbalancer.configurations",
		havingValue = "zone-preference")
public ServiceInstanceListSupplier zonePreferenceDiscoveryClientServiceInstanceListSupplier(
		ConfigurableApplicationContext context) {
	return ServiceInstanceListSupplier.builder().withDiscoveryClient()
			.withZonePreference().withCaching().build(context);
}