Spring boot隨機(jī)端口你都不會還怎么動態(tài)擴(kuò)容
一般情況下每個spring boot工程啟動都有固定的端口,但是固定端口不利用服務(wù)的動態(tài)擴(kuò)容,如果在一臺服務(wù)器上需要對同一個服務(wù)進(jìn)行多實例部署,很容易出現(xiàn)端口沖突,那么怎么解決這個問題呢?
random隨機(jī)端口
在spring boot中,可以通過${random}來生成隨機(jī)數(shù)字,我們可以在配置文件中,這么設(shè)置端口:
server.port=${random.int(2000,8000)}
通過random.int方法,指定生成2000~8000的隨機(jī)端口。這樣每次啟動的端口都不一樣。
多次啟動,發(fā)現(xiàn)每次的端口都不一致說明配置成功。
注意事項:這里需要注意spring boot項目啟動屬性文件的加載順序,spring boot的屬性是由里向外加載,所以最外層的最后被加載,會覆蓋里層的屬性。所以如果主動在啟動命令中使用?server.port配置了項目的端口號,那么屬性文件中配置的隨機(jī)端口屬性就不會生效。
通過System.setProperty設(shè)置有效隨機(jī)端口
上面的方法雖然暫時達(dá)到了想要的效果,但是有個問題:如果生成的這個隨機(jī)端口已經(jīng)被使用了,那么項目啟動就會出現(xiàn)端口沖突。
那么,我們能否通過一個檢測機(jī)制,讓生成的隨機(jī)端口一定是一個沒有被占用的有效的隨機(jī)端口呢?
有效端口檢測原理:
通過建立socket連接,Socket socket = new Socket(Address,port);#address代表主機(jī)的IP地址,port代表端口號如果對該主機(jī)的特定端口號能建立一個socket,則說明該主機(jī)的該端口在使用。Socket socket = new Socket(Address,port);#address代表主機(jī)的IP地址,port代表端口號如果對該主機(jī)的特定端口號能建立一個socket,則說明該主機(jī)的該端口在使用。
實現(xiàn)思路:
通過在項目啟動前,獲取有效的隨機(jī)端口并通過System.setProperty將變量設(shè)置到系統(tǒng)的全局變量中,這樣項目啟動時就可以從全局變量中獲取到server.port變量的值。這里的system,系統(tǒng)指的是 JRE (runtime)system,即設(shè)置jvm運行時的全局變量。
工具類:
@Slf4jpublic class NetUtils { /** * 測試本機(jī)端口是否被使用 * @param port * @return */ public static boolean isLocalPortUsing(int port){ boolean flag = true; try { //如果該端口還在使用則返回true,否則返回false,127.0.0.1代表本機(jī) flag = isPortUsing('127.0.0.1', port); } catch (Exception e) { } return flag; } /*** * 測試主機(jī)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; /** * 由于本機(jī)上安裝了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; } } } //沒有指定端口,則隨機(jī)生成一個可用的端口 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中。
總結(jié)
1、為什么要設(shè)置隨機(jī)端?主要是為了解決動態(tài)擴(kuò)容時出現(xiàn)端口沖突的問題。2、怎么獲取一個有效的隨機(jī)端口號3、spring boot下實現(xiàn)隨機(jī)端口的三種方式。關(guān)于方式三的自定義屬性源的實現(xiàn)方式可以多多品味,實踐一下,更好的體會屬性文件的加載順序。
到此這篇關(guān)于Spring boot隨機(jī)端口你都不會還怎么動態(tài)擴(kuò)容的文章就介紹到這了,更多相關(guān)Spring boot隨機(jī)端口內(nèi)容請搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. Django ORM實現(xiàn)按天獲取數(shù)據(jù)去重求和例子2. 解決ajax的delete、put方法接收不到參數(shù)的問題方法3. asp知識整理筆記4(問答模式)4. IntelliJ IDEA 統(tǒng)一設(shè)置編碼為utf-8編碼的實現(xiàn)5. 詳解idea中web.xml默認(rèn)版本問題解決6. chat.asp聊天程序的編寫方法7. IntelliJ IDEA 2020最新激活碼(親測有效,可激活至 2089 年)8. Jsp中request的3個基礎(chǔ)實踐9. jsp EL表達(dá)式詳解10. 怎樣才能用js生成xmldom對象,并且在firefox中也實現(xiàn)xml數(shù)據(jù)島?
