如何将Spring Boot作为客户端应用运行?

12

在一个应用程序中,我有两个主要入口点

第一个主要入口点启动服务器,映射控制器并启动一些工作线程。这些工作线程从云队列中接收消息。

如果负载增加,我希望能够添加额外的工作线程来完成我的工作。因此,在我的应用程序中,我有一个第二个主要入口点,我想要能够在不启动spring-boot默认服务器(作为客户端应用程序)的情况下启动它,以避免端口冲突(很明显这将导致失败)。

我该如何实现这一点?


1
最好在Spring Cloud中运行它,并让它为您管理水平扩展。https://spring.io/guides/gs/spring-cloud-and-lattice/ - duffymo
其他云服务商,如AWS(Beanstalk),也可以管理水平扩展。 - amitection
1
当然。我的意思是你应该让他们去做;不要试图自己管理这个。懒一点——使用现有的资源。 - duffymo
1个回答

24

使用命令行启动serverclient配置文件

若要使用相同的JAR包和入口点,但加载2个不同配置文件,只需在运行时提供Spring配置文件即可。这样就可以加载不同的application-${profile}.properties并可能触发有条件的Java配置。

定义2个Spring配置文件(clientserver:

  • 每个都有自己的application-${profile}.properties
  • client的属性将禁用Web容器

拥有单个SpringBootApp和入口点:

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

将这个类作为你的主类。

src/main/resources/application-server.properties

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

src/main/resources/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启动具有serverclient配置文件的应用程序

启动器:

@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/main/resources/application-server.properties:

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

src/main/resources/application-client.properties:

spring.application.name=client
#server.port= in my example, the client is not a webapp
请注意,您还可以拥有2个SpringBootApp(ClientSpringBootAppServerSpringBootApp),每个都有自己的主要入口点,这是一种类似的设置,可以让您配置不同的AutoConfigurationComponentScan
@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);
    }
}

我如何在终端中运行这两个不同的应用程序“服务器”和“客户端”?我需要有两个不同的构建还是可以简单地使用“-cp”来完成? - amitection
我看了这个,但似乎在spring-boot中不起作用。 - amitection
1
太棒了!正是我在寻找的。非常感谢! - amitection

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接