ocelot其它部分

权限认证

目前基本解决了结构问题。官网还提供了认证集成。

官网文档:身份验证 — Ocelot 1.0.0 文档

目前网上有很多介绍集成IdentityServer4,已经很成熟。

介绍几篇博客

Consul+Ocelot搭建微服务实践--IdentityServer集成_Jonny Lin的博客-CSDN博客

Ocelot简易教程(五)之集成IdentityServer认证以及授权 - 依乐祝 - 博客园 (cnblogs.com)

.NET Core微服务之基于Ocelot+IdentityServer实现统一验证与授权_dotNET跨平台的博客-CSDN博客

对应IdentityServer4是新版

需要在服务项目nuget Duende.IdentityServer


builder.Services.AddIdentityServer(options =>
{
    options.EmitStaticAudienceClaim = true;
})
    .AddInMemoryIdentityResources(Config.GetIdentityResources())
    .AddInMemoryApiScopes(Config.ApiScopes)
    .AddInMemoryClients(Config.GetClients());
 public class Config
    {
        public static IEnumerable<ApiResource> GetApiResources()
        {
            return new List<ApiResource>()
            {
                new ApiResource("api", "My Api"){ Scopes ={"api"} }
            };
        }

        public static IEnumerable<Client> GetClients()
        {
            return new List<Client>()
            {
                new Client()
                {
                    ClientId = "client",
                    AllowedGrantTypes = GrantTypes.ResourceOwnerPasswordAndClientCredentials,
                     AccessTokenType = AccessTokenType.Reference,
                    ClientSecrets =
                    {
                        new Secret("123456".Sha256())
                    },
                    AllowedScopes =
                    {
                        "api"
                    },
                  Claims =new List<ClientClaim>()
                  {
                   new ClientClaim(IdentityModel.JwtClaimTypes.Role,"admin"),
                    new ClientClaim(IdentityModel.JwtClaimTypes.NickName,"江北"),
                     new ClientClaim("Email","**********@163.com"),
               }
                }
            };
        }

        public static IEnumerable<ApiScope> ApiScopes =>
            new ApiScope[] { new ApiScope("api") };

        public static IEnumerable<IdentityResource> GetIdentityResources()
        {
            return new List<IdentityResource>
            {
                new IdentityResources.OpenId(),
                new IdentityResources.Profile()
            };
        }

        public static IEnumerable<TestUser> Users()
        {
            return new[]
            {
                new TestUser
                {
                    SubjectId = "",
                    Username = "mail@qq.com",
                    Password = "password"
                }
            };
        }
    }

ocelot也需要引入Duende.IdentityServer

//添加鉴权认证
builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
                .AddJwtBearer("AuthKey", options =>
                {
                    //鉴权中心服务地址
                    options.Authority = "https://localhost:19138";
                    options.RequireHttpsMetadata = false;
                    options.Audience = "api";
                    options.TokenValidationParameters = new TokenValidationParameters
                    {
                        ValidateAudience = false
                    };
                });

其实和IdentityServer4集成一样,只是库不同。

Polly熔断

Polly是一种开源的.NET弹性和瞬态故障处理库,允许我们以非常顺畅和线程安全的方式来执诸如行重试,断路,超时,故障恢复等策略。

Polly主要功能

重试(Retry)

断路器(Circuit-breaker)

超时检测(Timeout)

缓存(Cache)

降级(FallBack)

在ocelot项目中引入Ocelot.Provider.Polly

添加

builder.Services.AddOcelot(builder.Configuration).AddConsul().AddPolly(); //加 Consul服务 polly

其他信息推荐一篇文章

Ocelot 学习(二) Polly - CRUDEngineer - 博客园 (cnblogs.com)

我的配置

