springboot does not occupy the port to start

springboot does not occupy the port to start

Non-web project

In the service architecture, some springboot projects simply serve as services and do not provide web services

No need to rely on this time

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

But if you start springboot, it will automatically shut down after startup, which can be solved as follows

Implement CommandLineRunner , rewrite run method so that it will not be closed after startup

@SpringBootApplication
@EnableDubbo
public class SeaProviderLogApplication implements CommandLineRunner {

    public static void main(String[] args) {
        SpringApplication.run(SeaProviderLogApplication.class, args);
    }

    @Override
    public void run(String... args) throws Exception {
        System.out.println("SeaProviderLogApplication is starting...");
        while(true) {
            Thread.sleep(600000000);
            System.out.println("sleep....");
        }
    }
}

Someone might say that the introduction of spring-boot-starter-web is mainly for the convenience of testing, but in fact, unit testing can be used for operation.

Use @SpringBootTest and @RunWith(SpringRunner.class) annotations to perform unit testing. The code is as follows

@SpringBootTest
@RunWith(SpringRunner.class)
public class IndexControllerTest {

    @Reference(version = "1.0.1")
    private ErrorLogService errorLogService;

    @Test
    public void bbb() {
        ErrorLog errorLog = new ErrorLog();
        errorLog.setName("error");
        System.out.println(errorLogService.sendMsg(errorLog));
    }
}

web project

 But sometimes due to the Maven aggregation project, it will rely on common or parent , which will naturally be introduced

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

 If it is started at this time, the default port is 8080, of course it can be configured in application.properties

server.port=8081 to modify, but it is more troublesome, because the http request is not exposed, there is no need to add spring-boot-starter-web dependency. If there are many services, the port setting is also a headache, which will cause port occupation problems.

Since web services are not provided, there is no need to expose the port. You can start it in the following two ways without setting the port number

The first:

Modify the application configuration file

spring:
  main:
    allow-bean-definition-overriding: true
    web-application-type: none

 The second type:

Modify the startup entry

    public static void main(String[] args) {
        new SpringApplicationBuilder(Application .class)
                .web(WebApplicationType.NONE)//.REACTIVE, .SERVLET
                .run(args);
    }

OK, perfect solution, no need to consider port allocation problem anymore

Springboot integration dubbo can refer to  springboot2.x pure annotation integration dubbo

Reference: https://cloud.tencent.com/developer/article/1473490 springboot does not occupy the port to start-Cloud + Community-Tencent Cloud