微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Asp.Net Core Ocelot

Asp.Net Core Ocelot

1.1 Ocelot简介

Ocelot是一个用.NET Core实现并且开源的API网关技术,它的功能包括了:路由、请求聚合、服务发现、认证、鉴权、限流熔断、并内置了负载均衡器、Service Fabric、Skywalking等的集成。而且这些功能都只需要简单的配置即可完成。@H_404_5@

(1)配置说明@H_404_5@

  • Routes - 告诉Ocelot如何处理上游的请求
  • GlobalConfiguration - 全局配置,此节点的配置允许覆盖Routes里面的配置,你可以在这里进行通用的一些配置信息
  • Downstream - 下游服务配置
  • UpStream - 上游服务配置
  • Aggregates - 服务聚合配置
  • ServiceName, LoadBalancer, UseServicediscovery - 配置服务发现
  • Authenticationoptions - 配置服务认证
  • RouteClaimsRequirement - 配置Claims鉴权
  • RateLimitOptions - 限流配置
  • FileCacheOptions - 缓存配置
  • QosOptions - 服务质量与熔断
  • DownstreamHeaderTransform - 头信息转发

1.2 Asp.Net Core使用Ocelot

(1)新建Peng.Core.ApiGateWay,Install-Package Ocelot@H_404_5@

这里还是使用Apollo远程配置@H_404_5@

  <ItemGroup>
    <packagereference Include="Ocelot" Version="18.0.0" />
	<packagereference Include="Com.Ctrip.Framework.Apollo.Configuration" Version="2.6.2" />
    <packagereference Include="Swashbuckle.AspNetCore" Version="6.2.3" />
  </ItemGroup>

(2)注入Ocelot,这里还需要添加Cors@H_404_5@


builder.Services.AddOcelot(_configuration);

app.USEOcelot().Wait();

image-20220402142432000@H_404_5@@H_404_5@

(3)配置网关,服务A,服务B启动的IP@H_404_5@

image-20220402142603679@H_404_5@@H_404_5@

(4)配置Apollo@H_404_5@

新建一个NameSpace@H_404_5@

image-20220402142658861@H_404_5@@H_404_5@

ocelotconfig.json@H_404_5@

{
    "GlobalConfiguration": {
      "BaseUrl": "https://localhost:9000/"
    },
    "Routes": [
      {
        "DownstreamScheme": "https",
        "DownstreamHostAndPorts": [
          {
            "Host": "localhost",
            "Port": "9001"
          }
        ],
        "UpstreamPathTemplate": "/api/a/v1/{controller}",
        "DownstreamPathTemplate": "/api/a/v1/{controller}",
        "UpstreamHttpMethod": [
          "GET",
          "POST",
          "DELETE",
          "PUT",
          "OPTIONS"
        ],
       
        "UseServicediscovery": false,
        "LoadBalancerOptions": {
          "Type": "RoundRobin"
        }
      },
      {
        "DownstreamScheme": "https",
        "DownstreamHostAndPorts": [
          {
            "Host": "localhost",
            "Port": "9002"
          }
        ],
        "UpstreamPathTemplate": "/api/b/v1/{controller}",
        "DownstreamPathTemplate": "/api/b/v1/{controller}",
        "UpstreamHttpMethod": [
          "GET",
          "POST",
          "DELETE",
          "PUT",
          "OPTIONS"
        ],
        
        "UseServicediscovery": false,
        "LoadBalancerOptions": {
          "Type": "RoundRobin"
        }
      }
    ]
  }

appsetting.json配置Apollo,这里三个服务配置都一样@H_404_5@

{
  "Apollo": {
    "AppId": "Peng.Core.Service",
    "Env": "DEV",
    "Namespaces": [ "Shared.json", "ocelotconfig.json" ],
    "MetaServer": "http://192.168.188.180:8080",
    "configserver": [ "http://192.168.188.180:8080/" ]
  }
}

image-20220402142841952@H_404_5@@H_404_5@

(5)通过网关的IP访问接口@H_404_5@

image-20220402150413322@H_404_5@@H_404_5@

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