org.springframework.cloud.client.discovery.DiscoveryClient Java Examples

The following examples show how to use org.springframework.cloud.client.discovery.DiscoveryClient. 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: DiscoveryClientHealthIndicator.java    From spring-cloud-commons with Apache License 2.0 6 votes vote down vote up
@Override
public Health health() {
	Health.Builder builder = new Health.Builder();

	if (this.discoveryInitialized.get()) {
		try {
			DiscoveryClient client = this.discoveryClient.getIfAvailable();
			List<String> services = client.getServices();
			String description = (this.properties.isIncludeDescription())
					? client.description() : "";
			builder.status(new Status("UP", description)).withDetail("services",
					services);
		}
		catch (Exception e) {
			this.log.error("Error", e);
			builder.down(e);
		}
	}
	else {
		builder.status(new Status(Status.UNKNOWN.getCode(),
				"Discovery Client not initialized"));
	}
	return builder.build();
}
 
Example #2
Source File: ZookeeperLoadBalancerConfiguration.java    From spring-cloud-zookeeper with Apache License 2.0 6 votes vote down vote up
@Bean
@ConditionalOnBean(DiscoveryClient.class)
@ConditionalOnMissingBean
public ServiceInstanceListSupplier zookeeperDiscoveryClientServiceInstanceListSupplier(
		DiscoveryClient discoveryClient, Environment env,
		ApplicationContext context,
		ZookeeperDependencies zookeeperDependencies) {
	DiscoveryClientServiceInstanceListSupplier firstDelegate = new DiscoveryClientServiceInstanceListSupplier(
			discoveryClient, env);
	ZookeeperServiceInstanceListSupplier secondDelegate = new ZookeeperServiceInstanceListSupplier(firstDelegate,
			zookeeperDependencies);
	ObjectProvider<LoadBalancerCacheManager> cacheManagerProvider = context
			.getBeanProvider(LoadBalancerCacheManager.class);
	if (cacheManagerProvider.getIfAvailable() != null) {
		return new CachingServiceInstanceListSupplier(secondDelegate,
				cacheManagerProvider.getIfAvailable());
	}
	return secondDelegate;
}
 
Example #3
Source File: ProxyRouteLocator.java    From heimdall with Apache License 2.0 5 votes vote down vote up
public ProxyRouteLocator(String servletPath, DiscoveryClient discovery, ZuulProperties properties, ZuulRouteStorage storage) {

          super(servletPath, discovery, properties);
          this.storage = storage;
          this.discovery = discovery;
          this.properties = properties;
     }
 
Example #4
Source File: InstanceDiscoveryListenerTest.java    From spring-boot-admin with Apache License 2.0 5 votes vote down vote up
@BeforeEach
public void setup() {
	this.discovery = mock(DiscoveryClient.class);
	InstanceRepository repository = new EventsourcingInstanceRepository(new InMemoryEventStore());
	this.registry = spy(new InstanceRegistry(repository, new HashingInstanceUrlIdGenerator()));
	this.listener = new InstanceDiscoveryListener(this.discovery, this.registry, repository);
}
 
Example #5
Source File: LoadBalancerClientConfiguration.java    From spring-cloud-commons with Apache License 2.0 5 votes vote down vote up
@Bean
@ConditionalOnBean(DiscoveryClient.class)
@ConditionalOnMissingBean
@ConditionalOnProperty(value = "spring.cloud.loadbalancer.configurations",
		havingValue = "health-check")
public ServiceInstanceListSupplier healthCheckDiscoveryClientServiceInstanceListSupplier(
		ConfigurableApplicationContext context) {
	return ServiceInstanceListSupplier.builder().withBlockingDiscoveryClient()
			.withHealthChecks().withCaching().build(context);
}
 
Example #6
Source File: ZookeeperDiscoveryWithDependenciesIntegrationTests.java    From spring-cloud-zookeeper with Apache License 2.0 5 votes vote down vote up
@Test
public void should_find_an_instance_via_path_when_alias_is_not_found() {
	// given:
	final DiscoveryClient discoveryClient = this.discoveryClient;
	// expect:
	await().until(() -> !discoveryClient.getInstances("nameWithoutAlias").isEmpty());
}
 
