In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly introduces the relevant knowledge of how to achieve SpringBoot random port startup, the content is detailed and easy to understand, the operation is simple and fast, and it has a certain reference value. I believe you will gain something after reading this SpringBoot random port startup article. Let's take a look at it.
1. SpringBoot random port 1. Basic introduction
Random ports can automatically find ports that can be used within a specified range, and there is no need to specify a fixed startup port in the configuration file.
For example, in SpringBoot, if you need to run multiple instances, it is troublesome to modify the configuration file separately.
The principle of a random port is to establish a connection with the corresponding socket port. If it can be connected, it has been used, otherwise it has not been used.
The randomly obtained port verification can be used later through System.setProperty ("attribute name", port); write to memory, and then get it in the configuration file.
If the written name is server.port, you do not need to specify the port in the configuration file, otherwise you need to configure server.port=$ {attribute name}
This practice is based on SpringBoot general project. You can create project scaffolding directly.
[tip] for example, in a SpringCloud project, service providers can quickly start multiple services using random ports without having to modify the configuration file separately to start again
two。 Implementation steps
Create the ServerPortUtil .java port utility class, and use socket to connect to the specified port, for example, with the following conditions
a. The specified port range is 8000-65535
b. Identify whether the port is in use, and continue to generate it randomly if it has been used
c. If all ports are not available, an error is thrown directly and the operation is interrupted.
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 (); / / maximum number of attempts is port range int maxRetryCount = MAX_PORT-MIN_PORT While (maxRetryCount > 0) {/ / Random ports within the specified range. If you write a random algorithm, adjust 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 has no port available, end of operation"); System.exit (1); return null } / * check whether a given port is available * * @ author tianxincode@163.com * @ since 2020-10-14 * / public static boolean isLocalePortUsing (int port) {try {/ / establish a Socket connection, return true if the port is still in use, false otherwise 127.0.0.1 represents native new Socket (InetAddress.getByName ("127.0.0.1"), port) Return true;} catch (Exception e) {/ / exception indicates that the port is not connected and the port can use} return false;}}
Create a StartCommand.java command class, call the random port function to get the port information, and then write the port information to the runtime environment
a. If the parameter passed in contains the port, the specified one is used, otherwise automatically generated
Import com.codecoord.randomport.util.ServerPortUtil;import org.springframework.util.StringUtils;public class StartCommand {/ * Port attribute name. If the name is server.port, you do not need to specify it in the configuration file, otherwise you need to specify the name configured here, such as ${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);}}
Write port information to the environment before the startup class starts
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) {/ / write port information new StartCommand (args); SpringApplication.run (SpringbootRandomPortApplication.class, args);}}
Specify the port as randomly generated port information in the configuration file
Server: # Random Port configuration port: ${auto.port}
The project test starts the project normally. You can see the port information of the startup in the console.
2. SpringBoot multi-instance operation
The multi-instance running of SpringBoot is configured in IDEA as follows
Then run/debug can start it on startup.
This is the end of the article on "how to achieve random port startup in SpringBoot". Thank you for reading! I believe that everyone has a certain understanding of the knowledge of "how to achieve SpringBoot random port startup". If you want to learn more knowledge, you are welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.