2016-08-29 50 views
2

我有一个应用程序2主入口点如何将spring-boot作为客户端应用程序运行?

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

在负荷增加的情况下,我希望能够增加更多的工人做我的工作。所以,我在我的应用程序中的第二主切入点,我希望能够启动没有春天开机启动默认的服务器(作为客户端应用程序),以避免端口冲突(显然这将导致失败)。

如何实现这一目标?

+1

在Spring Cloud中运行它并让它为您管理水平缩放比较好。 https://spring.io/guides/gs/spring-cloud-and-lattice/ – duffymo

+0

像aws(beanstalk)这样的其他云也可以管理水平缩放。 – amitection

+1

当然。我的观点是你应该让他们去做;不要试图自己管理这个。懒惰 - 使用可用的。 – duffymo

回答

7

serverclient型材

要使用相同的罐子和相同的入口点有2个不同的配置文件的命令行启动,您应该简单地提供了Spring配置文件在运行时,有不同的应用程序 - $ {简介} .properties加载(并可能触发条件Java配置)。

定义2个弹簧型材(clientserver

  • 每辆车都有自己的application-${profile}.properties
  • 客户端的属性将禁用Web容器

有一个SpringBootApp和入口点

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

使这个类你的主类。

的src /主/资源/ application-server.properties

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

的src /主/资源/ 应用程序的客户端。性能

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 { 
    //... 
} 

启动从与serverclient型材

发射器的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); 
    } 
} 

可以指定额外配置类(特定于客户端或服务器):

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(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); 
    } 
} 
+0

如何从终端运行这两个不同的应用程序“服务器”和“客户端”?我需要两种不同的版本,还是只需使用“-cp”就可以完成? – amitection

+0

我通过[this](http://stackoverflow.com/questions/19882752/how-to-configure-pom-xml-to-run-2-java-mains-in-1-maven-project)但它似乎不适用于弹簧引导。 – amitection

+0

更新了答案 – alexbt

相关问题