net core 网关 初使用(1)

匿名 (未验证) 提交于 2019-12-02 23:43:01

新建 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();     } }  

效果


文章来源: https://blog.csdn.net/jiegemena/article/details/92562870
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!