ocelot网关 jwt_使用ASP.NET Core,Ocelot,MongoDB和JWT的微服务

ocelot网关 jwt

目录 (Table of Contents)

  1. Introduction

    介绍

  2. Development Environment

    开发环境

  3. Technologies

    技术领域

  4. Architecture

    建筑

    1. Microservices

      微服务

    2. API Gateways

      API网关

    3. Client Apps

      客户端应用

    Source Code

    源代码

  5. Unit Tests

    单元测试

  6. Monitoring using Health Checks

    使用运行状况检查进行监视

  7. How to Run the Application

    如何运行应用程序

  8. How to Deploy the Application

    如何部署应用程序

  9. References

    参考文献

  10. History

    历史

介绍 (Introduction)

Image 1

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

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

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

源代码 (Source Code)

Image 5
  • 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>(CatalogItem.DocumentNam
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值