1. Introduction
1.介绍
In this writeup, we’ll explore an interesting example with JHipster – building a simple microservices architecture. We’ll show how to build and deploy all the required pieces, and in the end, we’ll have a full-blown microservice application up and running.
在这篇报道中,我们将通过JHipster探讨一个有趣的例子–构建一个简单的微服务架构。我们将展示如何构建和部署所有需要的部分,最后,我们将拥有一个完整的微服务应用程序并开始运行。
If you’re new to JHipster, please check our introductory article before proceeding to have a good understanding of the basics of this project generation tool.
如果你是JHipster的新手,请在继续前查看我们的介绍性文章,以便充分了解这个项目生成工具的基本情况。
2. Monolithic vs. Microservice
2.单片机与微服务
In our initial article, we illustrated how to create and bootstrap a monolithic application which is relatively easy to maintain.
在我们最初的文章中,我们说明了如何创建和引导一个相对容易维护的单体应用。
Our microservice system, on the other hand, will separate the front-end the back-end which, in turn, can also be split into several small applications, each dealing with a subset of the full application domain. Naturally, as with all microservice implementations, this solves some problems but also introduces some complexity, such as dealing with component registry and security.
另一方面,我们的微服务系统将把前端和后端分开,而后端也可以分成几个小的应用,每个应用处理整个应用领域的一个子集。当然,和所有的微服务实现一样,这解决了一些问题,但也引入了一些复杂性,比如处理组件注册和安全。
JHipster will take care of most difficulties of managing the microservice applications, with the help of modern open-source tools like Netflix’s Eureka Server and Hashicorp’s Consul.
JHipster将在Netflix的Eureka Server和Hashicorp的Consul等现代开源工具的帮助下,解决管理微服务应用程序的大多数困难。
There are, of course, some things to consider here, like how big or complicated our domain is, how critical is our application and what levels of availability do we want need to have, are we going to host our services on different servers and locations, etc. The goal of these tools is of course to these permutations possible and easy to manage.
当然,这里有一些事情需要考虑,比如我们的领域有多大或多复杂,我们的应用有多关键,我们希望需要有什么级别的可用性,我们是否要将我们的服务托管在不同的服务器和地点,等等。这些工具的目标当然是使这些变化成为可能并易于管理。
2.1. JHipster Microservice Components
2.1.JHipster微服务组件
When working on a Microservice architecture with JHipster, we’ll need to build and deploy at least three different projects: a JHipster Registry, a Microservice Gateway, and at least one Microservice Application.
当使用JHipster进行微服务架构时,我们需要建立和部署至少三个不同的项目:一个JHipster注册中心、一个微服务网关和至少一个微服务应用程序。
The JHipster Registry is an essential piece of the microservice architecture. It ties all the other components together and enables them to communicate with each other.
JHipster注册表是微服务架构的一个重要部分。它将所有其他组件联系在一起,并使它们能够相互通信。
The Microservice Application contains the back-end code. Once running it will expose the API for the domain it is concerned with. A Microservice Architecture may be composed of many microservice applications, each containing a few related entities and business rules.
微服务应用程序包含后端代码。一旦运行,它将暴露它所关注的领域的API。一个微服务架构可能由许多微服务应用组成,每个应用包含一些相关的实体和业务规则。
And the Microservice Gateway has all the front-end (Angular) code and will consume the API created by the whole group of micro service applications:
而微服务网关拥有所有的前端(Angular)代码,并将消费整个微服务应用组创建的API。
3. Installation
3.安装
For all the details about the installation process, check our introductory article on JHipster.
有关安装过程的所有细节,请查看我们的关于JHipster的介绍性文章。
4. Creating a Microservice Project
4.创建一个微服务项目
Now let’s install the three core components of our microservice project.
现在让我们来安装我们的微服务项目的三个核心组件。
4.1. Installing JHipster Registry
4.1.安装JHipster注册表
Since the JHipster Registry is a standard JHipster, so we just need to download and run it. There is no need to modify it:
由于JHipster注册表是一个标准的JHipster,所以我们只需要下载并运行它。没有必要修改它。
git clone https://github.com/jhipster/jhipster-registry
cd jhipster-registry && ./mvnw
This will clone the jhipster-registry project from GitHub and start the application. Once it successfully starts up, we can visit http://localhost:8761/ and log in with user admin and password admin:
这将从GitHub上克隆jhipster-registry项目并启动该应用程序。一旦成功启动,我们可以访问http://localhost:8761/,用用户admin和密码admin登录。
4.2. Installing a Microservice Application
4.2.安装微服务应用程序
Here is where we start to build the actual features of our project. In this example, we’ll create a simple Microservice Application that manages cars. So first we’ll create the application, and then we’ll add an entity to it:
这里是我们开始建立我们项目的实际功能的地方。在这个例子中,我们将创建一个简单的管理汽车的微服务应用程序。因此,首先我们将创建一个应用程序,然后我们将向其添加一个实体。
# create a directory for the app and cd to it
mkdir car-app && cd car-app
# run the jhipster wizard
yo jhipster
Once the wizard starts, let’s follow the instructions to create a Microservice type application named carapp. Some other relevant parameters are:
一旦向导启动,让我们按照说明创建一个名为carapp的微服务类型的应用程序。其他一些相关的参数是。
- port: 8081
- package: com.car.app
- authentication: JWT
- service discovery: JHipster Registry
The screenshot bellow shows the complete set of options:
下面的截图显示了完整的选项集。
Now we’ll add a car entity to our application:
现在我们要在我们的应用程序中添加一个汽车实体。
# runs entity creation wizard
yo jhipster:entity car
The entity creation wizard will start. We should follow the instructions to create an entity named the car with three fields: make, model, and price.
实体创建向导将启动。我们应该按照指示来创建一个名为car的实体,有三个字段。make, model, 和price.。
Once that’s finished, our first Microservice application is complete. If we have a look at the generated code, we’ll notice that there’s no javascript, HTML, CSS or any front-end code. Those will all be produced once the Microservice Gateway is created. Also, check out the README file for important information about the project and useful commands.
一旦完成了这些,我们的第一个微服务应用程序就完成了。如果我们看一下生成的代码,我们会发现没有javascript、HTML、CSS或任何前端代码。这些都将在微服务网关创建后产生。另外,看看README文件,了解关于项目的重要信息和有用的命令。
To finish up, let’s run our newly created component:
最后,让我们运行我们新创建的组件。
./mvnw
Before running the above command, we should ensure that the jhipster-registry component is up and running. Otherwise, we’ll get an error.
在运行上述命令之前,我们应该确保jhipster-registry组件已经启动并运行。否则,我们会得到一个错误。
If everything went according to plan, our car-app would start, and the jhipster-registry log will tell us that the app was successfully registered:
如果一切按计划进行,我们的汽车应用将启动,jhipster-registry日志将告诉我们,该应用已成功注册。
Registered instance CARAPP/carapp:746e7525dffa737747dcdcee55ab43f8
with status UP (replication=true)
4.3. Installing a Microservice Gateway
4.3.安装微服务网关
Now the front-end bit. We’ll create a Microservice Gateway and indicate to it that we have an entity on an existent component for which we want to create the front-end code:
现在是前端部分。我们将创建一个微服务网关,并向它指出我们在一个现有的组件上有一个实体,我们想为它创建前端代码。
# Create a directory for our gateway app
mkdir gateway-app && cd gateway-app
# Runs the JHipster wizard
yo jhipster
Let’s follow the instructions to create an application of type Microservice gateway. We’ll name the application gateway, and select the following options for the other parameters:
让我们按照说明来创建一个微服务网关类型的应用程序。我们将应用程序命名为gateway,并为其他参数选择以下选项。
- port: 8080
- package: com.gateway
- auth: JWT
- service discovery: JHipster Registry
Here is a summary of the complete set of parameters:
以下是一套完整的参数概要。
Let’s move on to entity creation:
让我们继续讨论实体创建。
# Runs entity creation wizard
yo jhipster:entity car
When asked if we want to generate from an existent microservice, choose Yes, then type in the relative path to the car-app root directory (ex.: ../car-app). Finally, when asked if we want to update the entity, choose Yes, regenerate the entity.
当询问我们是否要从现有的微服务中生成时,选择Yes,然后键入car-app根目录的相对路径(例如:./car-app)。最后,当询问我们是否要更新实体时,选择是,重新生成实体。
JHipster will find the Car.json file which is part of the existent Microservice Application we’ve created earlier and will use the metadata contained in that file to create all the necessary UI code for that entity:
JHipster将找到Car.json文件,该文件是我们先前创建的现有微服务应用程序的一部分,并将使用该文件中包含的元数据来为该实体创建所有必要的UI代码。
Found the .jhipster/Car.json configuration file, entity can be automatically generated!
Time to run the gateway-app and test if everything is working:
是时候运行gateway-app并测试一切是否正常。
# Starts up the gateway-app component
./mvnw
Let’s now navigate to http://localhost:8080/ and log in with user admin and password admin. On the top menu, we should see an item Car that will take us to the car list page. All good!
现在让我们导航到http://localhost:8080/,用用户admin和密码admin登录。在顶部菜单上,我们应该看到一个项目Car,它将把我们带到汽车列表页。一切顺利!
4.4. Creating a Second Microservice Application
4.4.创建第二个微服务应用程序
Next, let’s take our system one step further and create a second component of type Microservice Application. This new component will manage car dealers, so we’ll add an entity called the dealer to it.
接下来,让我们把我们的系统更进一步,创建第二个微服务应用类型的组件。这个新组件将管理汽车经销商,所以我们将在其中添加一个名为dealer的实体。
Let’s create a new directory, navigate to it and run the yo jhipster command:
让我们创建一个新的目录,导航到它并运行yo jhipster命令。
mkdir dealer-app && cd dealer-app
yo jhipster
After that, we type in dealerapp as the application’s name and choose port 8082 for it to run (it is critical that this is a different port than the ones we’re using for the jhipster-registry and car-app).
之后,我们输入dealerapp作为应用程序的名称,并选择端口8082让它运行(关键是这个端口与我们用于jhipster-注册表和car-app的不同)。
For the other parameters, we can choose any option we want. Remember this is a separate microservice so that it can use different database types, cache strategy, and tests than the car-app component.
对于其他参数,我们可以选择任何我们想要的选项。记住这是一个独立的微服务,所以它可以使用与汽车-app组件不同的数据库类型、缓存策略和测试。
Let’s add a couple of fields to our dealer entity. For example name and address:
让我们为我们的dealer实体添加几个字段。例如,名称和地址:。
# Runs the create entity wizard
yo jhipster:entity dealer
We shouldn’t forget to navigate to gateway-app and tell it to generate the front-end code for the dealer entity:
我们不应该忘记导航到gateway-app并告诉它为dealer实体生成前端代码。
# Navigate to the gateway-app root directory
cd ../gateway-app
# Runs the create entity wizard
yo jhipster:entity dealer
Finally, run ./mvnw on the dealer-app root directory to start up that component.
最后,在./mvnw上运行dealer-app根目录以启动该组件。
Next, we can visit our gateway application at http://localhost:8080 and refresh the page to see the newly created menu item for the Dealer entity.
接下来,我们可以访问我们的网关应用程序http://localhost:8080,并刷新页面以看到新创建的经销商实体的菜单项。
Before we wrap up, let’s have a look at the jhipster-registry application again at http://localhost:8761/. Click on the Applications menu item to check that all of our three components were successfully identified and registered:
That’s it! We’ve created a sophisticated architecture comprised of one Gateway app with all the front-end code backed by two microservices in just a few minutes.
在我们总结之前,让我们再看看jhipster-registry应用程序,http://localhost:8761/。点击应用程序菜单项,检查我们的三个组件都被成功识别和注册:
这就是了!我们在短短几分钟内就创建了一个由一个Gateway应用程序组成的复杂架构,所有的前端代码都由两个微服务支持。
5. Conclusion
5.结论
Starting a Microservice Architecture project with JHipster is quite easy; we only need to create as many Microservice Applications as we need and one Microservice Gateway and we are ready to go.
使用JHipster开始一个微服务架构项目是非常容易的;我们只需要创建我们需要的微服务应用程序和一个微服务网关,我们就可以开始了。
You can explore the framework further over at the official JHipster website.
您可以在JHipster官方网站上进一步探索该框架。
As always, the codebase for our car-app, dealer-app, and gateway-app are available over on GitHub.
一如既往,我们的汽车应用、经销商应用和网关应用的代码库可在GitHub上找到。