{
  "Serilog": {
    "MinimumLevel": {
      "Default": "Information",
      "Override": {
        "Microsoft": "Information",
        "System": "Information"
      }
    },
    "WriteTo": [
      { "Name": "Console" },
      {
        "Name": "File",
        "Args": { "path": "C:\\LogFiles\\log.txt" }
      }
    ],
    "Enrich": [ "FromLogContext", "WithMachineName", "WithThreadId" ]
  },
  "Routes": [
    {
      "UseServiceDiscovery": true,
      "DownstreamPathTemplate": "/api/{url}",
      "DownstreamScheme": "http",
      "ServiceName": "homepage",
      "LoadBalancerOptions": {
        "Type": "LeastConnection"
      },
      "UpstreamPathTemplate": "/ss/{url}",
      "UpstreamHttpMethod": [ "Get", "Post" ],
      // "ReRoutesCaseSensitive": false, // non case sensitive
      //"RateLimitOptions": {
      //  "ClientWhitelist": [], //白名单
      //  "EnableRateLimiting": true,
      //  "Period": "5m",     // 1s,5m,1h,1d
      //  "PeriodTimespan": 5,//多少秒之后客户端可以重试
      //  "Limit": 5          //统计时间段内允许的最大请求数量
      //},
      //缓存
      "FileCacheOptions": {
        "TtlSeconds": 30 //缓存时间(秒)
      },
      "AuthenticationOptions": {

        "AuthenticationProviderKey": "AuthKey",

        "AllowedScopes": []

      }
      //熔断设置
      //"QoSOptions": {
      //  "ExceptionsAllowedBeforeBreaking": 3, //允许多少个异常请求
      //  "DurationOfBreak": 10000, // 熔断的时间,单位为ms
      //  "TimeoutValue": 10000 // 如果下游请求的处理时间超过多少则自如将请求设置为超时 默认90秒
      //}
    }
  ],
  "urls": "https://*:5000;http://*:4000",
  "GlobalConfiguration": {
    //"BaseUrl": null,
    "RequestIdKey": "OcRequestId",
    "ReRouteIsCaseSensitive": true, //是否区分路由字母大小写
    "ServiceDiscoveryProvider": {
      "Host": "localhost", // Consul Service IP
      "Port": 8500 // Consul Service Port
      // "Type": "Consul"
      //"Type": "PollConsul",
      //"PollingInterval": 100 //健康检查时间端
    },
    "RateLimitOptions": {
      "QuotaExceededMessage": "Too many requests,maybe later?11", // 当请求过载被截断时返回的消息
      "HttpStatusCode": 666 // 当请求过载被截断时返回的http status
    }

  }

}

微服务结构

目前走马观花一样说了一遍,也基本就这些内容目前。

祝大家新年新气象。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
【1】项目代码完整且功能都验证ok,确保稳定可靠运行后才上传。欢迎下载使用!在使用过程中,如有问题或建议,请及时私信沟通,帮助解答。 【2】项目主要针对各个计算机相关专业,包括计科、信息安全、数据科学与大数据技术、人工智能、通信、物联网等领域的在校学生、专业教师或企业员工使用。 【3】项目具有较高的学习借鉴价值,不仅适用于小白学习入门进阶。也可作为毕设项目、课程设计、大作业、初期项目立项演示等。 【4】如果基础还行,或热爱钻研,可基于此项目进行二次开发,DIY其他不同功能,欢迎交流学习。 【注意】 项目下载解压后,项目名字和项目路径不要用中文,否则可能会出现解析不了的错误,建议解压重命名为英文名字后再运行!有问题私信沟通,祝顺利! 基于C语言实现智能决策的人机跳棋对战系统源码+报告+详细说明.zip基于C语言实现智能决策的人机跳棋对战系统源码+报告+详细说明.zip基于C语言实现智能决策的人机跳棋对战系统源码+报告+详细说明.zip基于C语言实现智能决策的人机跳棋对战系统源码+报告+详细说明.zip基于C语言实现智能决策的人机跳棋对战系统源码+报告+详细说明.zip基于C语言实现智能决策的人机跳棋对战系统源码+报告+详细说明.zip基于C语言实现智能决策的人机跳棋对战系统源码+报告+详细说明.zip基于C语言实现智能决策的人机跳棋对战系统源码+报告+详细说明.zip基于C语言实现智能决策的人机跳棋对战系统源码+报告+详细说明.zip基于C语言实现智能决策的人机跳棋对战系统源码+报告+详细说明.zip基于C语言实现智能决策的人机跳棋对战系统源码+报告+详细说明.zip基于C语言实现智能决策的人机跳棋对战系统源码+报告+详细说明.zip基于C语言实现智能决策的人机跳棋对战系统源码+报告+详细说明.zip
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值