I have searched throughout the web and below is the sample code i have found.
https://spring.io/blog/2015/07/14/microservices-with-spring
below are the simplified steps in creating running the project
created pom with all dependencies and build the maven project
1. create package "io.pivotal.microservices.services.registration"
a.)RegistrationServer.java
b.) registration-server.yml inside "src\main\resources"
c.) run RegistrationServer.java and access http://localhost:1111/
run RegistrationServer with jar created.
2. Create account services
a.) create package "io.pivotal.microservices.services.accounts" and service class "AccountsServer.java"
This class need "AccountsServer.class" and "AccountRepository.java"
b.) Business logic for Account. create package "io.pivotal.microservices.accounts"
create "Account.java" pojo
create "AccountRepository.java"
create "AccountsConfiguration.java"
create "AccountsController.java". --> need exceptioon class also for this.
create "HomeController.java"
c.) create exception class for account
create package "io.pivotal.microservices.exceptions" and class "AccountNotFoundException.java"
Now error in the "AccountsServer.java" will be resolved.
Run "AccountsServer.java" -->
3. Errors
a.)error [db-config.properties] cannot be opened because it does not exist
Added "db-config.properties" to folder "src\main\resources"
b.)Error
class path resource [testdb/schema.sql] cannot be opened because it does not exist
create folder "testdb" inside "resources" and Added "schema.sql" and "data.sql"
4. Now run "http://localhost:1111/" , you can see the account service registered.
for more info gotot "http://localhost:1111/eureka/apps/"
Alternativel go and see "http://localhost:1111/eureka/apps/ACCOUNTS-SERVICE"
5. Configuration options
Registration takes upto 30s cause this is default client refresh time. Can change setting "eureka.instance.leaseRenewalIntervalInSeconds"
Note : Smaller numbers are not recommened in Prod
eureka:
instance:
leaseRenewalIntervalInSeconds: 5 # DO NOT DO THIS IN PRODUCTION
6. Acessing microservice web service
a.) create package "io.pivotal.microservices.services.web"
b.) create classes
"Account.java"
HomeController.java
SearchCriteria.java
WebAccountsController.java --> needs "WebAccountsController"
WebAccountsService.java
WebServer.java
7. Make sure you have yml files for each service
registration-server.yml
accounts-server.yml
web-server.yml
8. Running Three services without building jar
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
9. Building jar file.
As we have 3 main classes we need to specify the main class inside POM file.
In this case we create onc single class with main method "Main.java" and call other services via that class
inside POM
<properties>
<start-class>io.pivotal.microservices.services.Main</start-class>
</properties>
Then create Main class inside package "io.pivotal.microservices.services"
Call the each service and run as below
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
https://spring.io/blog/2015/07/14/microservices-with-spring
below are the simplified steps in creating running the project
created pom with all dependencies and build the maven project
1. create package "io.pivotal.microservices.services.registration"
a.)RegistrationServer.java
b.) registration-server.yml inside "src\main\resources"
c.) run RegistrationServer.java and access http://localhost:1111/
run RegistrationServer with jar created.
2. Create account services
a.) create package "io.pivotal.microservices.services.accounts" and service class "AccountsServer.java"
This class need "AccountsServer.class" and "AccountRepository.java"
b.) Business logic for Account. create package "io.pivotal.microservices.accounts"
create "Account.java" pojo
create "AccountRepository.java"
create "AccountsConfiguration.java"
create "AccountsController.java". --> need exceptioon class also for this.
create "HomeController.java"
c.) create exception class for account
create package "io.pivotal.microservices.exceptions" and class "AccountNotFoundException.java"
Now error in the "AccountsServer.java" will be resolved.
Run "AccountsServer.java" -->
3. Errors
a.)error [db-config.properties] cannot be opened because it does not exist
Added "db-config.properties" to folder "src\main\resources"
b.)Error
class path resource [testdb/schema.sql] cannot be opened because it does not exist
create folder "testdb" inside "resources" and Added "schema.sql" and "data.sql"
4. Now run "http://localhost:1111/" , you can see the account service registered.
for more info gotot "http://localhost:1111/eureka/apps/"
Alternativel go and see "http://localhost:1111/eureka/apps/ACCOUNTS-SERVICE"
5. Configuration options
Registration takes upto 30s cause this is default client refresh time. Can change setting "eureka.instance.leaseRenewalIntervalInSeconds"
Note : Smaller numbers are not recommened in Prod
eureka:
instance:
leaseRenewalIntervalInSeconds: 5 # DO NOT DO THIS IN PRODUCTION
6. Acessing microservice web service
a.) create package "io.pivotal.microservices.services.web"
b.) create classes
"Account.java"
HomeController.java
SearchCriteria.java
WebAccountsController.java --> needs "WebAccountsController"
WebAccountsService.java
WebServer.java
7. Make sure you have yml files for each service
registration-server.yml
accounts-server.yml
web-server.yml
8. Running Three services without building jar
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
9. Building jar file.
As we have 3 main classes we need to specify the main class inside POM file.
In this case we create onc single class with main method "Main.java" and call other services via that class
inside POM
<properties>
<start-class>io.pivotal.microservices.services.Main</start-class>
</properties>
Then create Main class inside package "io.pivotal.microservices.services"
Call the each service and run as below
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
No comments:
Post a Comment