概括

一应用多端口通俗来讲是指启动一个springboot项目同时监听多个端口。

使用场景:

1.反向代理中间件、动态网关实现。上篇文章介绍的是SpringBoot 反向代理的实现,简单接口转发可以满足,但仅仅依靠单个端口配置路由转发无法满足多域名多站点的反向代理,因为前端静态文件CSS、JS是相对路径。而多端口就能满足需求,Nginx通过多端口,实现的一个Nginx服务代理多个Web前端站点。

2.接口安全,实现一应用 提供对外端口和对内端口。

实现

pom.xml 引入

<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId>
</dependency>

application.yml 配置

这里配置除了占用默认的8080端口,还会占用 9000-9070端口

server:followPort: 9000-9070

多Tomcat配置类

package com.terry.config;import lombok.extern.slf4j.Slf4j;
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.servlet.server.ServletWebServerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;import java.io.IOException;
import java.net.InetAddress;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;/*** Tomcat多端口占用配置* @author terry* @date 2022/7/26*/
@Configuration
@Slf4j
public class EmbeddedTomcatConfiguration {@Value("${server.followPort}")private String followPort;/*** tomcat 从节点(用来处理代理请求)* @return*/@Beanpublic ServletWebServerFactory servletContainer() {TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory();String[] ports = followPort.split("-");List<Connector> result = new ArrayList<>();for (int i = Integer.parseInt(ports[0]); i <= Integer.parseInt(ports[1]); i++) {// if (!checkPortBind(i)) {result.add(buildConnector(i));}log.info("Tomcat Follow Port Binding {} Success!", followPort);tomcat.addAdditionalTomcatConnectors(result.toArray(new Connector[] {})); // 添加httpreturn tomcat;}public Connector buildConnector(Integer port){Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");connector.setScheme("http");connector.setPort(port);return connector;}/*** 验证端口占用(效率慢)* @param port* @return*/public Boolean checkPortBind(Integer port){boolean isBind = true;try {new Socket(InetAddress.getByName("127.0.0.1"), port);} catch (IOException e) {isBind = false;}return isBind;}
}

启动成功,打印日志如下

2022-07-26 23:34:20.584  INFO 22856 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
2022-07-26 23:34:20.816  INFO 22856 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) 9000 (http) 9001 (http) 9002 (http) 9003 (http) 9004 (http) 9005 (http) 9006 (http) 9007 (http) 9008 (http) 9009 (http) 9010 (http) 9011 (http) 9012 (http) 9013 (http) 9014 (http) 9015 (http) 9016 (http) 9017 (http) 9018 (http) 9019 (http) 9020 (http) 9021 (http) 9022 (http) 9023 (http) 9024 (http) 9025 (http) 9026 (http) 9027 (http) 9028 (http) 9029 (http) 9030 (http) 9031 (http) 9032 (http) 9033 (http) 9034 (http) 9035 (http) 9036 (http) 9037 (http) 9038 (http) 9039 (http) 9040 (http) 9041 (http) 9042 (http) 9043 (http) 9044 (http) 9045 (http) 9046 (http) 9047 (http) 9048 (http) 9049 (http) 9050 (http) 9051 (http) 9052 (http) 9053 (http) 9054 (http) 9055 (http) 9056 (http) 9057 (http) 9058 (http) 9059 (http) 9060 (http) 9061 (http) 9062 (http) 9063 (http) 9064 (http) 9065 (http) 9066 (http) 9067 (http) 9068 (http) 9069 (http) 9070 (http) with context path ''
2022-07-26 23:34:20.823  INFO 22856 --- [           main] com.terry.App                            : Started App in 1.726 seconds (JVM running for 2.577)

【Spring Web教程】SpringBoot 实现一应用多端口相关推荐

  1. 【Spring Web教程】SpringBoot 整合WebSocket

    概括 WebSocket是一种服务端和网页之间的通讯协议,服务端跟网页保持着长连接,可以达到服务端主动给网页发消息的功能. 常用场景:Web聊天室.通知和紧急告警.网页消息通信.物联网通讯. Spri ...

  2. 【Spring Web教程】SpringBoot 整合SpringFox-SwaggerUI 3

