在Ocelot中使用自定义的中间件(一)

2023-11-06 07:32

本文主要是介绍在Ocelot中使用自定义的中间件(一),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

Ocelot是ASP.NET Core下的API网关的一种实现,在微服务架构领域发挥了非常重要的作用。本文不会从整个微服务架构的角度来介绍Ocelot,而是介绍一下最近在学习过程中遇到的一个问题,以及如何使用中间件(Middleware)来解决这样的问题。

问题描述

在上文中,我介绍了一种在Angular站点里基于Bootstrap切换主题的方法。之后,我将多个主题的boostrap.min.css文件放到一个ASP.NET Core Web API的站点上,并用静态文件的方式进行分发,在完成这部分工作之后,调用这个Web API,就可以从服务端获得主题信息以及所对应的样式文件。例如:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

// GET http://localhost:5010/api/themes

{

    "version": "1.0.0",

    "themes": [

        {

            "name": "蔚蓝 (Cerulean)",

            "description": "Cerulean",

            "category": "light",

            "cssMin": "http://localhost:5010/themes/cerulean/bootstrap.min.css",

            "navbarClass": "navbar-dark",

            "navbarBackgroundClass": "bg-primary",

            "footerTextClass": "text-light",

            "footerLinkClass": "text-light",

            "footerBackgroundClass": "bg-primary"

        },

        {

            "name": "机械 (Cyborg)",

            "description": "Cyborg",

            "category": "dark",

            "cssMin": "http://localhost:5010/themes/cyborg/bootstrap.min.css",

            "navbarClass": "navbar-dark",

            "navbarBackgroundClass": "bg-dark",

            "footerTextClass": "text-dark",

            "footerLinkClass": "text-dark",

            "footerBackgroundClass": "bg-light"

        }

    ]

}

当然,整个项目中不仅仅是有这个themes API,还有另外2-3个服务在后台运行,项目是基于微服务架构的。为了能够让前端有统一的API接口,我使用Ocelot作为服务端的API网关,以便为Angular站点提供API服务。于是,我定义了如下ReRoute规则:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

{

    "ReRoutes": [

        {

            "DownstreamPathTemplate": "/api/themes",

            "DownstreamScheme": "http",

            "DownstreamHostAndPorts": [

                {

                    "Host": "localhost",

                    "Port": 5010

                }

            ],

            "UpstreamPathTemplate": "/themes-api/themes",

            "UpstreamHttpMethod": [ "Get" ]

        }

    ]

}

假设API网关运行在http://localhost:9023,那么基于上面的ReRoute规则,通过访问http://localhost:9023/themes-api/themes,即可转发到后台的http://localhost:5010/api/themes,完成API的调用。运行一下,调用结果如下:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

// GET http://localhost:9023/themes-api/themes

{

    "version": "1.0.0",

    "themes": [

        {

            "name": "蔚蓝 (Cerulean)",

            "description": "Cerulean",

            "category": "light",

            "cssMin": "http://localhost:5010/themes/cerulean/bootstrap.min.css",

            "navbarClass": "navbar-dark",

            "navbarBackgroundClass": "bg-primary",

            "footerTextClass": "text-light",

            "footerLinkClass": "text-light",

            "footerBackgroundClass": "bg-primary"

        },

        {

            "name": "机械 (Cyborg)",

            "description": "Cyborg",

            "category": "dark",

            "cssMin": "http://localhost:5010/themes/cyborg/bootstrap.min.css",

            "navbarClass": "navbar-dark",

            "navbarBackgroundClass": "bg-dark",

            "footerTextClass": "text-dark",

            "footerLinkClass": "text-dark",

            "footerBackgroundClass": "bg-light"

        }

    ]

}

看上去一切正常,但是,每个主题设置的css文件地址仍然还是指向下游服务的URL地址,比如上面的cssMin中,还是使用的http://localhost:5010。从部署的角度,外部是无法访问除了API网关以外的其它服务的,于是,这就造成了css文件无法被访问的问题。

解决这个问题的思路很简单,就是API网关在返回response的时候,将cssMin的地址替换掉。如果在Ocelot的配置中加入以下ReRoute设置:

1

2

3

4

5

6

7

8

9

10

11

12

{

  "DownstreamPathTemplate": "/themes/{name}/bootstrap.min.css",

  "DownstreamScheme": "http",

  "DownstreamHostAndPorts": [

    {

      "Host": "localhost",

      "Port": 5010

    }

  ],

  "UpstreamPathTemplate": "/themes-api/theme-css/{name}",

  "UpstreamHttpMethod": [ "Get" ]

}

