我的第一个微服务系列(一):使用Ocelot搭建网关

一、引言

  Ocelot是一个网关,用来为.Net面向微服务架构提供统一的入口,它功能强大,包括了:路由、请求聚合、服务发现、认证、鉴权、限流熔断、并内置了负载均衡器与Service Fabric、Butterfly Tracing集成。这些功能只都只需要简单的配置即可完成。本篇不做官方文档的翻译介绍,需要了解的请自行阅读官方文档  https://ocelot.readthedocs.io/en/latest/introduction/bigpicture.html ,或者参考博客园文章 https://www.cnblogs.com/jesse2013/p/net-core-apigateway-ocelot-docs.html 。本篇介绍在.Net Core 2.2集成Ocelot,实现我们的Api GateWay。

二、配置Ocelot

  Ocelot仅适用于.NET Core,目前是为netstandard2.0构建的。因此,使用Ocelot必须是netstandard2.0以上的项目。所以新建一个.Net Core 2.2 API 项目,引入Nuget包

Install-Package Ocelot -Version 13.5.2

  Ocelot使用配置文件来配置路由、服务聚合、服务发现、认证、鉴权、限流、熔断、缓存、Header头传递等信息,在项目中定义一个ocelot.json文件,基本的配置如下。

{
    "ReRoutes": [],
    "GlobalConfiguration": {
        "BaseUrl": "https://localhost"
    }
}

  ReRoutes 将用来配置路由,需要注意的是 BaseUrl,Ocelot需要知道它正在运行的URL,以便进行Header查找和替换以及某些管理配置。所以BaseUrl是我们外部暴露的Url,如果您正在运行容器,Ocelot可能会在网址http://123.12.1.1:6543上运行,但是如果使用了类似nginx绑定了域名 https://api.mybusiness.com,在这种情况下,Ocelot基本网址应为https://api.mybusiness.com。

  这里我们在本地运行,所以使用了localhost,在Program.cs中把端口指定为80,并且将ocelot.json添加到配置中。

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;

namespace Gateway.Api
{
    public class Program
    {
        public static void Main(string[] args)
        {
            CreateWebHostBuilder(args)
                .UseUrls("http://+:80")
                .Build()
                .Run();
        }

        public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
            WebHost.CreateDefaultBuilder(args)
                .ConfigureAppConfiguration((webhost,builder)=>
                {
                    builder.SetBasePath(webhost.HostingEnvironment.ContentRootPath)
                    .AddJsonFile("Ocelot.json");
                })
                .UseStartup<Startup>();
    }
}

  接着需要将ocelot注入到项目并且添加到中间件中

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using Ocelot.DependencyInjection;
using Ocelot.Middleware;
using IdentityServer4.AccessTokenValidation;

namespace Gateway.Api
{
    public class Startup
    {
        // This method gets called by the runtime. Use this method to add services to the container.
        // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddOcelot();
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            app.UseOcelot();
        }
    }
}
原文地址:https://www.cnblogs.com/jesen1315/p/11493140.html