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

时间:2019-05-10 18:50:02

标签: java spring spring-boot netflix-zuul

Spring Boot Starter:2.1.4。发行

我正在尝试使用spring-cloud-starter-netflix-zuul在春季启动时设置服务代理,但是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);
    }

}

1 个答案:

答案 0 :(得分:1)

之所以会发生这种情况,是因为您要添加注释 @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路径和其他功能,例如:< / p>

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

这里是配置zuul的文档:

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