• 使用ocelot作为api网关


    • 新建网站项目然后添加ocelot 的nuget包
    • 新建ocelot.json的网关的配置文件
      {
        "GlobalConfiguration": {
          "BaseUrl": "https://api.mybusiness.com"
        },
        "ReRoutes": [
          {
            "DownstreamPathTemplate": "/api/values",
            "DownstreamScheme": "http",
            "DownstreamHostAndPorts": [
              {
                "Host": "localhost",
                "Port": 60907
              }
            ],
            "UpstreamPathTemplate": "/users",
            "UpstreamHttpMethod": [ "Get" ],
            "AuthenticationOptions": {
              "AuthenticationProviderKey": "finbook",
              "AllowedScopes": []
            }
          },
          {
            "DownstreamPathTemplate": "/connect/token",
            "DownstreamScheme": "http",
            "DownstreamHostAndPorts": [
              {
                "Host": "localhost",
                "Port": 52619
              }
            ],
            "UpstreamPathTemplate": "/connect/token",
            "UpstreamHttpMethod": [ "Post" ]
          }
        ]
      }
    • 添加配置文件进入netcore管道
          public class Program
          {
              public static void Main(string[] args)
              {
                  BuildWebHost(args).Run();
              }
      
              public static IWebHost BuildWebHost(string[] args) =>
                  WebHost.CreateDefaultBuilder(args)
                       .ConfigureAppConfiguration((hostingContext, builder) =>
                       {
                           builder
                           .SetBasePath(hostingContext.HostingEnvironment.ContentRootPath)
                           .AddJsonFile("Ocelot.json");
                       })
                      .UseStartup<Startup>()
                      .Build();
          }
             public void ConfigureServices(IServiceCollection services)
              {
                  services.AddOcelot();
              }
    • 集成identityserver
        services.AddAuthentication()
         .AddIdentityServerAuthentication(authenticationProviderKey, options=> {
                          options.Authority = "http://localhost:52619/";
                          options.ApiName = "geteway_api";
                          options.SupportedTokens = SupportedTokens.Both;
                          options.ApiSecret = "secret";
                          options.RequireHttpsMetadata = false;//是否是https请求
                      });
    • 参考自大佬文章http://www.jessetalk.cn/2018/03/19/net-core-apigateway-ocelot-docs/
  • 相关阅读:
    阿里云中挖矿病毒
    flutter 返回刷新页面
    PM2 常用命令
    阿里云Redis 配置
    stm32f407启动文件分析
    C++类的前置声明
    Qt快速入门学习笔记(画图篇)
    Qt快速入门学习笔记(基础篇)
    Qt入门实例
    Qt编码设置
  • 原文地址:https://www.cnblogs.com/chongyao/p/10436927.html
Copyright © 2020-2023  润新知