• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

SpringBoot随机端口启动的实现

springboot 搞代码 4年前 (2022-01-05) 109次浏览 已收录 0个评论
文章目录[隐藏]

本文主要介绍了SpringBoot随机端口启动的实现,文中通过示例代码介绍的非常详细,需要的朋友们下面随着小编来一起学习学习吧

一、SpringBoot随机端口

1.基础介绍

  • 随机端口可以自动找指定范围内可使用的端口,不需要在配置文件中指定固定的启动端口
  • 例如在SpringBoot中假如需要运行多个实例,则需要单独修改配置文件比较麻烦
  • 随机端口的原理就是与对应socket端口建立连接,能连接则已被使用,反之未被使用
  • 随机获取的端口校验可使用之后通过System.setProperty(“属性名称”, port);写入内存,然后就可以在配置文件中获取到
  • 如果写入的名称为server.port则不用在配置文件中指定端口,否则需要配置server.port=${属性名称}
  • 本实践基于SpringBoot普通工程,直接创建项目脚手架即可
  • 【tip】例如在SpringCloud项目中服务提供者,可以使用随机端口快速启动多个服务,而不需要单独修改配置文件再启动

2.实现步骤

创建ServerPortUtil .java端口工具类,使用socket连接指定端口,例如有以下条件

a. 指定端口范围为8000-65535
b. 识别端口是否使用,已被使用则继续随机产生
c. 如果全部端口不可使用则直接抛出错误,中断运行

 import java.net.InetAddress; import java.net.Socket; import java.util.Random; <p style="color:transparent">来源gao!%daima.com搞$代*!码网</p>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 [email protected] * @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; } } 

创建StartCommand.java命令类,调用随机端口功能获取端口信息,然后将端口信息写入运行环境中
a. 如果传入的参数包含了端口则使用指定的,否则使用自动生成

 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); } } 

在启动类启动前向环境写入端口信息

 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); } } 

在配置文件中指定端口为随机生成的端口信息

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

项目测试 正常启动项目,可以在控制台看到启动的端口信息

二、SpringBoot多实例运行

SpringBoot的多实例运行在IDEA中配置如下

然后在启动上run/debug启动即可

到此这篇关于SpringBoot随机端口启动的实现的文章就介绍到这了,更多相关SpringBoot随机端口启动内容请搜索gaodaima搞代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持gaodaima搞代码网

以上就是SpringBoot随机端口启动的实现的详细内容,更多请关注gaodaima搞代码网其它相关文章!


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:SpringBoot随机端口启动的实现

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址