• SpringBoot使用随机端口启动


    1.获取可用端口工具类

    1. import java.net.InetAddress;
    2. import java.net.Socket;
    3. import java.util.Random;
    4. public class ServerPortUtil {
    5. private static final int MAX_PORT = 65535;
    6. private static final int MIN_PORT = 8000;
    7. public static String getAvailablePort() {
    8. Random random = new Random();
    9. // 最大尝试次数为端口范围
    10. int maxRetryCount = MAX_PORT - MIN_PORT;
    11. while (maxRetryCount > 0) {
    12. // 指定范围内随机端口,随便写的算法,根据自己需要调整
    13. int port = random.nextInt(MAX_PORT - MIN_PORT) + MIN_PORT;
    14. boolean isUsed = isLocalePortUsing(port);
    15. if (!isUsed) {
    16. return String.valueOf(port);
    17. }
    18. --maxRetryCount;
    19. }
    20. System.out.println("系统暂无端口可用,运行结束");
    21. System.exit(1);
    22. return null;
    23. }
    24. /**
    25. * 检查给定端口是否可用
    26. *
    27. * @author tianxincode@163.com
    28. * @since 2020/10/14
    29. */
    30. public static boolean isLocalePortUsing(int port) {
    31. try {
    32. // 建立一个Socket连接, 如果该端口还在使用则返回true, 否则返回false, 127.0.0.1代表本机
    33. new Socket(InetAddress.getByName("127.0.0.1"), port);
    34. return true;
    35. } catch (Exception e) {
    36. // 异常说明端口连接不上,端口能使用
    37. }
    38. return false;
    39. }
    40. }

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

    1. import com.codecoord.randomport.util.ServerPortUtil;
    2. import org.springframework.util.StringUtils;
    3. public class StartCommand {
    4. /**
    5. * 端口属性名称,如果名称为server.port则在配置文件中不用指定,否则需要指定为此处配置的名称,如${auto.port}
    6. */
    7. private static final String SERVER_PORT = "auto.port";
    8. public StartCommand(String[] args) {
    9. boolean isServerPort = false;
    10. String serverPort = "";
    11. if (args != null) {
    12. for (String arg : args) {
    13. if (StringUtils.hasText(arg) && arg.startsWith("--server.port" )) {
    14. isServerPort = true;
    15. serverPort = arg;
    16. break;
    17. }
    18. }
    19. }
    20. String port;
    21. if (isServerPort) {
    22. port = serverPort.split("=")[1];
    23. } else {
    24. port = ServerPortUtil.getAvailablePort();
    25. }
    26. System.out.println("Current project port is " + port);
    27. System.setProperty(SERVER_PORT, port);
    28. }
    29. }

    3.SpringBoot启动前调用 

    1. import com.codecoord.randomport.config.StartCommand;
    2. import org.springframework.boot.SpringApplication;
    3. import org.springframework.boot.autoconfigure.SpringBootApplication;
    4. @SpringBootApplicatio
    5. public class SpringbootRandomPortApplication {
    6. public static void main(String[] args) {
    7. // 写入端口信息
    8. new StartCommand(args);
    9. SpringApplication.run(SpringbootRandomPortApplication.class, args);
    10. }
    11. }

    4.修改配置文件

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

  • 相关阅读:
    自定义实现hashmap-python
    一篇文章带你彻底搞懂wait/notify
    非零基础自学Java (老师:韩顺平) 第8章 面向对象编程(中级部分) 8.6 面向对象编程三大特征 && 8.7 快速入门案例
    MySQL高级七:MySQL服务器的逻辑框架
    协程Part1-boost.Coroutine.md
    Linux主机连接腾讯云服务器
    一篇理解网络分层原理
    CSS实现空心的“尖角”
    Java Spring Cloud XV 之 Redis I
    关于进制的简单介绍
  • 原文地址:https://blog.csdn.net/guo__hang/article/details/133748450