You may have build the all the microservices in a single project or One project has many micro services.
In that case how to run those services picking up one by one as needed.
Example i have taken from
https://spring.io/blog/2015/07/14/microservices-with-spring
eg : we have three micro services projects.
1. Registration server - eureka server
2. Account service
3. Web
How to run without building the jar:
we use maven and use spring-boot plugin to specify 'run' and give the main class with "-Dstart-class" tag
below is the three sample commands, run from the sample project directory where the POM file exist.(where you run mvn clean package command)
mvn spring-boot:run -Dstart-class=io.pivotal.microservices.services.registration.RegistrationServer
mvn spring-boot:run -Dstart-class=io.pivotal.microservices.services.accounts.AccountsServer
mvn spring-boot:run -Dstart-class=io.pivotal.microservices.services.web.WebServer
How to run with Jar
Since we have few main classes
1. We need to specify the maven what is our main class
<properties>
<start-class>io.pivotal.microservices.services.Main</start-class>
</properties>
2. Create One single class with Main method.
We call it Main.java
As we can. specify only one main class, we need to create a class with one main method which will guide to / call to other main methods (RegistrationServer.java , AccountsServer.java, WebServer.java)
3. Run jar with calling each service via Main.java
open three command lines and execute below three command, which will up three services
java -jar target/microservices-demo-2.0.0.RELEASE.jar registration
java -jar target/microservices-demo-2.0.0.RELEASE.jar accounts
java -jar target/microservices-demo-2.0.0.RELEASE.jar web
----------------------------------------------------------------below code snipet for Main.java--------------
If you do not create Main.java, still maven will build the project without error, but running this you will get
Exception in thread "main" java.lang.ClassNotFoundException: io.pivotal.microservices.services.Main
----------------------------------------------------------------------------------
In that case how to run those services picking up one by one as needed.
Example i have taken from
https://spring.io/blog/2015/07/14/microservices-with-spring
eg : we have three micro services projects.
1. Registration server - eureka server
2. Account service
3. Web
How to run without building the jar:
we use maven and use spring-boot plugin to specify 'run' and give the main class with "-Dstart-class" tag
below is the three sample commands, run from the sample project directory where the POM file exist.(where you run mvn clean package command)
mvn spring-boot:run -Dstart-class=io.pivotal.microservices.services.registration.RegistrationServer
mvn spring-boot:run -Dstart-class=io.pivotal.microservices.services.accounts.AccountsServer
mvn spring-boot:run -Dstart-class=io.pivotal.microservices.services.web.WebServer
How to run with Jar
Since we have few main classes
1. We need to specify the maven what is our main class
<properties>
<start-class>io.pivotal.microservices.services.Main</start-class>
</properties>
2. Create One single class with Main method.
We call it Main.java
As we can. specify only one main class, we need to create a class with one main method which will guide to / call to other main methods (RegistrationServer.java , AccountsServer.java, WebServer.java)
3. Run jar with calling each service via Main.java
open three command lines and execute below three command, which will up three services
java -jar target/microservices-demo-2.0.0.RELEASE.jar registration
java -jar target/microservices-demo-2.0.0.RELEASE.jar accounts
java -jar target/microservices-demo-2.0.0.RELEASE.jar web
----------------------------------------------------------------below code snipet for Main.java--------------
If you do not create Main.java, still maven will build the project without error, but running this you will get
Exception in thread "main" java.lang.ClassNotFoundException: io.pivotal.microservices.services.Main
----------------------------------------------------------------------------------
package io.pivotal.microservices.services; import io.pivotal.microservices.services.accounts.AccountsServer; import io.pivotal.microservices.services.registration.RegistrationServer; import io.pivotal.microservices.services.web.WebServer; /** * Allow the servers to be invoked from the command-line. The jar is built with * this as theMain-Class
in the jar'sMANIFEST.MF
. * * @author Paul Chapman */ public class Main { public static void main(String[] args) { String serverName = "NO-VALUE"; switch (args.length) { case 2: // Optionally set the HTTP port to listen on, overrides // value in the-server.yml file System.setProperty("server.port", args[1]); // Fall through into .. case 1: serverName = args[0].toLowerCase(); break; default: usage(); return; } if (serverName.equals("registration") || serverName.equals("reg")) { RegistrationServer.main(args); } else if (serverName.equals("accounts")) { AccountsServer.main(args); } else if (serverName.equals("web")) { WebServer.main(args); } else { System.out.println("Unknown server type: " + serverName); usage(); } } protected static void usage() { System.out.println("Usage: java -jar ... [server-port]"); System.out.println( " where server-name is 'reg', 'registration', " + "'accounts' or 'web' and server-port > 1024"); } }
No comments:
Post a Comment