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

您的位置:首頁技術(shù)文章
文章詳情頁

Spring boot隨機端口你都不會還怎么動態(tài)擴容

瀏覽:64日期:2023-05-16 17:32:08

一般情況下每個spring boot工程啟動都有固定的端口,但是固定端口不利用服務(wù)的動態(tài)擴容,如果在一臺服務(wù)器上需要對同一個服務(wù)進行多實例部署,很容易出現(xiàn)端口沖突,那么怎么解決這個問題呢?

random隨機端口

在spring boot中,可以通過${random}來生成隨機數(shù)字,我們可以在配置文件中,這么設(shè)置端口:

server.port=${random.int(2000,8000)}

通過random.int方法,指定生成2000~8000的隨機端口。這樣每次啟動的端口都不一樣。

多次啟動,發(fā)現(xiàn)每次的端口都不一致說明配置成功。

Spring boot隨機端口你都不會還怎么動態(tài)擴容

Spring boot隨機端口你都不會還怎么動態(tài)擴容

注意事項:這里需要注意spring boot項目啟動屬性文件的加載順序,spring boot的屬性是由里向外加載,所以最外層的最后被加載,會覆蓋里層的屬性。所以如果主動在啟動命令中使用?server.port配置了項目的端口號,那么屬性文件中配置的隨機端口屬性就不會生效。

通過System.setProperty設(shè)置有效隨機端口

上面的方法雖然暫時達到了想要的效果,但是有個問題:如果生成的這個隨機端口已經(jīng)被使用了,那么項目啟動就會出現(xiàn)端口沖突。

那么,我們能否通過一個檢測機制,讓生成的隨機端口一定是一個沒有被占用的有效的隨機端口呢?

有效端口檢測原理:

通過建立socket連接,Socket socket = new Socket(Address,port);#address代表主機的IP地址,port代表端口號如果對該主機的特定端口號能建立一個socket,則說明該主機的該端口在使用。Socket socket = new Socket(Address,port);#address代表主機的IP地址,port代表端口號如果對該主機的特定端口號能建立一個socket,則說明該主機的該端口在使用。

實現(xiàn)思路:

通過在項目啟動前,獲取有效的隨機端口并通過System.setProperty將變量設(shè)置到系統(tǒng)的全局變量中,這樣項目啟動時就可以從全局變量中獲取到server.port變量的值。這里的system,系統(tǒng)指的是 JRE (runtime)system,即設(shè)置jvm運行時的全局變量。

工具類:

@Slf4jpublic class NetUtils { /** * 測試本機端口是否被使用 * @param port * @return */ public static boolean isLocalPortUsing(int port){ boolean flag = true; try { //如果該端口還在使用則返回true,否則返回false,127.0.0.1代表本機 flag = isPortUsing('127.0.0.1', port); } catch (Exception e) { } return flag; } /*** * 測試主機Host的port端口是否被使用 * @param host * @param port * @throws UnknownHostException */ public static boolean isPortUsing(String host,int port) { boolean flag = false; try { InetAddress Address = InetAddress.getByName(host); Socket socket = new Socket(Address,port); //建立一個Socket連接 flag = true; } catch (IOException e) { //log.info(e.getMessage(),e); } return flag; } //start--end是所要檢測的端口范圍 static int start=0; static int end=1024; /** * 由于本機上安裝了mysql,采用3306端口去驗證 * @param args */ public static void main(String args[]){ int testPost =3306; if(isLocalPortUsing(testPost)){System.out.println('端口 '+testPost+' 已被使用'); }else{System.out.println('端口 '+testPost+'未使用'); } }}

public class ServerPortUtils { /** * 獲取可用端口 * @return */ public static int getAvailablePort(){ int max = 65535; int min = 2000; Random random = new Random(); int port = random.nextInt(max)%(max-min +1) + min; boolean using = NetUtils.isLocalPortUsing(port); if(using){ return getAvailablePort(); }else{ return port; } }}

項目啟動前設(shè)置server.port環(huán)境變量

/** * 開始命令 */@Slf4jpublic class StartCommand { 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; } } } //沒有指定端口,則隨機生成一個可用的端口 if(!isServerPort){ int port = ServerPortUtils.getAvailablePort(); log.info('current server.port=' + port); System.setProperty('server.port',String.valueOf(port)); }else{//指定了端口,則以指定的端口為準(zhǔn) log.info('current server.port=' + serverPort.split('=')[1]); System.setProperty('server.port',serverPort.split('=')[1]); } }}

啟動類調(diào)用方法:

@SpringBootApplication@EnableUserClient@RestControllerpublic class DemoApplication { @Autowired Environment environment; public static void main(String[] args) { new StartCommand(args); SpringApplication.run(DemoApplication.class, args); }}

通過自定義PropertiesPropertySource屬性源實現(xiàn)

public class MyEnvironmentPostProcessor implements EnvironmentPostProcessor { @Override public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) { //MapPropertySource Properties properties = new Properties(); properties.put('server.port', ServerPortUtils.getAvailablePort()); System.out.println(properties.get('server.port')); PropertiesPropertySource source = new PropertiesPropertySource('myCustom', properties); environment.getPropertySources().addLast(source); //environment.getPropertySources().addAfter(); }}

通過配置在resources/META-INF/spring.factories文件中使用全名注冊

org.springframework.boot.env.EnvironmentPostProcessor=com.laowan.demo.command.MyEnvironmentPostProcessor

這樣在項目啟動后,就會將該屬性源加載到Environment中。

Spring boot隨機端口你都不會還怎么動態(tài)擴容

總結(jié)

1、為什么要設(shè)置隨機端?主要是為了解決動態(tài)擴容時出現(xiàn)端口沖突的問題。2、怎么獲取一個有效的隨機端口號3、spring boot下實現(xiàn)隨機端口的三種方式。關(guān)于方式三的自定義屬性源的實現(xiàn)方式可以多多品味,實踐一下,更好的體會屬性文件的加載順序。

到此這篇關(guān)于Spring boot隨機端口你都不會還怎么動態(tài)擴容的文章就介紹到這了,更多相關(guān)Spring boot隨機端口內(nèi)容請搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!

標(biāo)簽: Spring
相關(guān)文章:
主站蜘蛛池模板: 牡丹江市| 贺州市| 邓州市| 锡林浩特市| 伊春市| 广南县| 左贡县| 永康市| 保亭| 永康市| 宝山区| 绵竹市| 黎平县| 安化县| 上杭县| 如皋市| 上栗县| 铜川市| 肇东市| 汉中市| 沁阳市| 会宁县| 曲麻莱县| 正阳县| 西青区| 贵港市| 庆阳市| 安阳县| 平远县| 屏东市| 太保市| 荣昌县| 高密市| 旌德县| 谷城县| 阿拉善盟| 米泉市| 虎林市| 渭源县| 喀喇沁旗| 板桥市|