Exchange your_desired_port_number with the port quantity you need to use. Save the file and restart your Spring Boot software. Your software will now run on the desired port.
Methodology 2: YAML Configuration
In the event you’re utilizing YAML configuration as an alternative of the appliance.properties file, you may configure the port in your software.yml file. Add the next strains to specify the specified port:
Exchange your_desired_port_number with the port quantity you need to use. Save the file and restart your Spring Boot software to use the adjustments.
Methodology 3: Command Line Argument
One other strategy to configuring the port is by offering a command line argument when working your Spring Boot software. As a substitute of hardcoding the port in your configuration recordsdata, you may move it as a command line argument throughout software startup. Open your terminal or command immediate and navigate to the listing containing your Spring Boot software’s JAR file. Then run the next command:
$ java -jar -Dserver.port=your_desired_port_number your_application.jar
Exchange your_desired_port_number with the port quantity you need to use and your_application.jar with the precise identify of your Spring Boot software’s JAR file. This command units the server.port system property to the desired port quantity.
Methodology 4: Surroundings Variable
In the event you want to configure the port utilizing setting variables, you are able to do so by setting the SERVER_PORT variable. The Spring Boot software will robotically decide up this variable and use it because the port. Set the SERVER_PORT setting variable to your required port quantity earlier than beginning your software. The method for setting setting variables might fluctuate relying in your working system or deployment setting.
Methodology 5: Programmatically in Code
In sure circumstances, you might need to configure the port programmatically in your Java code. To attain this, create a configuration class and annotate it with @Configuration. Then, outline a bean of kind EmbeddedServletContainerCustomizer to customise the embedded servlet container settings. Here is an instance:
import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer;
import org.springframework.boot.context.embedded.ConfigurableEmbeddedServletContainer;
@Configuration
public class PortConfiguration {
@Bean
public EmbeddedServletContainerCustomizer containerCustomizer() {
return (ConfigurableEmbeddedServletContainer container) -> {
container.setPort(your_desired_port_number);
};
}
}
Exchange your_desired_port_number with the port quantity you need to use. By implementing this customizer, you may programmatically set the port to your Spring Boot software.
Methodology 6: Configuring Port Vary
In some circumstances, you might have to configure a variety of ports as an alternative of a particular port. This may be helpful when working a number of situations of your Spring Boot software or when deploying in a clustered setting. To configure a port vary, you may modify the appliance.properties file or use any of the beforehand talked about strategies.
To configure a port vary in software.properties, use the next syntax:
server.port=your_starting_port_number-your_ending_port_number
Exchange your_starting_port_number and your_ending_port_number with the specified vary of port numbers. For instance, server.port=8080-8090 configures the appliance to run on ports starting from 8080 to 8090.
Alternatively, you may apply the identical configurations utilizing YAML, command line arguments, setting variables, or programmatically in code, as mentioned within the earlier strategies.
Methodology 7: Random Port Allocation
Spring Boot gives an choice to allocate a random obtainable port to your software. This may be helpful in eventualities the place you do not need to hardcode a particular port and as an alternative let the system assign an obtainable port dynamically. To allow random port allocation, merely omit the server.port configuration in your software.properties or software.yml file.
When the appliance begins, Spring Boot robotically assigns an obtainable port. You possibly can retrieve the dynamically allotted port programmatically utilizing the getLocalPort() technique from the EmbeddedWebApplicationContext or WebServerApplicationContext courses. This lets you log or show the assigned port throughout runtime.
import org.springframework.beans.manufacturing unit.annotation.Autowired;
import org.springframework.boot.net.context.WebServerApplicationContext;
import org.springframework.stereotype.Part;
@Part
public class PortLogger {
@Autowired
personal WebServerApplicationContext applicationContext;
public void logPort() {
int port = applicationContext.getWebServer().getPort();
System.out.println("Software is working on port: " + port);
}
}
Within the instance above, the PortLogger element logs the assigned port throughout runtime.
Methodology 8: Reverse Proxy Configuration
In sure deployment eventualities, you’ll have a reverse proxy server, reminiscent of Nginx or Apache, sitting in entrance of your Spring Boot software. On this case, you may configure the port not directly by way of the reverse proxy server.
The reverse proxy server listens on a particular port and forwards requests to the backend Spring Boot software. To configure the port, modify the configuration file of your reverse proxy server and specify the specified port there. Usually, this entails updating the server configuration block or digital host configuration.
For instance, in an Nginx configuration file, you may arrange a server block as follows:
server {
hear 80;
server_name your_domain.com;
location / {
proxy_pass http://localhost:your_desired_port_number;
proxy_set_header Host $host;
proxy_set_header X-Actual-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
Exchange your_desired_port_number with the port quantity the place your Spring Boot software is working. Save the configuration file and restart the reverse proxy server. Requests obtained on the desired port will probably be forwarded to your Spring Boot software.
Conclusion
That is all about learn how to configure port for a spring Boot software. Configuring the port for a Spring Boot software provides you management over the community port on which your software listens. On this article, we explored totally different strategies to configure the port, together with utilizing configuration recordsdata, command line arguments, setting variables, and programmatically in code. We additionally mentioned configuring port ranges and enabling random port allocation.
By choosing the suitable technique based mostly in your necessities, you may successfully customise the port configuration to your Spring Boot software. Whether or not you want a particular port, a variety of ports, or dynamic allocation, Spring Boot gives the flexibleness to adapt to your wants and guarantee clean software deployment and operation.