Generating and Deploying Micro Frontends and Microservices With Entando Blueprint
Tags: jhipster, microservices, microfrontends, entando
Published: 2021-08-12 10:31:48

Hi my fellow developers,

Let’s go further in the bundle series today with this introduction to the Entando Component Generator (ECG). Based on [JHipster](https://www.jhipster.tech/), through a [blueprint](https://www.jhipster.tech/modules/creating-a-blueprint/), the ECG makes your life easier when it comes to developing a backend and micro frontend application. It allows you to generate your components and start the development of your business features, and package them into an Entando bundle to deploy them in a few steps.

In this blog post we will introduce the concept of application generation, entity definition and their deployment into an Entando application.

## Prerequisites

In order to run the commands present in this article, you need to have the ENT CLI and a running Entando instance installed. I suggest following the [automatic installation](https://dev.entando.org/v6.3/docs/getting-started/#automatic-install) process.

You also need a [GitHub](https://github.com/) account and two [repositories](https://docs.github.com/en/get-started/quickstart/create-a-repo). By convention we add the “-bundle” to the bundle repository. e.g “testProject” and “testProject-bundle”.

 

You also need a [DockerHub](https://hub.docker.com/) account.


## Generate your Backend and Frontend Components
### The Entando Component Generator
#### A JHipster Blueprint

JHipster is an open-source generator to create cloud and prod ready Java Spring boot applications. The blueprint technology allows third-party developers to customize the JHipster behavior and the outcomes.

The ECG is a blueprint that generates fully Entando compatible Java applications and micro frontends.

The ECG comes with the ENT CLI, or you can install it by running the `check-env` [command](https://dev.entando.org/v6.3/docs/reference/entando-cli.html#check-environment).

The correct JHipster and blueprint versions are automatically installed and are isolated from any existing JHipster versions if you have other instances installed.


#### Generate the Backend

The first step will be to generate the code source by using the JHipster blueprint in order to initiate the backend.

First create a folder to generate your project:


``` bash
mkdir testProject && cd testProject
```


From this folder, generate an application using the following command:


``` bash
ent jhipster --blueprints entando
```


Please note you will probably need to give some informations before JHipster will be launch if you don’t init the project before:

![unnamed_3_d0](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/u7n18jjgpnfwqpeky3jg.png)
 


When answering these questions. You can select default values by just clicking on enter or select specific values. For this blog post I only select the default ones.

![unnamed_1_1_d0](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/psqjrbvs9xiho5tu04it.png)
 


Note that you can elect to enable or disable micro frontend generation by answering the question:


``` bash
Would you like to generate micro frontends when creating entities?
```




* **Always**: MFE will be created each time an entity is created
* **Ask**: Each time an entity is created you can choose to generate the MFE or not
* **Never**: no MFE are created

After this step, your application is generated and contains the backend code in Java based on the SpringBoot and the configuration needed to run it in both dev and production environments.

![unnamed_2_1_d0](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0lgsadlf4nb3m5g7tzju.png)
 
### The Entity Sub-Generator
#### Entity’s Definition

The entity term comes from the Java Persistence API and defines the object used to wrap the data stored in the database in a Plain Old Java Object (POJO).

With JHipster, the entity generator generates this POJO and all the code and configuration with from a definition file.

The Entando Component Generator adds the capability to generate React micro frontends linked to the entity alongside the Java backend code.


#### Micro Frontend Definition

The micro frontend (MFE) extends the concept of self-contained isolated applications introduced by the microservices to apply it to the frontend. A micro frontend is a frontend application with a single responsibility, isolated from the rest of the application. The final application is composed of multiple micro frontends. 

In the HTML page the MFE is included by using a [custom element](https://developer.mozilla.org/en-US/docs/Web/Web_Components/Using_custom_elements), the MFE DOM is placed in a “shadow DOM”.

The ECG generates three micro frontends per entity



* **detailsWidget**: render the entity field values
* **formWidget**: permit the adding/editing for a given entity
* **tableWidget**: renders a list of entity in a HTML table

Each MFE is known as a Widget in the application bundle.



> Why are we talking about MFEs as "widgets" at Entando?
The widget is the Entando component that allows self-contained pieces of frontend logic to be deployed and installed into your Entando application.



#### Generate the Entity

From your project location, run:


``` bash
ent jhipster entity conference
```




* “conference” is the entity name

The first step is to define the entity fields by giving a name, select the type and validation rules. You can create any number of fields you want.


![unnamed_3_1_d0](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6qbjdjqsbma80i0tj2dw.png) 


 

Then you can choose to create relationships with other entities, but for this example we’ll generate only one entity.


![unnamed_4_d0](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9iljk9h9bt1p28tpqsw6.png)
 


The next question allows you to define the architecture you want to use in your application for controllers and business logic.



![unnamed_5_d0](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/h13zexpmtxbx0l03odu3.png) 



> The entity generator lets you choose between different implementations according to whether you want to expose the entities directly or use a business service layer between, with  interfaces or not.



The last step provides a few advanced options. Just answer “no” to both.

![unnamed_6_d0](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3kz26c3kvobq39v5doze.png)
 


Following these questions, the entity is created and the code is updated.


> A message about conflicts with Liquibase files will be displayed. The entity generator replaces the previous master.xml file. Here, just override it. A blog post about Liquibase file management is coming soon.


![unnamed_7_d0](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uxghih3j8ue30weh06jm.png)
 

The JPA entity is available in the domain folder. The MFEs can be found in the ui/widgets folder.


### The Generated Bundle

The bundle is available in the “bundle” folder at the project root level and references both the microservice as a plugin and the MFEs as widgets.

![unnamed_8_d0](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/h6a4bjabc30zp8kjiydc.png)
 

``` yaml
code: testproject-bundle
description: This is the testProject bundle
components:
 plugins:
   - plugins/testproject-plugin.yaml
 widgets:
   - ui/widgets/conference/tableWidget/conference-table-widget-descriptor.yaml
   - ui/widgets/conference/detailsWidget/conference-details-widget-descriptor.yaml
   - ui/widgets/conference/formWidget/conference-form-widget-descriptor.yaml
# entando-needle-descriptor-add-widgets - Entando blueprint will add widget bundle files here
```


But it’s not enough and in the next chapter we will improve it.


### Push the Code into a GitHub Repository

Even if it’s not mandatory to finish this tutorial, I suggest you push the code into a GitHub repository.


``` bash
git add .
git commit -m "first commit"
git branch -M main
git remote add origin https://github.com/<organisation>/testProject.git
git push -u origin main
```

## Build the Microservice and MFEs

To achieving this step the ENT CLI will help us by providing an unique command to build them all, run this command from your project location:


``` bash
ent prj build
```

![unnamed_9_d0](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nz0yd0n2mwvxgotjxge1.png)


This command  builds all the MFEs and the backend application (including the docker image), this process can take some time to complete.

When the build process is fully completed, you should have 



* Frontend build resources (JS, CSS…) in the bundle/resources folder

![unnamed_10_d0](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pwpqyvp8v3oen4mhp0i9.png)


* The MFE bundle definition files updated and moved to the main bundle folder

``` yaml
code: conference-details-widget
titles:
 en: Conference Details Widget
 it: Conference Details Widget
group: free
customUiPath: conference-details-widget.ftl
```


``` html
<#assign wp=JspTaglibs["/aps-core"]>
<script src="<@wp.resourceURL />testproject-bundle/static/js/2.567fc7e7.chunk.js"></script>
<script src="<@wp.resourceURL />testproject-bundle/static/js/2.567fc7e7.chunk.js.map"></script>
<script src="<@wp.resourceURL />testproject-bundle/static/js/main.29aeabf6.chunk.js"></script>
<script src="<@wp.resourceURL />testproject-bundle/static/js/main.29aeabf6.chunk.js.map"></script>
<script src="<@wp.resourceURL />testproject-bundle/static/js/runtime~main.ad29cb6e.js"></script>
<script src="<@wp.resourceURL />testproject-bundle/static/js/runtime~main.ad29cb6e.js.map"></script>
<link href="<@wp.resourceURL />testproject-bundle/static/css/main.552b72a3.chunk.css" rel="stylesheet">
<link href="<@wp.resourceURL />testproject-bundle/static/css/main.552b72a3.chunk.css.map" rel="stylesheet">
<#-- entando_resource_injection_point -->
<#-- Don't add anything above this line. The build scripts will automatically link the compiled JS and CSS for you and add them above this line so that the widget can be loaded-->

<conference-details service-url="/avdev4j/test-project/0-0-1-snapshot" />
```


![unnamed_11_d0](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xnnkd9imoof2nc5i99et.png)


* The docker image is pushed into your local docker repository

``` bash
docker image ls
```

![unnamed_12_d0](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xmx03928xl73xavols4b.png)


## Publish the Bundle

The publishing step will push the artifacts into repositories so that they can be downloaded by the Entando application during the installation phase.

We need two repositories:



* A GitHub repository to store the bundle definition and the JS/CSS artefacts
* A DockerHub project to push the Docker image


### Initiate the Publishing

Run the following command from your project location


``` bash
ent prj pbs-init
```


You need to answer the questions. Please note you have to use the HTTPS url for the GitHub repository.

![unnamed_13_d0](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/eve7a8lwndju8muz3mpy.png)
 

At the conclusion of this command the plugin and the main bundle descriptor have been pushed to GitHub. 

![unnamed_14_d0](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4jahorrmessm0h09a3oj.png)
 
### Publish the Artifacts

Run the following command from your project location:


``` bash
ent prj pbs-publish
```


You will need to provide a version for your bundle. Note this version will be used to tag your bundle on Git. We usually use the [semantic versioning](https://semver.org/), note that if you want to follow the Git tagging convention, you need to explicitly use the prefix “v”, e.g “v0.0.1”.

![unnamed_15_d0](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dz7d8tjf0kdq6qhlpnn5.png)
 


Your GitHub repository contains now all the bundle descriptors and the JS and CSS resource files.

![unnamed_16_d0](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5rt9evu4f6kci1vo1d16.png) 


The Docker image is available on DockerHub.

![unnamed_17_d0](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3zkqjgmslvtsg318os26.png)
 

## Deploy and Install the Bundle

Now that the bundle and the artifacts have been pushed into the different repositories, we can install them on the Entando application.

We define two different steps between making the bundle available in the Entando Component repository and install it or a given version.

To deploying your bundle you need to run this command from your project location:


``` bash
ent prj deploy
```

![unnamed_18_d0 (1)](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7irre21fgakgrgi885rw.png)
 


_The ECR is empty by default._

![unnamed_19_d0](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jkma0a5vrkv628ozo9d3.png)
 

_After the deployment, the ECR contains your bundle._

However the deployment is not enough to use our micro frontends into our application. We also need to explicitly install it. You can do it through the app-builder UI by clicking on the “install” button or by using the ENT CLI again and run the following command:


``` bash
ent prj install
```


Note adding the `--conflict-strategy` parameter allows you to define the strategy to adopt if the components already exist: override, create or skip them.

![unnamed_20_d0 (1)](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/z5dcf3xzfcy9vvvt1xul.png) 
 


_The bundle is installed and the components are available in your Entando application._

Congratulations, your micro frontends are now available and you can add into pages and display them.

To check the plugin running you can run the following command:


``` bash
ent kubectl get pods -n entando
```


You should be able to see two pods with the name starting by “&lt;organisation>-test-project”. One for the database and one is for the backend Java application.

![unnamed_21_d0](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qcrgvq39pem60jser1uu.png)


## Going Further

### Launch the backend and frontend locally

[https://dev.entando.org/v6.3/docs/reference/entando-cli.html#run-a-project-locally](https://dev.entando.org/v6.3/docs/reference/entando-cli.html#run-a-project-locally)


#### Start the Keycloak

In order to run the components locally you firstly need to start the keycloak image by running the following command into your folder:


``` bash
ent prj ext-keycloak start
```


The Keycloak is started when the following message is displayed:


![unnamed_22_d0](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/raif4aw7cjrdbk3p6qfe.png) 


#### Start the Backend

Starting the backend will launch the .jar artifact as you can do with any vanilla SpringBoot executable jar.

Still in you project location, run the command:


```
ent prj be-test-run
```


When the application is ready you can access it to: [http://localhost:8081/services/testProject](http://localhost:8081/services/testProject)

![Screenshot_20210623_at_14.07.40_d0](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yomobojuo0ttmc221j66.png)
 


 


#### Start the MFEs

To start a micro frontend you need to run the following command in your project location:


``` bash
ent prj fe-test-run
```


You will be able to select which one you want to start


![unnamed_23_d0](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/aidbb7atsoepws9ow3vy.png)
 


The MFE is available on the default 3000 port: [http://localhost:3000/](http://localhost:3000/)


![Screenshot_20210623_at_14.30.24_d0](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/w9c0avz4ohm4snkc0twc.png) 


If you want to run more than one at the same time, the port is incremented by one.

![unnamed_24_d0](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fhpxx6wzne7u9vj3ls5n.png)

### Deploy the Frontend or the Backend only

For some reasons you may want to handle only either the frontend or the backend. For example, for a modification in a MFE you should only re-build and push the frontend not the backend and the Docker image.

For achieving this, it’s possible to use the dedicated commands:


<table>
  <tr>
   <td>
   </td>
   <td>Frontend
   </td>
   <td>Backend
   </td>
  </tr>
  <tr>
   <td>Publish
   </td>
   <td><code>ent prj fe-build</code>
   </td>
   <td><code>ent prj be-build</code>
   </td>
  </tr>
  <tr>
   <td>Push
   </td>
   <td><code>ent prj fe-push</code>
   </td>
   <td><code>ent prj be-build</code>
   </td>
  </tr>
  <tr>
   <td>Deploy
   </td>
   <td colspan="2" ><code>ent prj deploy</code>
   </td>
  </tr>
</table>


If you want to learn about the ENT CLI commands, feel free to read [this series](https://www.entando.com/page/en/micro_learning_enhancing_developer_productivity_with_ent_cli?contentId=BLG4496&modelId=64) on our blog.


## Conclusion

This article is probably the one that goes further in the Entando workflow with bundles. From the generation to the installation, we saw a few tools we provide to make the developer's life easier.

Now you know how to leverage these tools to improve your productivity, the next blog post will deal with bundle extractions from an existing environment.

I’m looking forward to seeing you again and sharing my bundle knowledge with you.