当前位置: 首页 > 知识库问答 >
问题:

如何在Spring-Cloud中使用带有Zuul和Sidecar的ConsullDiscoveryClient

刘嘉木
2023-03-14

我现在正尝试用以下代码连接API网关微服务以进行咨询:

@SpringBootApplication
@EnableSidecar
@EnableDiscoveryClient
public class GatewayApplication {

    @Autowired
    @Qualifier("consulDiscoveryClient")
    DiscoveryClient discovery;

    public static void main(String[] args) {
        SpringApplication.run(GatewayApplication.class, args);
    }
}

这将导致以下异常:

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'gatewayApplication.MyZuulProxyConfiguration': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private org.springframework.cloud.client.discovery.DiscoveryClient org.springframework.cloud.netflix.zuul.ZuulProxyConfiguration.discovery; nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type [org.springframework.cloud.client.discovery.DiscoveryClient] is defined: expected single matching bean but found 2: consulDiscoveryClient,discoveryClient
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:334)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1214)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:543)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:305)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:301)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:196)
    at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:368)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1123)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1018)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:510)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:305)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:301)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:201)
    at org.springframework.boot.context.embedded.ServletContextInitializerBeans.getOrderedBeansOfType(ServletContextInitializerBeans.java:209)
    at org.springframework.boot.context.embedded.ServletContextInitializerBeans.addServletContextInitializerBeans(ServletContextInitializerBeans.java:85)
    at org.springframework.boot.context.embedded.ServletContextInitializerBeans.<init>(ServletContextInitializerBeans.java:73)
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.getServletContextInitializerBeans(EmbeddedWebApplicationContext.java:234)
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.selfInitialize(EmbeddedWebApplicationContext.java:221)
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.access$000(EmbeddedWebApplicationContext.java:84)
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext$1.onStartup(EmbeddedWebApplicationContext.java:206)
    at org.springframework.boot.context.embedded.tomcat.TomcatStarter.onStartup(TomcatStarter.java:54)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5156)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1408)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1398)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    ... 1 more
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private org.springframework.cloud.client.discovery.DiscoveryClient org.springframework.cloud.netflix.zuul.ZuulProxyConfiguration.discovery; nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type [org.springframework.cloud.client.discovery.DiscoveryClient] is defined: expected single matching bean but found 2: consulDiscoveryClient,discoveryClient
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:571)
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:331)
    ... 32 more
Caused by: org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type [org.springframework.cloud.client.discovery.DiscoveryClient] is defined: expected single matching bean but found 2: consulDiscoveryClient,discoveryClient
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1079)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:967)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:543)
    ... 34 more

此服务的pom.xml是:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <artifactId>api-gateway-microservice</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>

    <parent>
        <groupId>nl.quby.nxt</groupId>
        <artifactId>springCloudSpike</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-zuul</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-netflix-sidecar</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-consul-all</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

</project>

bootstrap.yml:

spring:
  application:
    name: gateway
  cloud:
    consul:
      host: consul
      port: 8500
      config:
        enabled: true

encrypt:
  failOnError: false

application.yml:

server:
  port: 10000

endpoints:
  restart:
    enabled: true
  shutdown:
    enabled: true
  health:
    sensitive: false

谁能告诉我如何使用Spring cloud和Consul作为发现和配置服务来创建aan API-Gateway?

共有1个答案

松国兴
2023-03-14

您应该使用@enalbezuulproxy,sidecar用于非java服务,用作服务发现客户端。@enalbezuulproxy还将为您启用发现客户端。

另外,您的错误是清楚地表明,您试图按接口autowireDiscoveryClient,并且在您的上下文中已经有两个接口:一个spring-boot autoconfigures和一个您创建的。

您不应该自己创建客户机,因为如果类路径上有适当的依赖项,自动配置将为您创建客户机。

 类似资料:
  • 我的项目中有一个常见的Spring云架构:用于服务发现的eureka、作为反向代理的zuul和一些微服务。我希望能够通过编程从zuul访问微服务。由于zuul由Ribbon支持,我只想自动连接一个,但显然这不起作用。下面是我的小测试设置。 Zulgateway应用程序。爪哇: 有些ervice.java: 下面是stackTrack的相关部分: 我是否需要配置其他东西来获得正确的?我还尝试用注释M

  • 我的微服务应用程序基于spring cloud:在两个微服务(service-a和service-b)前面配置了一个zuul网关。 我的一个API需要该service-a请求service-b;为此我使用。 Zuul向服务发送标头,以便它们正确重写HATEOAS链接(当服务配置为时)。 我的问题是,这些服务使用依赖于Hystrix的外文进行通信

  • 问题内容: 我一直在使用Zuul作为边缘服务和API网关。最近,我注意到Spring Cloud Platform发布了Spring Cloud Gateway。这两个网关有什么区别?为什么Zuul不扩展为支持SC-Gateway中的功能?一个新图书馆的总体驱动因素是什么?什么时候应该使用? 问题答案: 我是Spring Cloud Gateway的作者。Zuul使用阻塞API在Servlet 2

  • 我正在评估使用Spring Boot和Spring Cloud(Zuul和Eureka)构建微服务,这些微服务运行在单独的docker容器中,部署在单独的Amazon EC2实例中。 我可以使用boot2docker在我的Mac上本地运行(即不在Amazon/EC2),但当部署在EC2上时,Zuul找不到该服务,并报告一个“转发错误”(状态500)。 在EC2中,一切都被配置为在端口80上运行(只

  • 现在我要介绍旁观者和图集。我似乎无法正确配置这个。如果我在Zuul服务上启用Atlas,则Atlas客户端的RestTemplate将失败,因为Ribbon试图包装它,而Ribbon不知道“Atlas”。如果我为Atlas添加功能区客户端配置,那么我的API网关将调用Break。有没有办法把地图集路线排除在彩带包裹之外?我错过了什么?见下文: 我定义了一个指向localhost主机名“atlas”

  • 我正在尝试为我的Spring Cloud应用程序启用SSL。现在它由少数服务组成,尤里卡和祖尔(没有丝带或Feighn)。我发现很少提示如何为Eureka启用SSL,但无法使其工作。在Zuul方面,除了以下信息之外,我找不到任何信息:Zuul SSL支持,它有效地禁止使用随机端口:(server.port=0)。我的应用程序的application.yml: Eureka服务器applicatio