Microservice application with service discovery using Jhipster

Prakash
Developers Stacks
Published in
8 min readOct 25, 2017

Instroduction

JHipster is one of those open-source projects you stumble upon and immediately think, “Of course!” It combines three very successful frameworks in web development: Bootstrap, Angular, and Spring Boot. Bootstrap was one of the first dominant web-component frameworks. Its largest appeal was that it only required a bit of HTML and it worked! Bootstrap showed many in the Java community how to develop components for the web. It leveled the playing field in HTML/CSS development, much like Apple’s Human Interface Guidelines did for iOS apps.

At its core, JHipster is a Yeoman generator. Yeoman is a code generator that you run with a yo command to generate complete applications or useful pieces of an application. Yeoman generators promote what the Yeoman team calls the "Yeoman workflow" This is an opinionated client-side stack of tools that can help developers quickly build beautiful web applications. It takes care of providing everything needed to get working without the normal pains associated with a manual setup.

Learn more about JHipster, including its origin, at http://jhipster.github.io.

This tutorial shows you how to build a microservices architecture with JHipster 4.5.4. You’ll generate a gateway (powered by Netflix Zuul and the JHipster Gateway), a microservice (that talks to MongoDB), and use Docker Compose to make sure it all runs locally. Then you’ll deploy it to Minikube and Google Cloud using Kubernetes.

The Installing JHipster instructions show you all the tools you’ll need to use a released version of JHipster.

  1. Install Java 8 from Oracle
  2. Install Git from https://git-scm.com
  3. Install Node.js from http://nodejs.org (I used Node 6.11.0 to write this article)
  4. Install Yarn using the Yarn installation instructions
  5. Run the following command to install Yeoman
yarn global add yo

6. Run the following command to install JHipster

yarn global add generator-jhipster

To build a microservices architecture with JHipster, you’ll need to generate two applications and clone another.

  • Generate a gateway
  • Generate a microservice
  • Clone the JHipster Registry

You can see how these components fit in the diagram below.

To create a microservices project, open a terminal window and create a jhipster-microservice-example directory. Then create a blog directory for the gateway application.

mkdir -p jhipster-microservice-example/blog

In JHipster terms, a gateway is a normal JHipster application. This means you can develop it like a monolith, but it also acts as the entrance to your microservices. More specifically, it provides HTTP routing and load balancing, quality of service, security, and API documentation for all microservices.

In a terminal, navigate to the news directory and run jhipster.

cd jhipster-microservice-example/blogjhipster

JHipster prompts you with many questions about the type of application you want to generate and what features you’d like to include. Create the blog application with the 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: cassandra
  • Maven or Gradle: Maven
  • Other technologies:
  • 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

The project creation process will take a couple of minutes to run, depending on your internet connection speed. When it’s finished, you should see output like this:

Before you can run this project, you’ll need to download and start an instance of the JHipster Registry. Run the following commands in the jhipster-microservices-blog directory.

git clone git@github.com:jhipster/jhipster-registry.git registrycd registry && yarn && ./mvnw

The JHipster Registry is built on Spring Cloud Netflix and Spring Cloud Config. Patterns provided by Spring Cloud Netflix include Service Discovery (Eureka), Circuit Breaker (Hystrix), Intelligent Routing (Zuul), and Client Side Load Balancing (Ribbon).

In a previous post, I showed you how you can use Eureka for service discovery. JHipster Registry is a Eureka server, a Spring Cloud Config server, as well as an administration server. It includes dashboards to monitor and manage your JHipster applications.

JHipster Registry starts on port 8761 by default.

In a new terminal window, navigate to jhipster-microservices-example/blog and run ./mvnw to start the blog application and open http://localhost:8080 in your favorite browser. The first thing you’ll notice is a dapper-looking fellow explaining how you can sign in or register.

Sign in with username admin and password admin and you’ll have access to navigate through the Administration section. This section offers nice looking UIs on top of some Spring Boot’s many monitoring and configuration features. It also allows you to administer users:

And it allows you to see the Swagger docs associated with its API.

You can run the following command (in a separate terminal window) to start the Protractor tests and confirm everything is working properly.

yarn e2e

At this point, it’s a good idea to check your project into Git so you can easily see what changes are made going forward.

git initgit add .git commit -m “gateway created”

Shut down your blog application before proceeding to the next section.

For each entity you want to create, you will need:

  • 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

Also, you should have integration tests to verify that everything works and performance tests to verify that it runs fast. In an ideal world, you’d also have unit tests and integration tests for your Angular code.

The good news is JHipster can generate all of this code for you, including integration tests and performance tests.

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.

yo jhipster:entity blog

To generate a store microservice, open a terminal window and navigate to the jhipster-microservices-exampledirectory. Create a store directory and run jhipster in it.

cd ~/jhipster-microservices-examplemkdir storecd storejhipster

Use the following settings to generate a microservice that uses Cassandra 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: cassandra
  • 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

Commit your changes to Git. It’s always a good idea to do this before generating entities.

cd ~/jhipster-microservices-example/git add storegit commit -m "Generate store application"

Create a product entity by running the following command 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

Your terminal should look similar to the following after you’ve answered all these questions.

A microservice only contains the server-side code for the entities it contains. To generate an Angular UI for the product, navigate to the blog directory and run the same command.

yo jhipster:entity product

Use the following answers to the questions asked:

  • 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

A visual of these questions and answers is in the screenshot below.

commit your change to git.

cd ~/jhipster-microservices-examplegit add .git commit -m "Add product entity"

At this point, you should be able to verify everything works by starting the registry, blog, store, and Cassandra. You can run Cassandra using Docker Compose with the following command in the blog directory.You’ll need to have Docker installed and running for this command to work.

Command to up the cassandra instance in your application.

docker-compose -f src/main/docker/cassandra.yml up

Go into the bash of cassandra instance.

Command to check the running instance container_id

docker ps
docker exec -it container_id bash

Change to directory to store directory copy the keyspace and table queries.

Create manually keyspace and tables in cassandra instance.

All thinks work properly.

The Docker Compose section shows how you can run all your services using Docker.

Navigate to http://localhost:8080, log in with admin/admin, and go to Entities > Product. You should be able to add a product and see that it has a Cassandra identifier.

In your Blog gateway there is one microservice is running.

--

--