【问题标题】:Spring boot 2.1.0 management server port on different portSpring boot 2.1.0管理服务器端口在不同端口
【发布时间】:2019-04-17 06:57:29
【问题描述】:

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

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

management.server.port=1235

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

[ERROR] 2018-11-14 05:20:14.958 [main] SpringApplication - 应用程序运行失败 org.springframework.beans.FatalBeanException:ServletWebServerFactory 实现 com.my.MyApplication$2 无法实例化。要允许使用单独的管理端口,应使用顶级类或静态内部类 在 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] 在 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] 在 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] 在 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 ] 在 org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:863) ~[spring-beans-5.1.2.RELEASE.jar:5.1.2.RELEASE] 在 org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:863) ~[spring-context-5.1.2.RELEASE.jar:5.1.2.RELEASE] 在 org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:546) ~[spring-context-5.1.2.RELEASE.jar:5.1.2.RELEASE] 在 org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:140) ~[spring-boot-2.1.0.RELEASE.jar:2.1.0.RELEASE] 在 org.springframework.boot.SpringApplication.refresh(SpringApplication.java:775) [spring-boot-2.1.0.RELEASE.jar:2.1.0.RELEASE] 在 org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397) [spring-boot-2.1.0.RELEASE.jar:2.1.0.RELEASE] 在 org.springframework.boot.SpringApplication.run(SpringApplication.java:316) [spring-boot-2.1.0.RELEASE.jar:2.1.0.RELEASE] 在 org.springframework.boot.SpringApplication.run(SpringApplication.java:1260) [spring-boot-2.1.0.RELEASE.jar:2.1.0.RELEASE] 在 org.springframework.boot.SpringApplication.run(SpringApplication.java:1248) [spring-boot-2.1.0.RELEASE.jar:2.1.0.RELEASE] 在 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
    };

}

然后就可以了。

如何解决这个问题?

谢谢!

【问题讨论】:

  • 删除这个问题就不存在了:
  • ServletWebServerFactory implementation com.my.MyApplication$2 那个类是什么样子的?
  • @DarrenForsythe 即TomcatServletWebServerFactory
  • 您是否尝试过定义 bean TomcatServletWebServerFactoryCustomizer 并调用 addConnectorCustomizers 而不是 TomcatServletWebServerFactory

标签: spring spring-boot spring-boot-actuator


【解决方案1】:

您的 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);
        }
    });
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-09-17
    • 2020-05-13
    • 2019-02-20
    • 2016-04-24
    • 1970-01-01
    • 2021-01-12
    • 1970-01-01
    相关资源
    最近更新 更多