Example #7
Source File: StubRunnerSpringCloudAutoConfiguration.java    From spring-cloud-contract with Apache License 2.0 5 votes vote down vote up
@Bean
@ConditionalOnMissingBean(DiscoveryClient.class)
@ConditionalOnStubbedDiscoveryEnabled
@ConditionalOnProperty(value = "stubrunner.cloud.delegate.enabled",
		havingValue = "false", matchIfMissing = true)
public DiscoveryClient noOpStubRunnerDiscoveryClient(StubFinder stubFinder,
		StubMapperProperties stubMapperProperties,
		@Value("${spring.application.name:unknown}") String springAppName) {
	return new StubRunnerDiscoveryClient(stubFinder, stubMapperProperties,
			springAppName);
}
 
Example #8
Source File: LightminServerStandaloneDiscoveryConfiguration.java    From spring-batch-lightmin with Apache License 2.0 5 votes vote down vote up
@Bean
public LightminApplicationDiscoveryListener lightminApplicationDiscoveryListener(
        final DiscoveryClient discoveryClient,
        final DiscoveryRegistrationBean discoveryRegistrationBean,
        final HeartbeatMonitor heartbeatMonitor) {
    return new LightminApplicationDiscoveryListener(discoveryClient, discoveryRegistrationBean, heartbeatMonitor);
}
 
Example #9
Source File: DiscoveryClientVaultBootstrapConfigurationTests.java    From spring-cloud-vault with Apache License 2.0 5 votes vote down vote up
@Bean
DiscoveryClient discoveryClient() {

	DiscoveryClient mock = Mockito.mock(DiscoveryClient.class);
	when(mock.getInstances(anyString())).thenReturn(Collections.singletonList(
			new SimpleServiceInstance(URI.create("https://foo:1234"))));

	return mock;
}
 
Example #10
Source File: StubRunnerDiscoveryClient.java    From spring-cloud-contract with Apache License 2.0 5 votes vote down vote up
StubRunnerDiscoveryClient(DiscoveryClient delegate, StubFinder stubFinder,
		StubMapperProperties stubMapperProperties, String springAppName) {
	this.delegate = delegate instanceof StubRunnerDiscoveryClient
			? noOpDiscoveryClient() : delegate;
	if (log.isDebugEnabled()) {
		log.debug("Will delegate calls to discovery service [" + this.delegate
				+ "] if a stub is not found");
	}
	this.stubFinder = stubFinder;
	this.stubMapperProperties = stubMapperProperties;
}
 
Example #11
Source File: ApimlRoutingConfig.java    From api-layer with Eclipse Public License 2.0 5 votes vote down vote up
@Bean
@Autowired
public DiscoveryClientRouteLocator discoveryClientRouteLocator(DiscoveryClient discovery,
                                                               ZuulProperties zuulProperties,
                                                               ServiceRouteMapper serviceRouteMapper,
                                                               WebSocketProxyServerHandler webSocketProxyServerHandler,
                                                               PageRedirectionFilter pageRedirectionFilter) {
    List<RoutedServicesUser> routedServicesUsers = new ArrayList<>();
    routedServicesUsers.add(locationFilter());
    routedServicesUsers.add(webSocketProxyServerHandler);
    routedServicesUsers.add(pageRedirectionFilter);
    zuulProperties.setDecodeUrl(false);

    return new ApimlRouteLocator("", discovery, zuulProperties, serviceRouteMapper, routedServicesUsers);
}
 
Example #12
Source File: CloudFoundryDiscoveryClientConfigurationTest.java    From spring-cloud-cloudfoundry with Apache License 2.0 5 votes vote down vote up
@Test
public void testUseDnsTrue() {
	this.contextRunner.withUserConfiguration(CloudFoundryConfig.class)
			.withPropertyValues("spring.cloud.cloudfoundry.discovery.use-dns=true")
			.run((context) -> {
				DiscoveryClient discoveryClient = context
						.getBean(DiscoveryClient.class);
				assertThat(discoveryClient.getClass())
						.isEqualTo(CloudFoundryAppServiceDiscoveryClient.class);
			});
}
 
