net core Ocelot 網關 初使用(1)

新建 net core webapi 項目


項目文件

安裝 nuget 包


```

Install-Package Ocelot

```

配置


  • 添加一個Ocelot.json的文件用來添加Ocelot的配置

  • 轉發 http://localhost:53743/jsmer => http://localhost:8088

    {
    "GlobalConfiguration": {
        "BaseUrl": "http://localhost:53743"
    },
    "ReRoutes": [
        {
        "DownstreamPathTemplate": "/{postId}",
        "DownstreamScheme": "http",
        "DownstreamHostAndPorts": [
            {
            "Host": "localhost",
            "Port": 8088
            }
        ],
        "UpstreamPathTemplate": "/jsmer/{postId}",
        "UpstreamHttpMethod": [ "Get" ]
        }
    ]
    }
    
  • 說明

    Downstream是下游服務配置
    UpStream是上游服務配置
    Aggregates 服務聚合配置
    ServiceName, LoadBalancer, UseServiceDiscovery 配置服務發現
    AuthenticationOptions 配置服務認證
    RouteClaimsRequirement 配置Claims鑑權
    RateLimitOptions爲限流配置
    FileCacheOptions 緩存配置
    QosOptions 服務質量與熔斷
    DownstreamHeaderTransform頭信息轉發

添加中間件


Startup.cs

  1. 代碼如下
 using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
// 添加
using Ocelot.DependencyInjection;
// 添加
using Ocelot.Middleware;

namespace JSClound.Gateway
{
    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
       		 // 添加
            services.AddOcelot();
            
            services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
        }

        // 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.UseMvc();
			// 添加
            app.UseOcelot().Wait();
        }
    }
}
 }

Program.cs

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 JSClound.Gateway
{
    public class Program
    {
        public static void Main(string[] args)
        {
            BuildWebHost(args).Run();
        }

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

效果


在這裏插入圖片描述

在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章