2016-08-29 49 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

相關問題