Example #13
Source File: DiscoveryConfiguration.java    From microservices-dashboard with Apache License 2.0 5 votes vote down vote up
@Bean
LandscapeWatcher landscapeWatcher(DiscoveryClient discoveryClient, CatalogService catalogService,
		List<ApplicationFilter> applicationFilters,
		List<ApplicationInstanceFilter> applicationInstanceFilters,
		ApplicationEventPublisher publisher) {
	return new LandscapeWatcher(discoveryClient, catalogService,
			applicationFilters, applicationInstanceFilters, publisher);
}
 
Example #14
Source File: GatewayHealthIndicatorTest.java    From api-layer with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void testStatusIsDownWhenDiscoveryIsNotAvailable() {
    DiscoveryClient discoveryClient = mock(DiscoveryClient.class);
    when(discoveryClient.getInstances(CoreService.API_CATALOG.getServiceId())).thenReturn(
        Collections.singletonList(new DefaultServiceInstance(CoreService.API_CATALOG.getServiceId(), "host", 10014, true)));
    when(discoveryClient.getInstances(CoreService.DISCOVERY.getServiceId())).thenReturn(Collections.emptyList());
    when(discoveryClient.getInstances(ZOSMF)).thenReturn(Collections.emptyList());

    GatewayHealthIndicator gatewayHealthIndicator = new GatewayHealthIndicator(discoveryClient, authConfigurationProperties);
    Health.Builder builder = new Health.Builder();
    gatewayHealthIndicator.doHealthCheck(builder);
    assertEquals(Status.DOWN, builder.build().getStatus());
}
 
Example #15
Source File: UaaSignatureVerifierClient.java    From cubeai with Apache License 2.0 5 votes vote down vote up
public UaaSignatureVerifierClient(DiscoveryClient discoveryClient, @Qualifier("loadBalancedRestTemplate") RestTemplate restTemplate,
                              OAuth2Properties oAuth2Properties) {
    this.restTemplate = restTemplate;
    this.oAuth2Properties = oAuth2Properties;
    // Load available UAA servers
    discoveryClient.getServices();
}
 
Example #16
Source File: UaaSignatureVerifierClient.java    From cubeai with Apache License 2.0 5 votes vote down vote up
public UaaSignatureVerifierClient(DiscoveryClient discoveryClient, @Qualifier("loadBalancedRestTemplate") RestTemplate restTemplate,
                              OAuth2Properties oAuth2Properties) {
    this.restTemplate = restTemplate;
    this.oAuth2Properties = oAuth2Properties;
    // Load available UAA servers
    discoveryClient.getServices();
}
 
Example #17
Source File: UaaSignatureVerifierClient.java    From cubeai with Apache License 2.0 5 votes vote down vote up
public UaaSignatureVerifierClient(DiscoveryClient discoveryClient, @Qualifier("loadBalancedRestTemplate") RestTemplate restTemplate,
                              OAuth2Properties oAuth2Properties) {
    this.restTemplate = restTemplate;
    this.oAuth2Properties = oAuth2Properties;
    // Load available UAA servers
    discoveryClient.getServices();
}
 
Example #18
Source File: CompositeDiscoveryClientOrderTest.java    From spring-cloud-commons with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldGetOrderedDiscoveryClients() {
	// when:
	List<DiscoveryClient> discoveryClients = ((CompositeDiscoveryClient) this.discoveryClient)
			.getDiscoveryClients();

	// then:
	then(discoveryClients.get(0).description()).isEqualTo(CUSTOM_DISCOVERY_CLIENT);
	then(discoveryClients.get(1).description())
			.isEqualTo(DEFAULT_ORDER_DISCOVERY_CLIENT);
	then(discoveryClients.get(2).description()).isEqualTo("Simple Discovery Client");
	then(discoveryClients.get(3).description()).isEqualTo(FOURTH_DISCOVERY_CLIENT);
}
 
