在 Spring 中执行 Zuul 代理的问题(未找到 bean 类型)

Issue executing Zuul proxy in Spring (bean type not found)

Spring 引导程序:2.1.4.RELEASE

我正在尝试使用 spring-cloud-starter-netflix-zuul 在 spring 引导中设置服务代理,但是 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

此注释导致 class 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;

而在class的header可以看到评论:

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

这意味着它不是 auto-configuring。

如果删除注释 @EnableAutoConfiguration,它将不再寻找 zuul 自动配置,您可以在 application.yml(或 application.properties), 例子:

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

这里是配置zuul的文档:

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