自定义DiscoveryClient用于发现Spring Cloud Config服务器。

问题描述 投票:0回答:2

我正试图建立自己的 DiscoveryClient 将使用Docker Swarm作为服务源。 我已经让它与Spring Cloud Gateway和Spring Cloud Loadbalancer一起工作了。 然而,当我试图用它来为Spring Cloud Gateway和Spring Cloud Loadbalancer做发现时,我发现了一个新的服务源。configserver 通过设置 spring.cloud.config.discovery.enabled=true 我得到以下错误信息

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: {}

我也创建了AutoConfigure类,但没有成功。 该项目在这里 https:/github.comtrajanospring-cloud-demo。.

当查看调试日志时,无论有没有发现,都会发现 AutoConfiguration 豆子不加载在 条件评价报告 特别是其他图书馆中的那些。

类似于 春天的云先发现根本不起作用 但他们使用的是Eureka,而我正试图确定如何构建我自己的DiscoveryClient。

java spring spring-cloud
2个回答
0
投票

我看到你的 DiscoveryClient的配置类 有这些注释。

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

会不会是这些设置没有启用? 你提到你启用了发现,但你没有提到其他的ConditionOnX配置.如果它们没有启用,bean将无法加载。


0
投票

有一组单独的 org.springframework.boot.autoconfigure.EnableAutoConfiguratio 中的元素 spring.factory 叫做 org.springframework.cloud.bootstrap.BootstrapConfiguration

所以我加了这个

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

而这个班级

@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 {
}
© www.soinside.com 2019 - 2024. All rights reserved.