ami*_*ion 9 java client-applications entry-point spring-boot
我在一个应用程序中有2个主要入口点.
第一个main启动服务器,映射控制器并启动一些工作线程.这些工作人员从云队列接收消息.
如果负载增加,我希望能够增加额外的工作来完成我的工作.所以我在我的应用程序中有第二个主要入口点,我希望能够在不启动 spring-boot(作为客户端应用程序)的默认服务器的情况下启动,以避免端口冲突(显然会导致失败).
我该如何实现这一目标?
ale*_*xbt 18
server和client配置文件从命令行启动要使用具有2个不同配置文件的相同jar和相同入口点,您应该只是在运行时提供Spring配置文件,以便具有不同的应用程序 - $ {profile} .properties已加载(并且可能触发条件Java配置).
定义2个弹簧轮廓(client和server):
application-${profile}.properties有一个SpringBootApp和入口点:
@SpringBootApplication
public class SpringBootApp {
public static void main(String[] args) {
new SpringApplicationBuilder()
.sources(SpringBootApp.class)
.run(args);
}
}
Run Code Online (Sandbox Code Playgroud)
让这个班级成为你的主要班级.
src/main/resources/application-server.properties:
spring.application.name=server
server.port=8080
Run Code Online (Sandbox Code Playgroud)
src/main/resources/application-client.properties:
spring.application.name=client
spring.main.web-environment=false
Run Code Online (Sandbox Code Playgroud)
从命令行启动两个配置文件:
$ java -jar -Dspring.profiles.active=server YourApp.jar
$ java -jar -Dspring.profiles.active=client YourApp.jar
Run Code Online (Sandbox Code Playgroud)
您也可以@Configuration根据活动配置文件有条件地触发类:
@Configuration
@Profile("client")
public class ClientConfig {
//...
}
Run Code Online (Sandbox Code Playgroud)
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);
}
}
Run Code Online (Sandbox Code Playgroud)
您可以指定其他配置类(特定于客户端或服务器):
new SpringApplicationBuilder()
.sources(SpringBootApp.class, ClientSpecificConfiguration.class)
.profiles("client")
.web(false)
.run(args);
Run Code Online (Sandbox Code Playgroud)
src/main/resources/application-server.properties:
spring.application.name=server
server.port=8080
Run Code Online (Sandbox Code Playgroud)
src/main/resources/application-client.properties:
spring.application.name=client
#server.port= in my example, the client is not a webapp
Run Code Online (Sandbox Code Playgroud)
注意,你可能还有2个SpringBootApp(
ClientSpringBootApp,ServerSpringBootApp),每个都有自己的main,它是一个类似的设置,允许你配置不同的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);
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8721 次 |
| 最近记录: |