Sunday, October 14, 2018

Setting up a cluster of JHipster registry and cluster microservice

 
Today I'm going to demonstrate on setting up a cluster of JHipster registry and cluster microservice.
At the beginning, we need to install JHipster. So, follow the below instructions.
1. Install java 8 from Oracle
2. Install Git 
3. Install Node.js
4. Install Yarn using the Yarn installation instructions
5. Install install Yeoman by running below 

yarn global add yo

6. Finally, run the following comman to install JHipster.

yarn global add generator-jhipster

Microservices with JHipster
In order to build microservices architecture with JHipster, we need to create two applications and clone them with another one.
1. Generate a gateway
2. Generate a microservice
3. Clone the JHipster Registry


Creating an API Gateway
Open a terminal window and create a directory called example and create a blog directory for the gateway application. Then navigate to the blog directory and run JHipster
cd example
Jhipster
Then, it will ask you many questions regarding the application you want to generate and set the features as you wish.
Here, I build the application using following settings
Application type: Microservice gateway
Base name of the application: blog
Port: 8080
Default package name: org.jhipster.blog
JHipster Registry: Yes
Type of authentication: JWT
Type of database: SQL
Production database: PostgreSQL
Development database: H2 with disk-based persistence
Maven or Gradle: Maven
Other technologies: Elasticsearch
Client framework: Angular 4
Sass for CSS: Yes
Internationalization support: Yes
Native language: English
Additional languages: Spanish
Testing frameworks: Gatling, Protractor
Install other generators from the JHipster Marketplace: No



After finishing the application creation process it will show you the following message.

Before you can run this created project, you need to download and start an instance of the JHipster Registry. Run the below command to do that.
cd registry && yarn && ./mvnw

Run the below command in another terminal window
yarn start
Now, JHipster starts on port 8761

In a new terminal window navigate to the example/blog directory and run ./mvnw to start the blog application on port  8080 and go to the http://localhost:8080 in a web browser

Here, you can sign in by giving admin and admin for username and password respectively. You can see the metrics under Administration


Check the health under the Administration

You can also check the swagger docs API from Administration > API

Generate Entities
For each entity you want to create
·       A database table
·       A Liquibase change set
·       A JPA entity class
·       A Spring Data JpaRepository interface
·       A Spring MVC RestController class
·       An Angular model, state, component, dialog components, service
·       Several HTML pages for each component

JHipster can generate all of this code for you, including integration tests and performance tests. In addition, if you have entities with relationships, it will generate the necessary schema to support them (with foreign keys), and the TypeScript and HTML code to manage them. You can also set up validation to require certain fields, as well as control their length.
JHipster supports several methods of code generation. The first uses its entity sub-generator. The entity sub-generator is a command-line tool that prompts you with questions which you answer. JDL-Studio is a browser-based tool for defining your domain model with JHipster Domain Language (JDL). Finally, JHipster-UML is an option for those that like UML. Supported UML editors include Modelio, UML Designer, GenMyModel, and Visual Paradigm. I like the visual nature of JDL-Studio, so I’ll use it for this project.
Create a jhipster-jdl.jh file using Notepad++ or any other tool and save the below code to it.
entity Blog {
    name String required minlength(3),
    handle String required minlength(2)
}
 
entity Entry {
    title String required,
    content TextBlob required,
    date ZonedDateTime required
}
 
entity Tag {
    name String required minlength(2)
}
 
relationship ManyToOne {
    Blog{user(login)} to User,
    Entry{blog(name)} to Blog
}
 
relationship ManyToMany {
    Entry{tag(name)} to Tag{entry}
}
 
paginate Entry, Tag with infinite-scroll

Open a new terminal and navigate to the blog directory to import the jhipster-jdl.jh file. Then, run the below command.
jhipster import-jdl ~/blog/jhipster-jdl.jh
 
You’ll be prompted to overwrite src/main/resources/config/liquibase/master.xml. Type a to overwrite this file, as well as others.
Start the application using  ./mvnw and run yarn start(in another window) to view the UI for generate entities. Create a couple of blog for the current user.

Create a Microservice
Open a new terminal to generate a microservice called store, and create a store directory and jhipster in it.
Use the following settings to generate a microservice that uses MongoDB for its database.
Application type: Microservice application
Base name of the application: store
Port: 8081
Default package name: org.jhipster.store
Type of authentication: JWT
Use JHipster Registry: Yes
Type of database: MongoDB
Maven or Gradle: Maven
Other technologies: None
Internationalization support: Yes
Native language: English
Additional languages: Spanish
Testing frameworks: Gatling
Install other generators from the JHipster Marketplace: No

Generate Product Entity
Run the following command to create the product entity in the store directory
jhipster entity product
Use the following answers for the questions asked:

Do you want to add a field to your entity? Yes
What is the name of your field? name
What is the type of your field? String
Do you want to add validation rules to your field? Yes
Which validation rules do you want to add? Required

Do you want to add a field to your entity? Yes
What is the name of your field? price
What is the type of your field? BigDecimal
Do you want to add validation rules to your field? Yes
Which validation rules do you want to add? Required

Do you want to add a field to your entity? No

Do you want to use a Data Transfer Object (DTO)? No
Do you want to use separate service class for your business logic? No
Do you want pagination on your entity? Yes, with pagination links
Then, the terminal will be shown as given below
Generate UI for Product Entity
Since the microservice only contains the server-side code for the entities, we need to generate Angular JS or React JS UI for the product. Hence, go to the store directory run the below command.
jhipster entity product
You need to use below answers to the next
·       Do you want to generate this entity from an existing microservice? Yes
·       Enter the path to the microservice root directory: ../store
·       Do you want to update the entity? Yes

Now, you should be able to verify everything works by starting the registry, blog, store, and MongoDB. You can run MongoDB using Docker Compose with the following command in the store directory. You’ll need to have Docker installed and running for this command to work.
docker-compose -f src/main/docker/mongodb.yml up
Navigate to http://localhost:8080 and log in with admin/admin as username and password, then go to Entities > Product. Here, you should be able to add a product and see that it has a MongoDB identifier.



No comments:

Post a Comment