gpt4 book ai didi

不同端口上的spring boot 2.1.0管理服务器端口

转载 作者:行者123 更新时间:2023-12-04 02:55:31 25 4
gpt4 key购买 nike

在我的 spring boot 2.0 应用程序中,我的主应用程序监听端口 1234,我想让管理服务器在 1235 上运行。

所以在我的配置文件中,我设置:

management.server.port=1235

我的服务器无法启动,出现以下错误:

[ERROR] 2018-11-14 05:20:14.958 [main] SpringApplication - Application run failed org.springframework.beans.FatalBeanException: ServletWebServerFactory implementation com.my.MyApplication$2 cannot be instantiated. To allow a separate management port to be used, a top-level class or static inner class should be used instead at org.springframework.boot.actuate.autoconfigure.web.servlet.ServletManagementContextFactory.determineServletWebServerFactoryClass(ServletManagementContextFactory.java:77) ~[spring-boot-actuator-autoconfigure-2.1.0.RELEASE.jar:2.1.0.RELEASE] at org.springframework.boot.actuate.autoconfigure.web.servlet.ServletManagementContextFactory.registerServletWebServerFactory(ServletManagementContextFactory.java:64) ~[spring-boot-actuator-autoconfigure-2.1.0.RELEASE.jar:2.1.0.RELEASE] at org.springframework.boot.actuate.autoconfigure.web.servlet.ServletManagementContextFactory.createManagementContext(ServletManagementContextFactory.java:52) ~[spring-boot-actuator-autoconfigure-2.1.0.RELEASE.jar:2.1.0.RELEASE] at org.springframework.boot.actuate.autoconfigure.web.server.ManagementContextAutoConfiguration$DifferentManagementContextConfiguration.afterSingletonsInstantiated(ManagementContextAutoConfiguration.java:143) ~[spring-boot-actuator-autoconfigure-2.1.0.RELEASE.jar:2.1.0.RELEASE] at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:863) ~[spring-beans-5.1.2.RELEASE.jar:5.1.2.RELEASE] at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:863) ~[spring-context-5.1.2.RELEASE.jar:5.1.2.RELEASE] at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:546) ~[spring-context-5.1.2.RELEASE.jar:5.1.2.RELEASE] at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:140) ~[spring-boot-2.1.0.RELEASE.jar:2.1.0.RELEASE] at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:775) [spring-boot-2.1.0.RELEASE.jar:2.1.0.RELEASE] at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397) [spring-boot-2.1.0.RELEASE.jar:2.1.0.RELEASE] at org.springframework.boot.SpringApplication.run(SpringApplication.java:316) [spring-boot-2.1.0.RELEASE.jar:2.1.0.RELEASE] at org.springframework.boot.SpringApplication.run(SpringApplication.java:1260) [spring-boot-2.1.0.RELEASE.jar:2.1.0.RELEASE] at org.springframework.boot.SpringApplication.run(SpringApplication.java:1248) [spring-boot-2.1.0.RELEASE.jar:2.1.0.RELEASE] at com.my.MyApplication.main(EmsApplication.java:51) [main/:?]

如果我删除它:

@Bean
public TomcatServletWebServerFactory containerFactory() {
return new TomcatServletWebServerFactory() {
@Override
protected void customizeConnector(Connector connector) {
int maxSize = 50000000;
super.customizeConnector(connector);
connector.setMaxPostSize(maxSize);
connector.setMaxSavePostSize(maxSize);
if (connector.getProtocolHandler() instanceof AbstractHttp11Protocol) {

((AbstractHttp11Protocol <?>) connector.getProtocolHandler()).setMaxSwallowSize(maxSize);
}
}
t
};

}

然后就可以了。

如何解决这个问题?

谢谢!

最佳答案

您的 TomcatWebServerFactory 子类是一个匿名内部类。它需要是静态内部类或顶级类,以便它可以被实例化:

@Bean
public TomcatServletWebServerFactory containerFactory() {
return new CustomTomcatServletWebServerFactory();
}

static final class CustomTomcatServletWebServerFactory
extends TomcatServletWebServerFactory {

@Override
protected void customizeConnector(Connector connector) {
int maxSize = 50000000;
super.customizeConnector(connector);
connector.setMaxPostSize(maxSize);
connector.setMaxSavePostSize(maxSize);
if (connector.getProtocolHandler() instanceof AbstractHttp11Protocol) {
((AbstractHttp11Protocol<?>) connector.getProtocolHandler())
.setMaxSwallowSize(maxSize);
}
}

}

或者,您可以使用自定义程序而不是对 TomcatServletWebServerFactory 进行子类化:

@Bean
public WebServerFactoryCustomizer<TomcatServletWebServerFactory> tomcatCustomizer() {
return (tomcat) -> tomcat.addConnectorCustomizers((connector) -> {
int maxSize = 50000000;
connector.setMaxPostSize(maxSize);
connector.setMaxSavePostSize(maxSize);
if (connector.getProtocolHandler() instanceof AbstractHttp11Protocol) {

((AbstractHttp11Protocol<?>) connector.getProtocolHandler())
.setMaxSwallowSize(maxSize);
}
});
}

关于不同端口上的spring boot 2.1.0管理服务器端口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53298008/

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