代码改变世界

.net core使用ocelot---第一篇 简单使用

2019-08-08 14:37  一截生长  阅读(6222)  评论(2编辑  收藏  举报

简介原文地址

  接下来你会学习,基于asp.net core 用Ocelot实现一个简单的API网关。或许你会疑问什么是API网关,我们先看下面的截图

  

  API网关是访问你系统的入口,它包括很多东西,比如路由(Routing),身份验证(Authentication),服务发现(Service discovery),日志(Logging ),等等。

Ocelot

     Ocelot提供统一的访问入口,适用于.net开发的微服务或者开发的面向服务架构,可以访问Ocelot获得更多信息。

     我会用Ocelot实现一个简单的例子。

Step1

    先创建三个项目,如下所示。

项目名称

项目类型

描述

APIGateway

ASP.NET Core Empty

Demo的入口

CustomersAPIServices

ASP.NET Core Web API

API Service消费者相关操作

ProductsAPIServices

ASP.NET Core Web API

API Service 产品相关操作

 

 

Step2

     创建两个API services,在CustimersAPIServices项目中创建CustomersController。

[Route("api/[controller]")]  
public class CustomersController : Controller  
{          
    [HttpGet]  
    public IEnumerable<string> Get()  
    {  
        return new string[] { "Catcher Wong", "James Li"     };  
    }  
  
    [HttpGet("{id}")]  
    public string Get(int id)  
    {  
        return $"Catcher Wong - {id}";  
    }              
}  

 

 为了确定CustimersAPIServices的应用URL,我们应该在项目的类中添加UseUrls

public static IWebHost BuildWebHost(string[] args) =>  
        WebHost.CreateDefaultBuilder(args)  
            .UseStartup<Startup>()  
            .UseUrls("http://localhost:9001")  
            .Build();  

 

 在PriductsAPIServices项目中新建ProductsController

[Route("api/[controller]")]  
public class ProductsController : Controller  
{  
    [HttpGet]  
    public IEnumerable<string> Get()  
    {  
        return new string[] { "Surface Book 2", "Mac Book Pro" };  
    }  
}

 

  

 同样在项目的类中添加UseUrls

 

public static IWebHost BuildWebHost(string[] args) =>  
        WebHost.CreateDefaultBuilder(args)  
            .UseStartup<Startup>()  
            .UseUrls("http://localhost:9002")  
            .Build(); 

 

注意

    你可以通过项目的属性对应用的URL进行配置。

  

Step3

    运行CustimersAPIServices 和ProductsAPIServices。打开两个cmd终端,cd到两个服务的文件夹位置,输入 "dotnet run" 启动两个项目。

 运行成功如下所示。

Step4

    接下来我们新建 APIGateway项目,首先安装Ocelot安装包。

Install-Package Ocelot

     安装成功后,如下图所示。

 

Step5

       在项目下新建configuration.json如下所示。

{  
    "ReRoutes": [  
        {  
            "DownstreamPathTemplate": "/api/customers",  
            "DownstreamScheme": "http",  
            "DownstreamHost": "localhost",  
            "DownstreamPort": 9001,  
            "UpstreamPathTemplate": "/customers",  
            "UpstreamHttpMethod": [ "Get" ]  
        },  
        {  
            "DownstreamPathTemplate": "/api/customers/{id}",  
            "DownstreamScheme": "http",  
            "DownstreamHost": "localhost",  
            "DownstreamPort": 9001,  
            "UpstreamPathTemplate": "/customers/{id}",  
            "UpstreamHttpMethod": [ "Get" ]  
        },  
        {  
            "DownstreamPathTemplate": "/api/products",  
            "DownstreamScheme": "http",  
            "DownstreamPort": 9002,  
            "DownstreamHost": "localhost",  
            "UpstreamPathTemplate": "/api/products",  
            "UpstreamHttpMethod": [ "Get" ]  
        }  
    ],  
    "GlobalConfiguration": {  
        "RequestIdKey": "OcRequestId",  
        "AdministrationPath": "/administration"  
    }  
}  

  该文件是API网关的配置文件,包括两部分,ReRoutes和GlobalConfiguration。

  ReRoutes是告诉Ocelot如何操作上游的request请求,

  GlobalConfiguration有点黑客的感觉,允许对ReRoutes的设置进行重写。

  用下面的片段介绍ReRoutes。

{  
    "DownstreamPathTemplate": "/api/customers/{id}",  
    "DownstreamScheme": "http",  
    "DownstreamHost": "localhost",  
    "DownstreamPort": 9001,  
    "UpstreamPathTemplate": "/customers/{id}",  
    "UpstreamHttpMethod": [ "Get" ]  
}  

 

  以Downstream开头的项意味我们的请求会指向http://localhost:9001/api/customers/{id}

  以Upstream开头的项意味我们应该使用/customers/{id} 的HTTP Get请求去访问服务。

Step6

       修改Startup类,使用Ocelot。

public class Startup  
{  
    public Startup(IHostingEnvironment env)  
    {  
        var builder = new Microsoft.Extensions.Configuration.ConfigurationBuilder();  
        builder.SetBasePath(env.ContentRootPath)      
               //add configuration.json  
               .AddJsonFile("configuration.json", optional: false, reloadOnChange: true)  
               .AddEnvironmentVariables();  
  
        Configuration = builder.Build();  
    }  
  
    //change  
    public IConfigurationRoot Configuration { get; }  
  
    public void ConfigureServices(IServiceCollection services)  
    {  
        Action<ConfigurationBuilderCachePart> settings = (x) =>  
        {  
            x.WithMicrosoftLogging(log =>  
            {  
                log.AddConsole(LogLevel.Debug);  
  
            }).WithDictionaryHandle();  
        };  
        services.AddOcelot(Configuration, settings);  
    }  
      
    //don't use Task here  
    public async void Configure(IApplicationBuilder app, IHostingEnvironment env)  
    {  
        await app.UseOcelot();  
    }  
}  

 

  别忘了添加上面的configuration.json文件。

Step7

       这一步至关重要,用来配置Ocelot。

     我们新建IWebHostBuilder的新实例,不要使用var!!!

public class Program  
{  
    public static void Main(string[] args)  
    {  
        IWebHostBuilder builder = new WebHostBuilder();  
        builder.ConfigureServices(s =>  
        {  
            s.AddSingleton(builder);  
        });  
        builder.UseKestrel()  
               .UseContentRoot(Directory.GetCurrentDirectory())  
               .UseStartup<Startup>()  
               .UseUrls("http://localhost:9000");  
  
        var host = builder.Build();  
        host.Run();  
    }  
}  

  同样我们需要指明应用的URL。

Step8

     启动APIGateway,使用cmd通过dotnet run 命令。启动成功后,输入http://localhost:9000

  当我们通过客户端访问http://localhost:9000/api/products真实的路由是http://localhost:9002/api/products

       当我们访问http://localhost:9000/customers,真实的路由http://localhost:9001/api/customers

      当我们访问http://localhost:9000/customers/1, 真实的路由是http://localhost:9001/api/customers/1

源码:APIGatewayDemo

百度网盘

链接:https://pan.baidu.com/s/17sqfGcYx8yEHRL_LwKAUlA
提取码:p3d0

总结

       这篇文章介绍了通过Ocelot创建API网关。希望可以帮到你。

     由于只是简单的示例代码,Ocelot许多重要的特性比如服务发现,身份验证,服务质量(qos),未在示例中体现。