How to run spring-boot as a client application?

I have 2 Main entry points in one application.

the first main one starts the server, maps the controllers and starts some workflows. These workers receive messages from cloud queues.

In case of increased workload, I want to be able to add additional workers to do my job. Therefore, in my application there is a second Main entry point , which I want to run without starting the default server in spring-boot (as a client application) in order to avoid port conflict (and, obviously, it will cause a crash).

How do I achieve this?

+2
source share
1 answer

Run from the command line with profiles serverandclient

To use the same entry point and the same entry point with two different profiles, you just need to provide the Spring profile at run time so that a different application is loaded - $ {profile} .properties (and possibly the conditional Java configuration was launched).

2 Spring determine profiles ( clientand server) :

  • Each of them will have its own. application-${profile}.properties
  • Client Properties Disable Web Container

You have one SpringBootApp and an entry point :

@SpringBootApplication
public class SpringBootApp {
    public static void main(String[] args) {
        new SpringApplicationBuilder()
            .sources(SpringBootApp.class)
            .run(args);
    }
}

Make this class your primary class.

SRC/// application-server.properties

spring.application.name=server
server.port=8080

SRC/// application-client.properties

spring.application.name=client
spring.main.web-environment=false

:

$ java -jar -Dspring.profiles.active=server YourApp.jar
$ java -jar -Dspring.profiles.active=client YourApp.jar

@Configuration, :

@Configuration
@Profile("client")
public class ClientConfig {
    //...
}

IDE server client

@SpringBootApplication
public class SpringBootApp {
}

public class LauncherServer {
    public static void main(String[] args) {
        new SpringApplicationBuilder()
            .sources(SpringBootApp.class)
            .profiles("server")
            .run(args);
    }
}

public class ClientLauncher {
    public static void main(String[] args) {
        new SpringApplicationBuilder()
            .sources(SpringBootApp.class)
            .profiles("client")
            .web(false)
            .run(args);
    }
}

( ):

new SpringApplicationBuilder()
    .sources(SpringBootApp.class, ClientSpecificConfiguration.class)
    .profiles("client")
    .web(false)
    .run(args);

SRC/// application-server.properties

spring.application.name=server
server.port=8080

SRC/// application-client.properties

spring.application.name=client
#server.port= in my example, the client is not a webapp

: 2 SpringBootApp (ClientSpringBootApp, ServerSpringBootApp), , AutoConfiguration ComponentScan:

@SpringBootApplication
@ComponentScan("...")
public class ServerSpringBootApp {
    public static void main(String[] args) {
        new SpringApplicationBuilder()
            .sources(ServerSpringBootApp.class)
            .profiles("server")
            .run(args);
    }
}

//Example of a difference between client and server
@SpringBootApplication(exclude = SecurityAutoConfiguration.class) 
@ComponentScan("...")
public class ClientSpringBootApp {
    public static void main(String[] args) {
        new SpringApplicationBuilder()
            .sources(ClientSpringBootApp.class)
            .profiles("client")
            .web(false)
            .run(args);
    }
}
+8

Source: https://habr.com/ru/post/1652799/


All Articles