Tuesday, February 25, 2020

SpringBott change default port

In this post I will demonstrate situation where we need to run two Spring Boot instance in single server on two different ports.

Prerequisites 
  1. You should have install java 1.8 or above.
  2. You should have Eclipse installed in your PC.
  3. Your PC should setup Maven installed and configured.
  4. Your PC should have installed MySQL and Server need to have user who have access to the configured database and user should able to log in to the database by user name/password. 


Configure existing project from GITHUB

I will use https://github.com/NirmalBalasooriya/RestApiSpringBoot Project for the demonstration of this post. Check out the project in to you IDE.

Then open project property file and update the Database username and password according to your MySQL instance.

#==== connect to mysql ======#
spring.jpa.hibernate.ddl-auto=update
spring.datasource.url=jdbc:mysql://localhost:3306/sample?useSSL=false
spring.datasource.username=root
spring.datasource.password=
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.continueOnError=true
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect

spring.jpa.properties.hibernate.show_sql=true
spring.jpa.properties.hibernate.use_sql_comments=false
spring.jpa.properties.hibernate.format_sql=true

#==== Logging configurations ======#
logging.level.root=WARN,INFO,ERROR
logging.level.com.baeldung=TRACE


Run the application

I will demonstrate it from IntelliJ as mention below. First perform maven install command by double click on "install" under maven in InelliJ as show below.



Then after successful installation you can see generated jar file in target folder. Go to that target folder from command line and run the application by executing following command in command line.


java -jar RestApiSpringBoot-0.0.1-SNAPSHOT.jar

After successful run you should be able to see following output on command line. As you can see server started on port 8080 which is default port.




Now you should be able to access following web endpoint

http://localhost:8080/findBook/1


Lets configure the application on different port and Run the application

In order to run on different port we need to add server.port configuration in to Spring Boot property file.

#==== connect to mysql ======#
spring.jpa.hibernate.ddl-auto=update
spring.datasource.url=jdbc:mysql://localhost:3306/sample?useSSL=false
spring.datasource.username=root
spring.datasource.password=
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.continueOnError=true
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
server.port = 8090

spring.jpa.properties.hibernate.show_sql=true
spring.jpa.properties.hibernate.use_sql_comments=false
spring.jpa.properties.hibernate.format_sql=true

#==== Logging configurations ======#
logging.level.root=WARN,INFO,ERROR
logging.level.com.baeldung=TRACE

Now lets run the application

Go to run debug configuration and add following command 

spring-boot:run



Then run the application  by click on Run button




Then you should be able to see following output after successful start of the applciation



Now server started on 8090 and you should be able to access same web service from following like.


http://localhost:8090/findBook/1

No comments:

Post a Comment