国产成人精品亚洲777人妖,欧美日韩精品一区视频,最新亚洲国产,国产乱码精品一区二区亚洲

您的位置:首頁技術文章
文章詳情頁

SpringBoot隨機端口啟動的實現

瀏覽:68日期:2023-02-24 09:51:24
目錄一、SpringBoot隨機端口1.基礎介紹2.實現步驟二、SpringBoot多實例運行一、SpringBoot隨機端口1.基礎介紹 隨機端口可以自動找指定范圍內可使用的端口,不需要在配置文件中指定固定的啟動端口 例如在SpringBoot中假如需要運行多個實例,則需要單獨修改配置文件比較麻煩 隨機端口的原理就是與對應socket端口建立連接,能連接則已被使用,反之未被使用 隨機獲取的端口校驗可使用之后通過System.setProperty('屬性名稱', port);寫入內存,然后就可以在配置文件中獲取到 如果寫入的名稱為server.port則不用在配置文件中指定端口,否則需要配置server.port=${屬性名稱} 本實踐基于SpringBoot普通工程,直接創建項目腳手架即可 【tip】例如在SpringCloud項目中服務提供者,可以使用隨機端口快速啟動多個服務,而不需要單獨修改配置文件再啟動 2.實現步驟

創建ServerPortUtil .java端口工具類,使用socket連接指定端口,例如有以下條件

a. 指定端口范圍為8000-65535b. 識別端口是否使用,已被使用則繼續隨機產生c. 如果全部端口不可使用則直接拋出錯誤,中斷運行

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

創建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;@SpringBootApplicatiopublic class SpringbootRandomPortApplication { public static void main(String[] args) {// 寫入端口信息 new StartCommand(args);SpringApplication.run(SpringbootRandomPortApplication.class, args); }}

在配置文件中指定端口為隨機生成的端口信息

server: # 隨機端口配置 port: ${auto.port}

項目測試 正常啟動項目,可以在控制臺看到啟動的端口信息

SpringBoot隨機端口啟動的實現

SpringBoot隨機端口啟動的實現

二、SpringBoot多實例運行

SpringBoot的多實例運行在IDEA中配置如下

SpringBoot隨機端口啟動的實現

SpringBoot隨機端口啟動的實現

然后在啟動上run/debug啟動即可

到此這篇關于SpringBoot隨機端口啟動的實現的文章就介紹到這了,更多相關SpringBoot隨機端口啟動內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!

標簽: Spring
相關文章:
主站蜘蛛池模板: 黎城县| 桦甸市| 林州市| 涿鹿县| 孟村| 通城县| 崇仁县| 仁怀市| 扶余县| 大丰市| 增城市| 龙陵县| 湘潭县| 肥城市| 宝应县| 屯门区| 盐亭县| 喜德县| 南昌市| 察雅县| 石狮市| 纳雍县| 连云港市| 阿图什市| 宝丰县| 海口市| 女性| 奎屯市| 额济纳旗| 龙南县| 会同县| 新安县| 长泰县| 永泰县| 陆丰市| 牙克石市| 曲麻莱县| 香河县| 淮滨县| 阿巴嘎旗| 东方市|