那么只需要将下游response中cssMin的值(比如http://localhost:5010/themes/cyborg/bootstrap.min.css)替换为Ocelot网关中设置的上游URL(比如http://localhost:9023/themes-api/theme-css/cyborg),然后将替换后的response返回给API调用方即可。这个过程,可以使用Ocelot中间件完成。

使用Ocelot中间件

Ocelot中间件是继承于OcelotMiddleware类的子类,并且可以在Startup.Configure方法中,通过app.UseOcelot方法将中间件注入到Ocelot管道中,然而,简单地调用IOcelotPipelineBuilder的UseMiddleware方法是不行的,它会导致整个Ocelot网关不可用。比如下面的方法是不行的:



这是因为没有将Ocelot的其它Middleware加入到管道中,Ocelot管道中只有ThemeCssMinUrlReplacer中间件。要解决这个问题,我目前的方法就是通过使用扩展方法,将所有Ocelot中间全部注册好,然后再注册自定义的中间件,比如:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

public static IOcelotPipelineBuilder BuildCustomOcelotPipeline(this IOcelotPipelineBuilder builder,

    OcelotPipelineConfiguration pipelineConfiguration)

{

    builder.UseExceptionHandlerMiddleware();

    builder.MapWhen(context => context.HttpContext.WebSockets.IsWebSocketRequest,

        app =>

        {

            app.UseDownstreamRouteFinderMiddleware();

            app.UseDownstreamRequestInitialiser();

            app.UseLoadBalancingMiddleware();

            app.UseDownstreamUrlCreatorMiddleware();

            app.UseWebSocketsProxyMiddleware();

        });

    builder.UseIfNotNull(pipelineConfiguration.PreErrorResponderMiddleware);

    builder.UseResponderMiddleware();

    builder.UseDownstreamRouteFinderMiddleware();

    builder.UseSecurityMiddleware();

    if (pipelineConfiguration.MapWhenOcelotPipeline != null)

    {

        foreach (var pipeline in pipelineConfiguration.MapWhenOcelotPipeline)

        {

            builder.MapWhen(pipeline);

        }

    }

    builder.UseHttpHeadersTransformationMiddleware();

    builder.UseDownstreamRequestInitialiser();

    builder.UseRateLimiting();

 

    builder.UseRequestIdMiddleware();

    builder.UseIfNotNull(pipelineConfiguration.PreAuthenticationMiddleware);

    if (pipelineConfiguration.AuthenticationMiddleware == null)

    {

        builder.UseAuthenticationMiddleware();

    }

    else

    {

        builder.Use(pipelineConfiguration.AuthenticationMiddleware);

    }

    builder.UseClaimsToClaimsMiddleware();

    builder.UseIfNotNull(pipelineConfiguration.PreAuthorisationMiddleware);

    if (pipelineConfiguration.AuthorisationMiddleware == null)

    {

        builder.UseAuthorisationMiddleware();

    }

    else

    {

        builder.Use(pipelineConfiguration.AuthorisationMiddleware);

    }

    builder.UseClaimsToHeadersMiddleware();

    builder.UseIfNotNull(pipelineConfiguration.PreQueryStringBuilderMiddleware);

    builder.UseClaimsToQueryStringMiddleware();

    builder.UseLoadBalancingMiddleware();

    builder.UseDownstreamUrlCreatorMiddleware();

    builder.UseOutputCacheMiddleware();

    builder.UseHttpRequesterMiddleware();

     

    return builder;

}

然后再调用app.UseOcelot即可:

1

2

3

4

5

6

app.UseOcelot((builder, config) =>

{

    builder.BuildCustomOcelotPipeline(config)

    .UseMiddleware<ThemeCssMinUrlReplacer>()

    .Build();

});

这种做法其实听起来不是特别的优雅,但是目前也没找到更合适的方式来解决Ocelot中间件注册的问题。

以下便是ThemeCssMinUrlReplacer中间件的代码,可以看到,我们使用正则表达式替换了cssMin的URL部分,使得css文件的地址可以正确被返回:



执行结果如下:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

// GET http://localhost:9023/themes-api/themes

{

  "version": "1.0.0",

  "themes": [

    {

      "name": "蔚蓝 (Cerulean)",

      "description": "Cerulean",

      "category": "light",

      "cssMin": "http://localhost:9023/themes-api/theme-css/cerulean",

      "navbarClass": "navbar-dark",

      "navbarBackgroundClass": "bg-primary",

      "footerTextClass": "text-light",

      "footerLinkClass": "text-light",

      "footerBackgroundClass": "bg-primary"

    },

    {

      "name": "机械 (Cyborg)",

      "description": "Cyborg",

      "category": "dark",

      "cssMin": "http://localhost:9023/themes-api/theme-css/cyborg",

      "navbarClass": "navbar-dark",

      "navbarBackgroundClass": "bg-dark",

      "footerTextClass": "text-dark",

      "footerLinkClass": "text-dark",

      "footerBackgroundClass": "bg-light"

    }

  ]

}

总结

本文介绍了使用Ocelot中间件实现下游服务response body的替换任务,在ThemeCssMinUrlReplacer的实现代码中,我们使用了context.DownstreamReRoute.DownstreamPathTemplate.Value来判断当前执行的URL是否需要由该中间件进行处理,以避免不必要的中间件逻辑执行。这个设计可以再优化一下,使用一个简单的框架让程序员可以通过Ocelot的配置文件来更为灵活地使用Ocelot中间件,下文介绍这部分内容。

这篇关于在Ocelot中使用自定义的中间件(一)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/355292

相关文章

使用Python删除Excel中的行列和单元格示例详解

《使用Python删除Excel中的行列和单元格示例详解》在处理Excel数据时,删除不需要的行、列或单元格是一项常见且必要的操作,本文将使用Python脚本实现对Excel表格的高效自动化处理,感兴... 目录开发环境准备使用 python 删除 Excphpel 表格中的行删除特定行删除空白行删除含指定

深入理解Go语言中二维切片的使用

《深入理解Go语言中二维切片的使用》本文深入讲解了Go语言中二维切片的概念与应用,用于表示矩阵、表格等二维数据结构,文中通过示例代码介绍的非常详细,需要的朋友们下面随着小编来一起学习学习吧... 目录引言二维切片的基本概念定义创建二维切片二维切片的操作访问元素修改元素遍历二维切片二维切片的动态调整追加行动态

prometheus如何使用pushgateway监控网路丢包

《prometheus如何使用pushgateway监控网路丢包》:本文主要介绍prometheus如何使用pushgateway监控网路丢包问题,具有很好的参考价值,希望对大家有所帮助,如有错误... 目录监控网路丢包脚本数据图表总结监控网路丢包脚本[root@gtcq-gt-monitor-prome

SpringBoot+EasyExcel实现自定义复杂样式导入导出

《SpringBoot+EasyExcel实现自定义复杂样式导入导出》这篇文章主要为大家详细介绍了SpringBoot如何结果EasyExcel实现自定义复杂样式导入导出功能,文中的示例代码讲解详细,... 目录安装处理自定义导出复杂场景1、列不固定,动态列2、动态下拉3、自定义锁定行/列,添加密码4、合并

Python通用唯一标识符模块uuid使用案例详解

《Python通用唯一标识符模块uuid使用案例详解》Pythonuuid模块用于生成128位全局唯一标识符,支持UUID1-5版本,适用于分布式系统、数据库主键等场景,需注意隐私、碰撞概率及存储优... 目录简介核心功能1. UUID版本2. UUID属性3. 命名空间使用场景1. 生成唯一标识符2. 数

SpringBoot中如何使用Assert进行断言校验

《SpringBoot中如何使用Assert进行断言校验》Java提供了内置的assert机制,而Spring框架也提供了更强大的Assert工具类来帮助开发者进行参数校验和状态检查,下... 目录前言一、Java 原生assert简介1.1 使用方式1.2 示例代码1.3 优缺点分析二、Spring Fr

Android kotlin中 Channel 和 Flow 的区别和选择使用场景分析

《Androidkotlin中Channel和Flow的区别和选择使用场景分析》Kotlin协程中,Flow是冷数据流,按需触发,适合响应式数据处理;Channel是热数据流,持续发送,支持... 目录一、基本概念界定FlowChannel二、核心特性对比数据生产触发条件生产与消费的关系背压处理机制生命周期

java使用protobuf-maven-plugin的插件编译proto文件详解

《java使用protobuf-maven-plugin的插件编译proto文件详解》:本文主要介绍java使用protobuf-maven-plugin的插件编译proto文件,具有很好的参考价... 目录protobuf文件作为数据传输和存储的协议主要介绍在Java使用maven编译proto文件的插件

SpringBoot线程池配置使用示例详解

《SpringBoot线程池配置使用示例详解》SpringBoot集成@Async注解,支持线程池参数配置(核心数、队列容量、拒绝策略等)及生命周期管理,结合监控与任务装饰器,提升异步处理效率与系统... 目录一、核心特性二、添加依赖三、参数详解四、配置线程池五、应用实践代码说明拒绝策略(Rejected

C++ Log4cpp跨平台日志库的使用小结

《C++Log4cpp跨平台日志库的使用小结》Log4cpp是c++类库,本文详细介绍了C++日志库log4cpp的使用方法,及设置日志输出格式和优先级,具有一定的参考价值,感兴趣的可以了解一下... 目录一、介绍1. log4cpp的日志方式2.设置日志输出的格式3. 设置日志的输出优先级二、Window