java - 在 Spring 中执行 Zuul 代理时出现问题(未找到 bean 类型)

标签 java spring spring-boot netflix-zuul

Spring Boot 入门:2.1.4.RELEASE

我正在尝试使用 spring-cloud-starter-netflix-zuul 在 Spring Boot 中设置服务代理,但是 jar 在启动过程中抛出以下异常

***************************
APPLICATION FAILED TO START
***************************

Description:

Field server in org.springframework.cloud.netflix.zuul.ZuulServerAutoConfiguration required a bean of type 'org.springframework.boot.autoconfigure.web.ServerProperties' that could not be found.

The injection point has the following annotations:
        - @org.springframework.beans.factory.annotation.Autowired(required=false)


Action:

Consider defining a bean of type 'org.springframework.boot.autoconfigure.web.ServerProperties' in your configuration.

下面是我的主要内容

package com.xxx.serviceproxy;

import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.boot.WebApplicationType;
import org.springframework.boot.autoconfigure.web.ServerProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Controller;

@Configuration
@ComponentScan
@EnableAutoConfiguration
@Controller
@EnableZuulProxy
@SpringBootApplication
public class ServiceProxyApplication {

    public static void main(String[] args) {
        new SpringApplicationBuilder(ServiceProxyApplication.class).web(WebApplicationType.NONE).run(args);
    }

}

最佳答案

发生这种情况是因为您添加了注释@EnableAutoConfiguration

此注释会导致搜索 ZuulServerAutoConfiguration 类。

您可以在这里看到:

https://github.com/spring-cloud/spring-cloud-netflix/blob/master/spring-cloud-netflix-zuul/src/main/java/org/springframework/cloud/netflix/zuul/ZuulServerAutoConfiguration.java

其中有一个自动连接的

@Autowired
protected ServerProperties server;

在类的标题中您可以看到注释:

// Make sure to get the ServerProperties from the same place as a normal web app would
// FIXME @Import(ServerPropertiesAutoConfiguration.class)
public class ZuulServerAutoConfiguration {

这意味着它不会自动配置。

如果删除注释@EnableAutoConfiguration,它将不再寻找zuul自动配置,你可以在application.yml(或application.properties)中配置zuul路径和其他功能,例如:

 zuul:
  routes:
    users:
      path: /myusers/**
      serviceId: users_service

这里是配置zuul的文档:

https://cloud.spring.io/spring-cloud-netflix/multi/multi__router_and_filter_zuul.html

关于java - 在 Spring 中执行 Zuul 代理时出现问题(未找到 bean 类型),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56083317/

相关文章:

java - Spring JPA 一个实体中的多个多对一关系

java - 在测试用例中定义 spring active profile

java - 关于重载的说明

java - 从 ANT 运行 junitreport 时出现 OutOfMemoryError

java - 使用 while 循环返回整数的第一个和最后一个数字

java - Spring Boot 1.5禁用oauth2安全

java - Spring WebClient 从 Json 请求中过滤 Null

java - PrimeFaces 组件不显示

java - spring如何配置CommonsPool2TargetSource?

Spring-Data-Cassandra SchemaAction 不工作