如何 运行 spring- 作为客户端应用程序启动?
How to run spring-boot as a client application?
我在单个应用程序中有 2 个主要入口点。
第一个 main 启动服务器,映射控制器并启动一些工作线程。这些工作人员从云队列接收消息。
如果负载增加,我希望能够添加额外的工作人员来完成我的工作。所以我在我的应用程序中有一个 second Main 入口点,我希望能够在不启动 spring 中的默认服务器 的情况下启动 -boot(作为客户端应用程序)以避免端口冲突(显然这会导致失败)。
我该如何实现?
使用 server
和 client
配置文件从命令行启动
要使用具有 2 个不同配置文件的相同 jar 和相同入口点,您应该在运行时简单地提供 Spring 配置文件,以加载不同的 application-${profile}.properties(并且可能有条件 Java 配置触发)。
定义 2 spring 个配置文件(client
和 server
):
- 每个人都会有自己的
application-${profile}.properties
- 客户端的属性将禁用 Web 容器
有一个 SpringBootApp 和入口点:
@SpringBootApplication
public class SpringBootApp {
public static void main(String[] args) {
new SpringApplicationBuilder()
.sources(SpringBootApp.class)
.run(args);
}
}
将此 class 作为您的主要内容-class。
src/main/resources/申请-server.properties:
spring.application.name=server
server.port=8080
src/main/resources/申请-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
classes 也根据活动配置文件有条件地触发:
@Configuration
@Profile("client")
public class ClientConfig {
//...
}
使用 server
和 client
配置文件从 IDE 启动
发射器:
@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);
}
}
您可以指定附加配置classes(特定于客户端或服务器):
new SpringApplicationBuilder()
.sources(SpringBootApp.class, ClientSpecificConfiguration.class)
.profiles("client")
.web(false)
.run(args);
src/main/resources/申请-server.properties:
spring.application.name=server
server.port=8080
src/main/resources/申请-client.properties:
spring.application.name=client
#server.port= in my example, the client is not a webapp
Note, you may also have 2 SpringBootApp (ClientSpringBootApp
,
ServerSpringBootApp
), each with its own main, it's a similar setup
which allow you to configure different AutoConfiguration
or 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);
}
}
我在单个应用程序中有 2 个主要入口点。
第一个 main 启动服务器,映射控制器并启动一些工作线程。这些工作人员从云队列接收消息。
如果负载增加,我希望能够添加额外的工作人员来完成我的工作。所以我在我的应用程序中有一个 second Main 入口点,我希望能够在不启动 spring 中的默认服务器 的情况下启动 -boot(作为客户端应用程序)以避免端口冲突(显然这会导致失败)。
我该如何实现?
使用 server
和 client
配置文件从命令行启动
要使用具有 2 个不同配置文件的相同 jar 和相同入口点,您应该在运行时简单地提供 Spring 配置文件,以加载不同的 application-${profile}.properties(并且可能有条件 Java 配置触发)。
定义 2 spring 个配置文件(client
和 server
):
- 每个人都会有自己的
application-${profile}.properties
- 客户端的属性将禁用 Web 容器
有一个 SpringBootApp 和入口点:
@SpringBootApplication
public class SpringBootApp {
public static void main(String[] args) {
new SpringApplicationBuilder()
.sources(SpringBootApp.class)
.run(args);
}
}
将此 class 作为您的主要内容-class。
src/main/resources/申请-server.properties:
spring.application.name=server
server.port=8080
src/main/resources/申请-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
classes 也根据活动配置文件有条件地触发:
@Configuration
@Profile("client")
public class ClientConfig {
//...
}
使用 server
和 client
配置文件从 IDE 启动
发射器:
@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);
}
}
您可以指定附加配置classes(特定于客户端或服务器):
new SpringApplicationBuilder()
.sources(SpringBootApp.class, ClientSpecificConfiguration.class)
.profiles("client")
.web(false)
.run(args);
src/main/resources/申请-server.properties:
spring.application.name=server
server.port=8080
src/main/resources/申请-client.properties:
spring.application.name=client
#server.port= in my example, the client is not a webapp
Note, you may also have 2 SpringBootApp (
ClientSpringBootApp
,ServerSpringBootApp
), each with its own main, it's a similar setup which allow you to configure differentAutoConfiguration
orComponentScan
:
@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);
}
}