使用两个端口配置 Spring Boot [英] Configure Spring Boot with two ports

查看:32
本文介绍了使用两个端口配置 Spring Boot的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用两个不同的端口在 Spring Boot 中配置一个应用程序,但我还没有做到.我的第一个近似是使用两个控制器,我在两个控制器中定义了一个 @Bean,容器为 container.setPort(8080);我的第二个近似是添加执行器依赖项并更改管理端口,但我的应用程序没有运行."地址已被使用:绑定",如何配置具有两个端口的应用程序?我想要一个端口供管理员使用,另一个端口用于咨询我的 api.

I'm trying configure an application in Spring Boot with two differents ports, but I haven't got still. My first aproximation has been with two controllers and I have defined a @Bean inside the two controller with container.setPort(8080); And my second aproximation has been add the actuator dependency and change the port of the managament, but my application don't run. "Address already in use: bind", How can I confiure an application with two ports? I want one port for admin and the other port is for consults of my api.

推荐答案

如前所述,server.portmanagement.port 以及 可以设置 management.context-path 属性以使嵌入式容器侦听不同的端口(管理相关属性以访问 Actuator 端点).

As is has been mentioned before, server.port and management.port along with management.context-path properties could be set to make the embedded container to listen on different ports (management-related properties to access Actuator endpoints).

侦听 server.portmanagement.port 以外的端口:

To listen on ports other than server.port and management.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[] {});
    }
}

应用程序.yml

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

应用程序.java

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

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

我最近在 http://tech.asimio.net/2016/12/15/Configuring-Tomcat-to-Listen-on-Multiple-ports-using-Spring-Boot.html

这篇关于使用两个端口配置 Spring Boot的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