Example #19
Source File: UaaSignatureVerifierClient.java    From cubeai with Apache License 2.0 5 votes vote down vote up
public UaaSignatureVerifierClient(DiscoveryClient discoveryClient, @Qualifier("loadBalancedRestTemplate") RestTemplate restTemplate,
                              OAuth2Properties oAuth2Properties) {
    this.restTemplate = restTemplate;
    this.oAuth2Properties = oAuth2Properties;
    // Load available UAA servers
    discoveryClient.getServices();
}
 
Example #20
Source File: AdminServerDiscoveryAutoConfiguration.java    From spring-boot-admin with Apache License 2.0 5 votes vote down vote up
@Bean
@ConditionalOnMissingBean
@ConfigurationProperties(prefix = "spring.boot.admin.discovery")
public InstanceDiscoveryListener instanceDiscoveryListener(ServiceInstanceConverter serviceInstanceConverter,
		DiscoveryClient discoveryClient, InstanceRegistry registry, InstanceRepository repository) {
	InstanceDiscoveryListener listener = new InstanceDiscoveryListener(discoveryClient, registry, repository);
	listener.setConverter(serviceInstanceConverter);
	return listener;
}
 
Example #21
Source File: UaaSignatureVerifierClient.java    From cubeai with Apache License 2.0 5 votes vote down vote up
public UaaSignatureVerifierClient(DiscoveryClient discoveryClient, @Qualifier("loadBalancedRestTemplate") RestTemplate restTemplate,
                              OAuth2Properties oAuth2Properties) {
    this.restTemplate = restTemplate;
    this.oAuth2Properties = oAuth2Properties;
    // Load available UAA servers
    discoveryClient.getServices();
}
 
Example #22
Source File: UaaSignatureVerifierClient.java    From cubeai with Apache License 2.0 5 votes vote down vote up
public UaaSignatureVerifierClient(DiscoveryClient discoveryClient, @Qualifier("loadBalancedRestTemplate") RestTemplate restTemplate,
                              OAuth2Properties oAuth2Properties) {
    this.restTemplate = restTemplate;
    this.oAuth2Properties = oAuth2Properties;
    // Load available UAA servers
    discoveryClient.getServices();
}
 
Example #23
Source File: DubboServiceDiscoveryAutoConfiguration.java    From spring-cloud-alibaba with Apache License 2.0 5 votes vote down vote up
public DubboServiceDiscoveryAutoConfiguration(
		DubboServiceMetadataRepository dubboServiceMetadataRepository,
		ApplicationEventPublisher applicationEventPublisher,
		DiscoveryClient discoveryClient,
		ObjectProvider<Predicate<HeartbeatEvent>> heartbeatEventChangedPredicate) {
	this.dubboServiceMetadataRepository = dubboServiceMetadataRepository;
	this.applicationEventPublisher = applicationEventPublisher;
	this.discoveryClient = discoveryClient;
	this.heartbeatEventChangedPredicate = heartbeatEventChangedPredicate;
}
 
Example #24
Source File: CloudFoundryDiscoveryClientConfigurationTest.java    From spring-cloud-cloudfoundry with Apache License 2.0 5 votes vote down vote up
@Test
public void testUseDnsFalse() {
	this.contextRunner.withUserConfiguration(CloudFoundryConfig.class)
			.withPropertyValues("spring.cloud.cloudfoundry.discovery.use-dns=false")
			.run((context) -> {
				DiscoveryClient discoveryClient = context
						.getBean(DiscoveryClient.class);
				assertThat(discoveryClient.getClass())
						.isEqualTo(CloudFoundryDiscoveryClient.class);
			});
}
 
Example #25
Source File: ZookeeperDiscoveryAutoRegistrationFalseTests.java    From spring-cloud-zookeeper with Apache License 2.0 5 votes vote down vote up
@Test
public void discovery_client_is_zookeeper() {
	// given: this.discoveryClient
	// expect:
	then(discoveryClient).isInstanceOf(CompositeDiscoveryClient.class);
	CompositeDiscoveryClient composite = (CompositeDiscoveryClient) discoveryClient;
	List<DiscoveryClient> discoveryClients = composite.getDiscoveryClients();
	DiscoveryClient first = discoveryClients.get(0);
	then(first).isInstanceOf(ZookeeperDiscoveryClient.class);
}
 
