Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Web server failed to start. Port 8080 was already in use. Spring Boot microservice

Tags:

I am trying to call webAPI from gradle project.

My build.gradle is as following.

plugins {
    id 'org.springframework.boot' version '2.1.4.RELEASE'
    id 'java'
}

apply plugin: 'io.spring.dependency-management'

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter'
    runtimeOnly 'org.springframework.boot:spring-boot-devtools'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
    compile 'org.springframework.boot:spring-boot-starter-webflux'
    compile 'org.projectreactor:reactor-spring:1.0.1.RELEASE'
}

If I remove following dependency

compile 'org.springframework.boot:spring-boot-starter-webflux'

It works, but if I add it back. it gives error as

Web server failed to start. Port 8080 was already in use.

So, how do I fix this, so that I can use webclient? Because application is not web application which requires port to run. it is a sort of microservice.

I just want to use WebClient of Spring Boot. How do i use it without converting my application into web application.

like image 234
Anonymous Creator Avatar asked Jun 07 '19 19:06

Anonymous Creator


People also ask

How do you stop the port which is already in use in spring boot?

1) On the top right corner of your console, there is a red button, to stop the spring boot application which is already running on this port just click on the red button to terminate. 2) If the red button is not activated you need to right click on the console and select terminate/disconnect all. Hope this helps.

How do I fix the start of Tomcat failed the server port 8080 is already in use?

Answers 10 : of Deployment error:Starting of Tomcat failed, the server port 8080 is already in use. Change your Tomcat port address to 8084 and Shut Down Port to 8025. This will resolve your problem.


3 Answers

If on windows and your getting this every time you run the application you need to keep doing:

> netstat -ano | findstr *<port used>*    TCP    0.0.0.0:*<port used>*  0.0.0.0:0              LISTENING       *<pid>*   TCP    [::]:*<port used>*     [::]:0                 LISTENING       *<pid>*  > taskkill /F /PID *<pid>* SUCCESS: The process with PID *<pid>* has been terminated. 

If netstat above includes something like this;

TCP    [zzzz:e2ce:44xx:1:axx6:dxxf:xxx:xxxx]:540yy [zzzz:e2ce:44xx:1:axx6:dxxf:xxx:xxxx]:*<port used>* TIME_WAIT 0 

Then you can either wait for a little while or reconfigure to use another port.

I suppose we could write some code to randomly generate and check if a port is free when the application runs. Though this will have diminishing returns as they start to get used up. On the other hand could add a resource clean up code that does what we have above once the application stops.

like image 109
Alan Carlyle Avatar answered Oct 06 '22 05:10

Alan Carlyle


If you don't want the embedded server to start, just set the following property in you application.properties (or .yml):

spring.main.web-application-type=none

If your classpath contains the necessary bits to start a web server, Spring Boot will automatically start it. To disable this behaviour configure the WebApplicationType in your application.properties

Source: https://docs.spring.io/spring-boot/docs/current/reference/html/howto-embedded-web-servers.html


If you application really is a Web application, then you can easily change the port using the server.port property (in your application's .properties/.yaml file, as a command line argument at startup, etc).

like image 23
Gustavo Passini Avatar answered Oct 06 '22 06:10

Gustavo Passini


You can change the default port of your application in application.properties by adding the following line:

server.port = 8090

like image 27
jayesh Avatar answered Oct 06 '22 06:10

jayesh