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

效果


在这里插入图片描述

在这里插入图片描述

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值