pom.xml
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.4.3</version> <relativePath/> </parent> <groupId>com.yang</groupId> <artifactId>multi-port</artifactId> <version>0.0.1-SNAPSHOT</version> <name>multi-port</name> <description>multi-port Spring Boot</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> <version>3.8.1</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
application.properties
server.port=8080 server.ext-port=8081,8082
几个Java类
package com.yang.multiport; import org.apache.catalina.connector.Connector; import org.apache.commons.lang3.ArrayUtils; import org.apache.commons.lang3.StringUtils; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Import; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import java.util.Arrays; import java.util.stream.Collectors; // @Import(PortConfig.class) @SpringBootApplication public class MultiPortApplication { public static void main(String[] args) { SpringApplication.run(MultiPortApplication.class, args); } } @RestController @RequestMapping("/test") class TestController { @GetMapping("/get") public String get() { return "ok"; } } @Configuration class PortConfig { @Value("${server.ext-port}") private String extPort; /** * SpringBoot-v1 * * @return */ /*@Bean public EmbeddedServletContainerFactory servletContainer() { TomcatEmbeddedServletContainerFactory tomcatFactory = new TomcatEmbeddedServletContainerFactory(); Connector[] additionalConnectorArr = this.additionalConnector(); if (ArrayUtils.isNotEmpty(additionalConnectorArr)) { tomcatFactory.addAdditionalTomcatConnectors(additionalConnectorArr); } return tomcatFactory; }*/ /** * SpringBoot-v2 * * @return */ @Bean public TomcatServletWebServerFactory servletContainer() { TomcatServletWebServerFactory tomcatFactory = new TomcatServletWebServerFactory(); Connector[] additionalConnectorArr = this.additionalConnector(); if (ArrayUtils.isNotEmpty(additionalConnectorArr)) { tomcatFactory.addAdditionalTomcatConnectors(additionalConnectorArr); } return tomcatFactory; } /** * 根据配置的多端口创建连接器数组 * * @return */ private Connector[] additionalConnector() { if (StringUtils.isBlank(this.extPort)) { return null; } else { String[] portArr = this.extPort.split(","); Connector[] connectorArr = new Connector[portArr.length]; Arrays.stream(portArr).map(this::ctreateConnector).collect(Collectors.toList()).toArray(connectorArr); return connectorArr; } } /** * 根据端口创建连接器对象 * * @param port * @return */ private Connector ctreateConnector(String port) { // Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol"); // http1.1之BIO Connector connector = new Connector("org.apache.coyote.http11.Http11Nio2Protocol"); // http1.1之NIO2 connector.setScheme("http"); connector.setPort(Integer.valueOf(port)); return connector; } }
console输出
D:\ProgramFiles\Java\jdk1.8.0_221\bin\java.exe "-javaagent:D:\ProgramFiles\JetBrains\IntelliJIDEA2018.2.1\IntelliJ IDEA 2018.2.1\lib\idea_rt.jar=50340:D:\ProgramFiles\JetBrains\IntelliJIDEA2018.2.1\IntelliJ IDEA 2018.2.1\bin" -Dfile.encoding=UTF-8 -classpath D:\ProgramFiles\Java\jdk1.8.0_221\jre\lib\charsets.jar;D:\ProgramFiles\Java\jdk1.8.0_221\jre\lib\deploy.jar;D:\ProgramFiles\Java\jdk1.8.0_221\jre\lib\ext\access-bridge-64.jar;D:\ProgramFiles\Java\jdk1.8.0_221\jre\lib\ext\cldrdata.jar;D:\ProgramFiles\Java\jdk1.8.0_221\jre\lib\ext\dnsns.jar;D:\ProgramFiles\Java\jdk1.8.0_221\jre\lib\ext\jaccess.jar;D:\ProgramFiles\Java\jdk1.8.0_221\jre\lib\ext\jfxrt.jar;D:\ProgramFiles\Java\jdk1.8.0_221\jre\lib\ext\localedata.jar;D:\ProgramFiles\Java\jdk1.8.0_221\jre\lib\ext\nashorn.jar;D:\ProgramFiles\Java\jdk1.8.0_221\jre\lib\ext\sunec.jar;D:\ProgramFiles\Java\jdk1.8.0_221\jre\lib\ext\sunjce_provider.jar;D:\ProgramFiles\Java\jdk1.8.0_221\jre\lib\ext\sunmscapi.jar;D:\ProgramFiles\Java\jdk1.8.0_221\jre\lib\ext\sunpkcs11.jar;D:\ProgramFiles\Java\jdk1.8.0_221\jre\lib\ext\zipfs.jar;D:\ProgramFiles\Java\jdk1.8.0_221\jre\lib\javaws.jar;D:\ProgramFiles\Java\jdk1.8.0_221\jre\lib\jce.jar;D:\ProgramFiles\Java\jdk1.8.0_221\jre\lib\jfr.jar;D:\ProgramFiles\Java\jdk1.8.0_221\jre\lib\jfxswt.jar;D:\ProgramFiles\Java\jdk1.8.0_221\jre\lib\jsse.jar;D:\ProgramFiles\Java\jdk1.8.0_221\jre\lib\management-agent.jar;D:\ProgramFiles\Java\jdk1.8.0_221\jre\lib\plugin.jar;D:\ProgramFiles\Java\jdk1.8.0_221\jre\lib\resources.jar;D:\ProgramFiles\Java\jdk1.8.0_221\jre\lib\rt.jar;F:\sourceCode\target\classes;E:\appData\mavenRepo\org\springframework\boot\spring-boot-starter-web\2.4.3\spring-boot-starter-web-2.4.3.jar;E:\appData\mavenRepo\org\springframework\boot\spring-boot-starter\2.4.3\spring-boot-starter-2.4.3.jar;E:\appData\mavenRepo\org\springframework\boot\spring-boot\2.4.3\spring-boot-2.4.3.jar;E:\appData\mavenRepo\org\springframework\boot\spring-boot-autoconfigure\2.4.3\spring-boot-autoconfigure-2.4.3.jar;E:\appData\mavenRepo\org\springframework\boot\spring-boot-starter-logging\2.4.3\spring-boot-starter-logging-2.4.3.jar;E:\appData\mavenRepo\ch\qos\logback\logback-classic\1.2.3\logback-classic-1.2.3.jar;E:\appData\mavenRepo\ch\qos\logback\logback-core\1.2.3\logback-core-1.2.3.jar;E:\appData\mavenRepo\org\apache\logging\log4j\log4j-to-slf4j\2.13.3\log4j-to-slf4j-2.13.3.jar;E:\appData\mavenRepo\org\apache\logging\log4j\log4j-api\2.13.3\log4j-api-2.13.3.jar;E:\appData\mavenRepo\org\slf4j\jul-to-slf4j\1.7.30\jul-to-slf4j-1.7.30.jar;E:\appData\mavenRepo\jakarta\annotation\jakarta.annotation-api\1.3.5\jakarta.annotation-api-1.3.5.jar;E:\appData\mavenRepo\org\yaml\snakeyaml\1.27\snakeyaml-1.27.jar;E:\appData\mavenRepo\org\springframework\boot\spring-boot-starter-json\2.4.3\spring-boot-starter-json-2.4.3.jar;E:\appData\mavenRepo\com\fasterxml\jackson\core\jackson-databind\2.11.4\jackson-databind-2.11.4.jar;E:\appData\mavenRepo\com\fasterxml\jackson\core\jackson-annotations\2.11.4\jackson-annotations-2.11.4.jar;E:\appData\mavenRepo\com\fasterxml\jackson\core\jackson-core\2.11.4\jackson-core-2.11.4.jar;E:\appData\mavenRepo\com\fasterxml\jackson\datatype\jackson-datatype-jdk8\2.11.4\jackson-datatype-jdk8-2.11.4.jar;E:\appData\mavenRepo\com\fasterxml\jackson\datatype\jackson-datatype-jsr310\2.11.4\jackson-datatype-jsr310-2.11.4.jar;E:\appData\mavenRepo\com\fasterxml\jackson\module\jackson-module-parameter-names\2.11.4\jackson-module-parameter-names-2.11.4.jar;E:\appData\mavenRepo\org\springframework\boot\spring-boot-starter-tomcat\2.4.3\spring-boot-starter-tomcat-2.4.3.jar;E:\appData\mavenRepo\org\apache\tomcat\embed\tomcat-embed-core\9.0.43\tomcat-embed-core-9.0.43.jar;E:\appData\mavenRepo\org\glassfish\jakarta.el\3.0.3\jakarta.el-3.0.3.jar;E:\appData\mavenRepo\org\apache\tomcat\embed\tomcat-embed-websocket\9.0.43\tomcat-embed-websocket-9.0.43.jar;E:\appData\mavenRepo\org\springframework\spring-web\5.3.4\spring-web-5.3.4.jar;E:\appData\mavenRepo\org\springframework\spring-beans\5.3.4\spring-beans-5.3.4.jar;E:\appData\mavenRepo\org\springframework\spring-webmvc\5.3.4\spring-webmvc-5.3.4.jar;E:\appData\mavenRepo\org\springframework\spring-aop\5.3.4\spring-aop-5.3.4.jar;E:\appData\mavenRepo\org\springframework\spring-context\5.3.4\spring-context-5.3.4.jar;E:\appData\mavenRepo\org\springframework\spring-expression\5.3.4\spring-expression-5.3.4.jar;E:\appData\mavenRepo\org\apache\commons\commons-lang3\3.8.1\commons-lang3-3.8.1.jar;E:\appData\mavenRepo\org\slf4j\slf4j-api\1.7.30\slf4j-api-1.7.30.jar;E:\appData\mavenRepo\org\springframework\spring-core\5.3.4\spring-core-5.3.4.jar;E:\appData\mavenRepo\org\springframework\spring-jcl\5.3.4\spring-jcl-5.3.4.jar com.yang.multport.MultiPortApplication . ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v2.4.3) 2021-03-02 00:14:16.333 INFO 14596 --- [ main] com.yang.multport.MultiPortApplication : Starting MultiPortApplication using Java 1.8.0_221 on LAPTOP-7FSASCN3 with PID 14596 (F:\sourceCode\target\classes started by Yang in F:\sourceCode) 2021-03-02 00:14:16.340 INFO 14596 --- [ main] com.yang.multport.MultiPortApplication : No active profile set, falling back to default profiles: default 2021-03-02 00:14:19.344 INFO 14596 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http) 8081 (http) 8082 (http) 2021-03-02 00:14:19.375 INFO 14596 --- [ main] o.a.coyote.http11.Http11Nio2Protocol : Initializing ProtocolHandler ["http-nio2-8081"] 2021-03-02 00:14:19.391 INFO 14596 --- [ main] o.a.coyote.http11.Http11Nio2Protocol : Initializing ProtocolHandler ["http-nio2-8082"] 2021-03-02 00:14:19.391 INFO 14596 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat] 2021-03-02 00:14:19.391 INFO 14596 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.43] 2021-03-02 00:14:19.535 INFO 14596 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext 2021-03-02 00:14:19.535 INFO 14596 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 3047 ms 2021-03-02 00:14:19.824 INFO 14596 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor' 2021-03-02 00:14:20.079 INFO 14596 --- [ main] o.a.coyote.http11.Http11Nio2Protocol : Starting ProtocolHandler ["http-nio2-8081"] 2021-03-02 00:14:20.080 INFO 14596 --- [ main] o.a.coyote.http11.Http11Nio2Protocol : Starting ProtocolHandler ["http-nio2-8082"] 2021-03-02 00:14:20.087 INFO 14596 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) 8081 (http) 8082 (http) with context path '' 2021-03-02 00:14:20.104 INFO 14596 --- [ main] com.yang.multport.MultiPortApplication : Started MultiPortApplication in 4.942 seconds (JVM running for 6.475) 2021-03-02 00:14:50.763 INFO 14596 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring DispatcherServlet 'dispatcherServlet' 2021-03-02 00:14:50.764 INFO 14596 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Initializing Servlet 'dispatcherServlet' 2021-03-02 00:14:50.766 INFO 14596 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Completed initialization in 2 ms
SpringBoot单进程监听多端口
转载文章标签 SpringBoot 文章分类 Java 后端开发
-
nginx 同时监听多个端口转发 nginx多进程监听一个端口
上一篇文章介绍了Nginx的基本安装,也看到了最简单的欢迎页面,不禁好奇它的欢迎页面是如何展示出来的?nginx是如何处理来自我的请求的,它内部机制是什么?为什么说nginx性能好能抗住较高的并发?我们常用的tomcat跟nginx为什么是两种使用场景?我对此有佷多的疑惑,带着这些问题我们出发了,不过可能有些地方有错误,佷多理解还不够到位,敬请谅解和避坑。一、Nginx显示默认首页的过程解析上篇文
nginx 同时监听多个端口转发 不同进程 监听不同协议的同一个端口 服务器的端口号和客户进程的端口号 nginx Nginx