Example #26
Source File: LoadBalancerClientConfiguration.java    From spring-cloud-commons with Apache License 2.0 5 votes vote down vote up
@Bean
@ConditionalOnBean(DiscoveryClient.class)
@ConditionalOnMissingBean
@ConditionalOnProperty(value = "spring.cloud.loadbalancer.configurations",
		havingValue = "zone-preference")
public ServiceInstanceListSupplier zonePreferenceDiscoveryClientServiceInstanceListSupplier(
		ConfigurableApplicationContext context) {
	return ServiceInstanceListSupplier.builder().withBlockingDiscoveryClient()
			.withZonePreference().withCaching().build(context);
}
 
Example #27
Source File: KubernetesDiscoveryClientConfigClientBootstrapConfigurationTests.java    From spring-cloud-kubernetes with Apache License 2.0 5 votes vote down vote up
@Test
public void onWhenRequested() throws Exception {
	setup("server.port=7000", "spring.cloud.config.discovery.enabled=true",
			"spring.cloud.kubernetes.discovery.enabled:true",
			"spring.cloud.kubernetes.enabled:true", "spring.application.name:test",
			"spring.cloud.config.discovery.service-id:configserver");
	assertEquals(1, this.context.getParent()
			.getBeanNamesForType(DiscoveryClient.class).length);
	DiscoveryClient client = this.context.getParent().getBean(DiscoveryClient.class);
	verify(client, atLeast(2)).getInstances("configserver");
	ConfigClientProperties locator = this.context
			.getBean(ConfigClientProperties.class);
	assertEquals("http://fake:8888/", locator.getUri()[0]);
}
 
Example #28
Source File: CloudFoundryDiscoveryClientConfiguration.java    From spring-cloud-cloudfoundry with Apache License 2.0 5 votes vote down vote up
@Bean
@ConditionalOnProperty(
		value = "spring.cloud.cloudfoundry.discovery.use-container-ip",
		havingValue = "true")
@ConditionalOnMissingBean(DiscoveryClient.class)
public SimpleDnsBasedDiscoveryClient discoveryClient(
		ObjectProvider<ServiceIdToHostnameConverter> provider,
		CloudFoundryDiscoveryProperties properties) {
	ServiceIdToHostnameConverter converter = provider.getIfAvailable();
	return converter == null ? new SimpleDnsBasedDiscoveryClient(properties)
			: new SimpleDnsBasedDiscoveryClient(properties, converter);
}
 
Example #29
Source File: SpringCloudNamingService.java    From brpc-java with Apache License 2.0 5 votes vote down vote up
public SpringCloudNamingService(BrpcURL namingUrl) {
    this.updateInterval = Constants.DEFAULT_INTERVAL;
    namingServiceTimer = new HashedWheelTimer(new CustomThreadFactory("namingService-timer-thread"));
    discoveryClient = BrpcApplicationContextUtils.getBean("discoveryClient", DiscoveryClient.class);
    if (discoveryClient == null) {
        throw new RuntimeException("discovery client is null");
    }
}
 
Example #30
Source File: EurekaClientAutoConfigurationTests.java    From spring-cloud-netflix with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldNotHaveDiscoveryClientWhenBlockingDiscoveryDisabled() {
	new ApplicationContextRunner()
			.withConfiguration(AutoConfigurations.of(UtilAutoConfiguration.class,
					DiscoveryClientOptionalArgsConfiguration.class,
					EurekaClientAutoConfiguration.class,
					EurekaDiscoveryClientConfiguration.class))
			.withPropertyValues("spring.cloud.discovery.blocking.enabled=false")
			.run(context -> {
				assertThat(context).doesNotHaveBean(DiscoveryClient.class);
				assertThat(context)
						.doesNotHaveBean(DiscoveryClientHealthIndicator.class);
			});
}