ocelot网关 jwt
目录 (Table of Contents)
介绍 (Introduction)
![Image 1](https://i-blog.csdnimg.cn/blog_migrate/008b1d74475321bacd471f4aa3751a2d.png)
A microservices architecture consists of a collection of small, independent, and loosely coupled services. Each service is self-contained, implements a single business capability, is responsible for persisting its own data, is a separate codebase, and can be deployed independently.
微服务架构由一组小型,独立和松散耦合的服务组成。 每个服务都是独立的,实现单个业务功能,负责持久保存自己的数据,是一个单独的代码库,并且可以独立部署。
API gateways are entry points for clients. Instead of calling services directly, clients call the API gateway, which forwards the call to the appropriate services.
API网关是客户端的入口点。 客户端不是直接调用服务,而是调用API网关,该网关将调用转发到适当的服务。
There are multiple advantages using microservices architecture:
使用微服务架构有多个优点:
- Developers can better understand the functionality of a service. 开发人员可以更好地了解服务的功能。
- Failure in one service does not impact other services. 一种服务的故障不会影响其他服务。
- It's easier to manage bug fixes and feature releases. 管理错误修复和功能发布更加容易。
- Services can be deployed in multiple servers to enhance performance. 可以将服务部署在多台服务器中以提高性能。
- Services are easy to change and test. 服务易于更改和测试。
- Services are easy and fast to deploy. 服务易于部署。
- Allows to choose technology that is suited for a particular functionality. 允许选择适合特定功能的技术。
Before choosing microservices architecture, here are some challenges to consider:
在选择微服务架构之前,需要考虑以下挑战:
- Services are simple but the entire system as a whole is more complex. 服务很简单,但是整个系统整体来说更复杂。
- Communication between services can be complex. 服务之间的通信可能很复杂。
- More services equals more resources. 更多服务等于更多资源。
- Global testing can be difficult. 全局测试可能很困难。
- Debugging can be harder. 调试可能会更困难。
Microservices architecture is great for large companies, but can be complicated for small companies who need to create and iterate quickly, and don't want to get into complex orchestration.
微服务架构对于大型公司而言非常有用,但对于需要快速创建和迭代且不想参与复杂编排的小型公司而言,它可能会变得复杂。
This article shows a working sample of microservices architecture using ASP.NET Core, Ocelot, MongoDB and JWT.
本文显示了一个使用ASP.NET Core,Ocelot,MongoDB和JWT的微服务体系结构的工作示例。
This article covers how to create microservices using ASP.NET Core, how to create API gateways using Ocelot, how to create repositories using MongoDB, how to handle JWT in microservices, how to unit test microservices using xUnit and Moq, how to monitor microservices using health checks, and finally how to deploy microservices using Docker containers on Linux distributions.
本文介绍如何使用ASP.NET Core创建微服务,如何使用Ocelot创建API网关,如何使用MongoDB创建存储库,如何在微服务中处理JWT,如何使用xUnit和Moq对单元进行微服务测试,如何使用来监视微服务运行状况检查,最后是如何在Linux发行版上使用Docker容器部署微服务。
Microservices and gateways are developed using ASP.NET Core and C#. Client apps are developed using HTML and JavaScript for sake of simplicity.
微服务和网关是使用ASP.NET Core和C#开发的。 为了简单起见,客户端应用程序是使用HTML和JavaScript开发的。
开发环境 (Development Environment)
- Visual Studio 2019 Visual Studio 2019
- .NET Core 3.1 .NET Core 3.1
- MongoDB MongoDB
- Postman 邮差
技术领域 (Technologies)
- C# C#
- ASP.NET Core ASP.NET核心
- Ocelot 豹猫
- Swashbuckle 十字花
- Serilog 塞里洛格
- JWT 智威汤逊
- MongoDB MongoDB
- xUnit 单位
- Moq 起订量
- HTML HTML
- CSS CSS
- JavaScript JavaScript
建筑 (Architecture)
![Image 2](https://i-blog.csdnimg.cn/blog_migrate/385a1036a1a7d195cd106f9230e0fe6b.jpeg)
There are three microservices:
有三种微服务:
Catalog microservice: allows to manage the catalog.
目录微服务 :允许管理目录。
Cart microservice: allows to manage the cart.
购物车微服务 :允许管理购物车。
Identity microservice: allows to manage users.
身份微服务 :允许管理用户。
Each microservice implements a single business capability and has its own MongoDB database.
每个微服务都实现单个业务功能,并拥有自己的MongoDB数据库。
There are two API gateways, one for the front end and one for the back end.
有两个API网关,一个用于前端,一个用于后端。
Below is the front end API gateway:
以下是前端API网关:
GET /catalog: retrieves catalog items.
GET / catalog :检索目录项。
GET /catalog/{id}: retrieves a catalog item.
GET / catalog / {id} :检索目录项。
GET /cart: retrieves cart items.
GET / cart :检索购物车项目。
POST /cart: adds a cart item.
POST / cart :添加购物车项目。
PUT /cart: updates a cart item.
PUT / cart :更新购物车项目。
DELETE /cart: deletes a cart item.
DELETE / cart :删除购物车项目。
POST /identity/login: performs a login.
POST / identity / login :执行登录。
POST /identity/register: registers a user.
POST / identity / register :注册用户。
GET /identity/validate: validates a JWT token.
GET / identity / validate :验证JWT令牌。
Below is the back end API gateway:
以下是后端API网关:
GET /catalog: retrieves catalog items.
GET / catalog :检索目录项。
GET /catalog/{id}: retrieves a catalog item.
GET / catalog / {id} :检索目录项。
POST /catalog: creates a catalog item.
POST / catalog :创建目录项。
PUT /catalog: updates a catalog item.
PUT / catalog :更新目录项。
DELETE /catalog/{id}: deletes a catalog item.
DELETE / catalog / {id} :删除目录项。
POST /identity/login: performs a login.
POST / identity / login :执行登录。
GET /identity/validate: validates a JWT token.
GET / identity / validate :验证JWT令牌。
Finally, there are two client apps. A front end for accessing the store and a back end for managing the store.
最后,有两个客户端应用程序。 用于访问商店的前端和用于管理商店的后端。
The front end allows registered users to see the available catalog items, allows to add catalog items to the cart, and allows to remove catalog items from the cart.
前端允许注册用户查看可用的目录项,允许将目录项添加到购物车,并允许从购物车中删除目录项。
Here is a screenshot of the store page in the front end:
这是前端商店页面的屏幕截图:
![Image 3](https://i-blog.csdnimg.cn/blog_migrate/ecc13f94cba5ae5e8d64be01c56b0120.jpeg)
The back end allows admin users to see the available catalog items, allows to add new catalog items, allows to update catalog items, and allows to remove catalog items.
后端允许管理员用户查看可用的目录项,允许添加新的目录项,允许更新目录项,并允许删除目录项。
Here is a screenshot of the store page in the back end:
这是后端商店页面的屏幕截图:
![Image 4](https://i-blog.csdnimg.cn/blog_migrate/eda601429551161a0ebb6b902cd6f549.jpeg)
源代码 (Source Code)
![Image 5](https://i-blog.csdnimg.cn/blog_migrate/93e1a4520a00474ff11c788d4a108b8d.jpeg)
CatalogMicroservice
project contains the source code of the microservice managing the catalog.CatalogMicroservice
项目包含管理目录的微服务的源代码。CartMicroservice
project contains the source code of the microservice managing the cart.CartMicroservice
项目包含管理购物车的微服务的源代码。IdentityMicroservice
project contains the source code of the microservice managing users.IdentityMicroservice
项目包含管理用户的微服务的源代码。Middleware
project contains the source code of common functionalities used by microservices.Middleware
项目包含微服务使用的通用功能的源代码。FrontendGateway
project contains the source code of the front end API gateway.FrontendGateway
项目包含前端API网关的源代码。BackendGateway
project contains the source code of the back end API gateway.BackendGateway
项目包含后端API网关的源代码。Frontend
project contains the source code of the front end client app.Frontend
项目包含前端客户端应用程序的源代码。Backend
project contains the source code of the back end client app.Backend
项目包含Backend
客户端应用程序的源代码。test
solution folder contains unit tests of all microservices.test
解决方案文件夹包含所有微服务的单元测试。
You can also find the source code on GitHub.
您还可以在GitHub上找到源代码。
微服务 (Microservices)
Let's start with the simplest microservice, CatalogMicroservice
.
让我们从最简单的微服务开始,即CatalogMicroservice
。
CatalogMicroservice
is responsible of managing the catalog.
CatalogMicroservice
负责管理目录。
Below is the model used by CatalogMicroservice
:
以下是CatalogMicroservice
使用的模型:
public class CatalogItem
{
public static readonly string DocumentName = "catalogItems";
public Guid Id { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public decimal Price { get; set; }
}
Below is the repository interface:
下面是存储库界面:
public interface ICatalogRepository
{
List<CatalogItem> GetCatalogItems();
CatalogItem GetCatalogItem(Guid catalogItemId);
void InsertCatalogItem(CatalogItem catalogItem);
void UpdateCatalogItem(CatalogItem catalogItem);
void DeleteCatalogItem(Guid catalogItemId);
}
Below is the repository:
以下是存储库:
public class CatalogRepository : ICatalogRepository
{
private readonly IMongoCollection<CatalogItem> _col;
public CatalogRepository(IMongoDatabase db)
{
_col = db.GetCollection<CatalogItem>(Catalog