    简介 SpringFox Swagger UI在2020年7月发布了 3.0版本,在Swagger2基础上减少了配置,优化如下: 使用starter依赖 无需Enable注解 减少依赖 代码配置 po ...

  3. 【Spring Web教程】SpringBoot 实现一应用多Tomcat容器

    概括 编写反向代理中间件的时候需要一个应用两个Tomcat,具体实现场景如下: 一个虚拟机进程创建两个Tomcat容器:主Tomcat.从Tomcat. 主Tomcat:处理前端应用请求.websoc ...

  4. SpringBoot | 第三十三章:Spring web Servcies集成和使用

    前言 最近有个单位内网系统需要对接统一门户,进行单点登录和待办事项对接功能.一般上政府系统都会要求做统一登录功能,这个没啥问题,反正业务系统都是做单点登录的,改下shiro相关类就好了.看了接入方案, ...

  5. Spring Web Flow实例教程

    目录: 参考文献 购物车用例 什么情况下可以使用 Spring Web Flow? 配置 Spring Web MVC 配置 Spring Web Flow 2.0 的基础 在购物车示例应用中配置 S ...

  6. 浅谈:Spring Boot原理分析,切换内置web服务器,SpringBoot监听项目(使用springboot-admin),将springboot的项目打成war包

    浅谈:Spring Boot原理分析(更多细节解释在代码注释中) 通过@EnableAutoConfiguration注解加载Springboot内置的自动初始化类(加载什么类是配置在spring.f ...

  7. Building a Restful Web Service(最好的Spring入门教程 --来自Spring官网的Guides)

    搭建一个Restful Web服务 本文档将引导你用Spring搭建一个"Hello,World!"Restful Web服务. 你要搭建的是什么? 你将搭建一个Web服务,这个服 ...

  8. Spring Cloud教程– Spring Cloud Config Server简介

    问题 SpringBoot在通过属性或YAML文件外部化配置属性方面提供了很大的灵活性. 我们还可以使用特定于配置文件的配置文件(例如application.properties , applicat ...

  9. 新一代Spring Web框架WebFlux!

    Spring WebFlux 教程:如何构建反应式 Web 应用程序 反应式系统提供了我们在高数据流世界中所需的无与伦比的响应能力和可扩展性.然而,反应式系统需要经过专门培训的工具和开发人员来实现这些 ...

最新文章

  1. android 计算运动速度,android – 计算参考真北的加速度
  2. 2018/3/18 noip模拟赛 20分
  3. 手把手带你入门Python爬虫(二、爬虫预备知识)
  4. python读json文件太大github_GitHub上最火的开源项目是啥|JSON文件实战处理
  5. Thrift初用小结
  6. 【Elasticsearch】Elasticsearch 索引 索引模板 生命周期 关系
  7. [习题]如何触发 GridView 身体里面的「子控件」的事件 (ASP.NET案例精编 / 清华大学出版社 Ch.10/11两章的补充)...
  8. OpenERP 中的on_change方法总结
  9. win98/win95
  10. Android热修复核心原理介绍
  11. HP 打印机系列打印出来有折痕,断断续续,解决方法如下:
  12. 持续造风,快手为品牌、商家提供“保姆式”服务
  13. 校验码——海明码及码距,码距
  14. 欲报从速,已有56所高校选择云创大学高质量免费直播授课!
  15. Synopsys ICC 笔记
  16. Bezier、B样条曲线曲面
  17. 如何部署JSP应用到阿里云服务器上(一)
  18. 第4版-信息系统项目管理师十大知识域及ITTO
  19. 麻省理工学院计算机硕士几年制,2020年麻省理工读研几年
  20. LabVIEW数据采集卡

热门文章

  1. Vue beforeRouteUpdate 监测路由
  2. 《计算机图形学》实验报告区域填充扫描线算法
  3. 虚拟机下 ubuntu 显示屏幕太小问题
  4. 软件工程自测题及答案
  5. 制作公司组织结构图的案例
  6. MFC 创建模态与非模态对话框
  7. java+testng+selenium的自动化测试代码
  8. 详细解释基址寻址和变址寻址区别
  9. 【对时间日期的加减操作】
  10. Mybatis之Interceptor拦截器