如何将spring

编程入门 行业动态 更新时间:2024-10-09 19:22:42
本文介绍了如何将spring-boot作为客户端应用程序运行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我在一个应用程序中有 2个主要入口点。

I have 2 Main entry points in a single application.

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

The first main starts the server, maps the controllers and starts some worker threads. These workers receive messages from cloud queues.

如果负载增加,我希望能够增加额外的工作来完成我的工作。所以我的应用程序中有一个第二个主入口点,我希望能够在spring-boot(作为客户端应用程序)中启动而不启动默认服务器,以便避免端口冲突(显然会导致失败)。

In case of increased load, I want to be able to add additional workers to do my job. So I have a second Main entry point in my application which I want to be able to start without starting the default server in spring-boot (as a client application) so as to avoid port conflict (and obviously which will lead to failure).

我如何实现这一目标?

How do I achieve this?

推荐答案

使用 server 和客户端配置文件

要使用相同的jar和相同的入口点以及2个不同的配置文件,您应该只是在运行时提供Spring配置文件,具有不同的应用程序 - 加载$ {profile} .properties(并且可能触发条件Java配置)。

Launching from the command line with server and client profiles

To use the same jar and same entry point with 2 different profiles, you should simply provide the Spring profile at runtime, to have distinct application-${profile}.properties loaded (and potentially Conditional Java config triggered).

定义2个弹簧配置文件(客户和服务器):

Define 2 spring profiles (client and server):

  • 每个都有自己的应用程序 - $ {profile} .properties
  • 客户端的属性将禁用Web容器

拥有一个SpringBootApp和入口点:

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

将此课程作为主要班级。

Make this class your main-class.

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

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

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

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

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 根据活动配置文件有条件触发的类:

You may have @Configuration classes triggered conditionally based on the active profile too:

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

L.使用服务器和客户端配置文件从IDE启动

启动器:

Launching from the IDE with server and client profiles

Launchers:

@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); } }

您可以指定其他配置类(特定于客户端或服务器):

You may specify additional configuration classes (specific to client or server):

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

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

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

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

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

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

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

注意,您可能还有2个SpringBootApp( ClientSpringBootApp , ServerSpringBootApp ),每个都有自己的main,它是一个类似的设置,允许你配置不同的 AutoConfiguration 或 ComponentScan :

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); } }

更多推荐

如何将spring

本文发布于:2023-11-16 14:44:24,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1605220.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何将   spring

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!