简体   繁体   中英

Custom DiscoveryClient for to discover Spring Cloud Config server

I am trying to build my own DiscoveryClient that would use Docker Swarm as the service source. I got it working with Spring Cloud Gateway and Spring Cloud Loadbalancer already. However, when I tried to use it to do discovery for the configserver by setting spring.cloud.config.discovery.enabled=true I get the following error

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.springframework.cloud.client.discovery.DiscoveryClient' available: expected at l
east 1 bean which qualifies as autowire candidate. Dependency annotations: {}

I have created AutoConfigure classes as well but no luck. The project is here https://github.com/trajano/spring-cloud-demo .

When looking at the debug logs, with and without the discovery, it appears that the AutoConfiguration beans do not load up in the CONDITIONS EVALUATION REPORT specifically the ones in other libraries.

Similar to Spring cloud discovery first does not work at all but they are using Eureka, whereas I am trying to determine how to build my own DiscoveryClient.

I see your Configuration class for the DiscoveryClient has these annotations:

@ConditionalOnDiscoveryEnabled
@ConditionalOnBlockingDiscoveryEnabled
@ConditionalOnDockerSwarmDiscoveryEnabled
@AutoConfigureAfter({
    DockerSwarmDiscoveryAutoConfiguration.class
})
@AutoConfigureBefore({
    SimpleDiscoveryClientAutoConfiguration.class,
    CommonsClientAutoConfiguration.class
})

Could it be those settings are not enabled? You mention you enabled discovery, but you do not mention the other ConditionOnX configurations. If they are not enabled, the bean would not load.

There was a separate set of org.springframework.boot.autoconfigure.EnableAutoConfiguratio elements in spring.factory called org.springframework.cloud.bootstrap.BootstrapConfiguration

So I added this

org.springframework.cloud.bootstrap.BootstrapConfiguration=\
net.trajano.spring.swarm.discovery.DockerSwarmDiscoveryClientConfigServiceBootstrapConfiguration

And this class

@ConditionalOnClass(ConfigServicePropertySourceLocator.class)
@ConditionalOnProperty("spring.cloud.config.discovery.enabled")
@Configuration(proxyBeanMethods = false)
@Import({
    DockerSwarmDiscoveryClientAutoConfiguration.class,
    // this emulates
    // @EnableDiscoveryClient, the import
    // selector doesn't run before the
    // bootstrap phase
    DockerClientConfiguration.class,
    DockerSwarmDiscoveryAutoConfiguration.class,
    DockerSwarmReactiveDiscoveryClientAutoConfiguration.class,
    ReactiveCommonsClientAutoConfiguration.class
})
public class DockerSwarmDiscoveryClientConfigServiceBootstrapConfiguration {
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM