spring 使用两个端口配置Sping Boot

ulmd4ohb  于 2023-08-02  发布在  Spring
关注(0)|答案(5)|浏览(171)

我尝试在Spring Boot中配置一个应用程序,使用两个不同的端口,但我没有得到。我的第一个近似方法是使用两个控制器,我在两个控制器中定义了一个@Bean,使用container.setPort(8080);。我的第二个近似方法是添加执行器依赖关系并更改管理端口,但我的应用程序无法运行。它显示:

Address already in use: bind

字符串
如何配置具有两个端口的应用程序?我想要一个端口管理和其他端口是我的API咨询。

0vvn1miw

0vvn1miw1#

如前所述,可以设置server.portmanagement.port沿着management.context-path属性,以使嵌入式容器监听不同的端口(访问Actuator端点的管理相关属性)。
要侦听server.portmanagement.port以外的端口,请执行以下操作:

@Configuration
public class EmbeddedTomcatConfiguration {

    @Value("${server.additionalPorts}")
    private String additionalPorts;

    @Bean
    public EmbeddedServletContainerFactory servletContainer() {
        TomcatEmbeddedServletContainerFactory tomcat = new TomcatEmbeddedServletContainerFactory();
        Connector[] additionalConnectors = this.additionalConnector();
        if (additionalConnectors != null && additionalConnectors.length > 0) {
            tomcat.addAdditionalTomcatConnectors(additionalConnectors);
        }
        return tomcat;
    }

    private Connector[] additionalConnector() {
        if (StringUtils.isBlank(this.additionalPorts)) {
            return null;
        }
        String[] ports = this.additionalPorts.split(",");
        List<Connector> result = new ArrayList<>();
        for (String port : ports) {
            Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
            connector.setScheme("http");
            connector.setPort(Integer.valueOf(port));
            result.add(connector);
        }
        return result.toArray(new Connector[] {});
    }
}

字符串
application.yml

server:
  port: ${appPort:8800}
  additionalPorts: 8881,8882


Application.java

@SpringBootApplication
@ComponentScan(...)
@Import(EmbeddedTomcatConfiguration.class)
public Application {

    public static void main(String[] args) {
        SpringApplication.run(Application .class, args);
    }
}


我最近在博客上写了这个主题:

w41d8nur

w41d8nur2#

由于springboot 2,ootero solution中提到的EmbeddedServletContainerFactory不再可用,因此您应该根据上下文使用TomcatServletWebServerFactoryTomcatReactiveWebServerFactory
除了出厂注射外,解决方案保持不变:

@Bean
public TomcatServletWebServerFactory servletContainer() {
    TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory();
    Connector[] additionalConnectors = this.additionalConnector();
    if (additionalConnectors != null && additionalConnectors.length > 0) {
        tomcat.addAdditionalTomcatConnectors(additionalConnectors);
    }
    return tomcat;
}

字符串

3zwjbxry

3zwjbxry3#

要更改执行器管理端口,可以使用属性

management.port=8081

字符串
查看完整的属性列表here

**更新:**在本例中,Actuator会再创建一个Embedded Tomcat(servlet容器)示例。参见herehere

unguejic

unguejic4#

要在单个项目中运行2个或更多应用程序或更改默认端口,可以执行如下操作

@SpringBootApplication
public class NewApplication {
     public static void main(String[] args) {
         SpringApplication app = new SpringApplication(NewApplication .class);
         app.setDefaultProperties(Collections.singletonMap("server.port", "8083"));
         app.run(args);
     }
}

字符串

6pp0gazn

6pp0gazn5#

如果只需要打开一个额外端口,则以下操作就足够了(Kotlin):

@Configuration
class AdditionalEndpointConfig {

    @Bean
    @ConditionalOnProperty(PORT_PROPERTY)
    fun tomcatServletWebServerFactory(@Value("\${$PORT_PROPERTY}") additionalPort: Int) =
        TomcatServletWebServerFactory().apply {
            addAdditionalTomcatConnectors(
                Connector("org.apache.coyote.http11.Http11NioProtocol").apply {
                    scheme = "http"
                    port = additionalPort
                })
        }

    companion object {
        const val PORT_PROPERTY = "server.additional.port"
    }
}

字符串

相关问题