Because there was a requirement to close the port on the screen and open the same screen on another port I have to implement multiple ports in SpringBoot, which is a reminder. Only one application can be launched, and multiple port ports are created in the included Tomcat.
application.yml
server:
  port : 8080
  additionalPorts: 8081, 8082
EmbeddedTomcatConfiguration.java
package org.example.configuration;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import org.apache.catalina.connector.Connector;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.boot.web.server.WebServerFactoryCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.StringUtils;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
public class EmbeddedTomcatConfiguration implements WebMvcConfigurer {
    @Value("${server.port}")
    private String serverPort;
    @Value("${management.port:${server.port}}")
    private String managementPort;
    @Value("${server.additionalPorts:null}")
    private String additionalPorts;
    @Bean
    public WebServerFactoryCustomizer<TomcatServletWebServerFactory> servletContainer() {
        return factory -> {
            Connector[] additionalConnectors = additionalConnector();
            if (additionalConnectors.length > 0) {
                factory.addAdditionalTomcatConnectors(additionalConnectors);
            }
        };
    }
    private Connector[] additionalConnector() {
        if (this.additionalPorts == null || this.additionalPorts.equals("")) {
            return new Connector[0];
        }
        Set<String> defaultPorts = new HashSet<>(Arrays.asList(this.serverPort, this.managementPort));
        String[] ports = this.additionalPorts.split(",");
        List<Connector> result = new ArrayList<>();
        for (String port : ports) {
            if (StringUtils.hasText(port) && !"null".equalsIgnoreCase(port) && !defaultPorts.contains(port)) {
                Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
                connector.setScheme("http");
                connector.setPort(Integer.parseInt(port.trim()));
                result.add(connector);
            }
        }
        return result.toArray(new Connector[] {});
    }
}
Load the configuration class created above at startup.
BootApp.java
package org.example;
import org.example.configuration.EmbeddedTomcatConfiguration;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Import;
@SpringBootApplication
@Import(EmbeddedTomcatConfiguration.class)
public class BootApp {
    public static void main(String[] args) {
        SpringApplication.run(BootApp.class, args);
    }
}
Run it and check it on Git Bash. It's a simple Controller that just returns a string when you access the endpoint.
$ curl http://localhost:8080/
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100    13  100    13    0     0    149      0 --:--:-- --:--:-- --:--:--   149
Hello World!
$ curl http://localhost:8081/
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100    13  100    13    0     0    812      0 --:--:-- --:--:-- --:--:--   866
Hello World!
$ curl http://localhost:8082/
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100    13  100    13    0     0    866      0 --:--:-- --:--:-- --:--:--   866
Hello World!
        Recommended Posts