当前位置: 首页 > news >正文

SpringBoot使用随机端口启动

1.获取可用端口工具类

import java.net.InetAddress;
import java.net.Socket;
import java.util.Random;public class ServerPortUtil {private static final int MAX_PORT = 65535;private static final int MIN_PORT = 8000;public static String getAvailablePort() {Random random = new Random();// 最大尝试次数为端口范围int maxRetryCount = MAX_PORT - MIN_PORT;while (maxRetryCount > 0) {// 指定范围内随机端口,随便写的算法,根据自己需要调整int port = random.nextInt(MAX_PORT - MIN_PORT) + MIN_PORT;boolean isUsed = isLocalePortUsing(port);if (!isUsed) {return String.valueOf(port);}--maxRetryCount;}System.out.println("系统暂无端口可用,运行结束");System.exit(1);return null;}/*** 检查给定端口是否可用** @author tianxincode@163.com* @since 2020/10/14*/public static boolean isLocalePortUsing(int port) {try {// 建立一个Socket连接, 如果该端口还在使用则返回true, 否则返回false, 127.0.0.1代表本机new Socket(InetAddress.getByName("127.0.0.1"), port);return true;} catch (Exception e) {// 异常说明端口连接不上,端口能使用}return false;}
}

2.获取到可用端口将端口信息写入运行环境中

import com.codecoord.randomport.util.ServerPortUtil;
import org.springframework.util.StringUtils;public class StartCommand {/*** 端口属性名称,如果名称为server.port则在配置文件中不用指定,否则需要指定为此处配置的名称,如${auto.port}*/private static final String SERVER_PORT = "auto.port";public StartCommand(String[] args) {boolean isServerPort = false;String serverPort = "";if (args != null) {for (String arg : args) {if (StringUtils.hasText(arg) && arg.startsWith("--server.port" )) {isServerPort = true;serverPort = arg;break;}}}String port;if (isServerPort) {port = serverPort.split("=")[1];} else {port = ServerPortUtil.getAvailablePort();}System.out.println("Current project port is " + port);System.setProperty(SERVER_PORT, port);}
}

3.SpringBoot启动前调用 

import com.codecoord.randomport.config.StartCommand;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplicatio
public class SpringbootRandomPortApplication {public static void main(String[] args) {// 写入端口信息 new StartCommand(args);SpringApplication.run(SpringbootRandomPortApplication.class, args);}
}

4.修改配置文件

server:# 随机端口配置port: ${auto.port}

http://www.lryc.cn/news/193281.html

相关文章:

  • NewStarCTF2023week2-ez_sql
  • 力扣-434.字符串中的单词数
  • 【ALO-BP预测】基于蚁狮算法优化BP神经网络回归预测研究(Matlab代码实现)
  • 分布式存储系统Ceph应用详解
  • 人工智能轨道交通行业周刊-第63期(2023.10.9-10.15)
  • OJ项目——统一数据格式返回,我是如何处理的?
  • Open CV 3D Python 环境搭建
  • C#中lock 和 ReaderWriterLock 的使用总结
  • Mac下通过nvm管理node
  • 易点易动固定资产管理系统:RFID出入监控,保障固定资产安全
  • Vue封装组件并发布到npm仓库
  • python+深度学习+opencv实现植物识别算法系统 计算机竞赛
  • 基于springboot实现医院急诊平台系统项目【项目源码】
  • 【02】基础知识:React - jsx语法规则
  • C语言 —— 指针
  • 淘宝店铺所有商品数据接口,淘宝整店所有商品数据接口,淘宝店铺商品接口,淘宝API接口
  • 【Redis】Java客户端使用zset命令
  • 记录一个@Transaction注解引发的bug
  • 解决docker使用pandarallel报错OSError: [Errno 28] No space left on device
  • Javascript自定义页面复制事件
  • Nginx:反向代理(示意图+配置)
  • macbook笔记本电脑内存怎么清理才能干净流畅?
  • spark 与 mapreduce 对比
  • kafka 相关概念
  • Ubuntu下vscode配置OpenCV以及Libtorch
  • 关于共识算法Raft的常见误解
  • Python学习基础笔记七十——模块和库1
  • SystemVerilog Assertions应用指南 第一章(1.28章节 内建的系统函数)
  • 正则表达式(自用)
  • 大厂真题:【模拟】OPPO2023秋招提前批-小欧数组求和