gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 09:54:43 27 4
gpt4 key购买 nike

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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com