Ocelot是用 .NET Core 实现的一个开源API网关。包含身份验证、路由、请求聚合等功能。能够轻松的集成IdentityServer

  • 基本使用
    在这里插入图片描述

  • 集成IdentityServer
    在这里插入图片描述

  • 多实例
    在这里插入图片描述

  • 集成Consul
    在这里插入图片描述

  • 集成 Service Fabric
    在这里插入图片描述

新建一个Web项目ApiGateways,添加nuget包引用

Install-Package Ocelot

在这里插入图片描述

  • ocelot.json
  1. {
  2. "ReRoutes": [
  3. {
  4. "DownstreamPathTemplate": "/todos/{id}",
  5. "DownstreamScheme": "https",
  6. "DownstreamHostAndPorts": [
  7. {
  8. "Host": "jsonplaceholder.typicode.com",
  9. "Port": 443
  10. }
  11. ],
  12. "UpstreamPathTemplate": "/todos/{id}",
  13. "UpstreamHttpMethod": [ "Get" ]
  14. }
  15. ],
  16. "GlobalConfiguration": {
  17. "BaseUrl": "https://localhost:5000"
  18. }
  19. }

此配置中,ReRoutes节点下的Downstream相关节点表示网关下游服务相关配置,以上,我们指定了任意请求都以https请求转发,其中DownstreamHostAndPorts表示下游服务地址和端口。Upstream表示上游请求配置

  • 配置服务引入oeclot.json
  1. public static IHostBuilder CreateHostBuilder(string[] args) =>
  2. Host.CreateDefaultBuilder(args)
  3. .ConfigureAppConfiguration((hostingContext, config) =>
  4. {
  5. config.SetBasePath(hostingContext.HostingEnvironment.ContentRootPath)
  6. .AddJsonFile("ocelot.json")
  7. .AddEnvironmentVariables();
  8. })
  9. .ConfigureWebHostDefaults(webBuilder =>
  10. {
  11. webBuilder.UseStartup<Startup>();
  12. });

此配置中,我们的网关服务运行在http://localhost,其中AuthenticationOptions是认证服务相关配置,另外有一个下游服务运行在http://localhost:5201

  1. public void ConfigureServices(IServiceCollection services)
  2. {
  3. services.AddOcelot();
  4. }
  1. public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
  2. {
  3. app.UseOcelot();
  4. }

添加nuget

Install-Package IdentityServer4 -Version 3.1.2

在这里插入图片描述

  • IdentityServer4 配置
    为便于展示,不做持久化,写在内存中
  1. public static class Config
  2. {
  3. // Defining an API Resource
  4. public static IEnumerable<ApiResource> Apis => new List<ApiResource>
  5. {
  6. new ApiResource("gateway_api","ApiGateways")
  7. };
  8. // Defining Client
  9. public static IEnumerable<Client> Clients => new List<Client>
  10. {
  11. new Client
  12. {
  13. ClientId="app_test",
  14. // no interactive user, use the clientid/secret for authentication
  15. AllowedGrantTypes=GrantTypes.ClientCredentials,
  16. // secret for authentication
  17. ClientSecrets={
  18. new Secret("123456".Sha256())
  19. },
  20. // scopes that client has access to
  21. AllowedScopes=new List<string>{
  22. "gateway_api",
  23. }
  24. }
  25. };
  26. // Defineing Identity Resource
  27. public static IEnumerable<IdentityResource> IdentityResources => new List<IdentityResource>
  28. {
  29. new IdentityResources.OpenId(),
  30. new IdentityResources.Profile(),
  31. };
  32. }
  • 添加IdentityServer4到容器
  1. public void ConfigureDevelopmentServices(IServiceCollection services)
  2. {
  3. var builder = services.AddIdentityServer()
  4. .AddInMemoryApiResources(Config.Apis)
  5. .AddInMemoryClients(Config.Clients)
  6. .AddInMemoryIdentityResources(Config.IdentityResources);
  7. builder.AddDeveloperSigningCredential();
  8. services.AddControllers();
  9. }
  • 添加IdentityServer4到请求管道
  1. public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
  2. {
  3. app.UseHttpsRedirection();
  4. app.UseRouting();
  5. app.UseIdentityServer();
  6. app.UseAuthorization();
  7. app.UseEndpoints(endpoints =>
  8. {
  9. endpoints.MapControllers();
  10. });
  11. }

在这里插入图片描述
我们将在ServiceA中提供一个简单服务:为一个用户打一个标签

  1. [Route("[controller]")]
  2. [ApiController]
  3. public class UserController : ControllerBase
  4. {
  5. [HttpPost]
  6. [Route("tag/create")]
  7. public IActionResult CreateTag([FromForm]int userId, [FromForm]string value)
  8. {
  9. // 假设数据库记录添加成功,直接返回对象
  10. Tag tagEntity = new Tag();
  11. tagEntity.Id = 1;
  12. tagEntity.UserId = userId;
  13. tagEntity.Value = value;
  14. return Ok(tagEntity);
  15. }
  16. }
  17. public class Tag
  18. {
  19. public int Id { get; set; }
  20. public int UserId { get; set; }
  21. public string Value { get; set; }
  22. }

支持我们三个项目已经建立完成,但要通过网关身份认证服务请求到创建标签的服务,我们还需要对网关服务做一些修改。
首先,在ocelot.json新增AuthenticationOptions配置IdentityServer4身份认证服务中对应的资源

  1. {
  2. "ReRoutes": [
  3. {
  4. "DownstreamPathTemplate": "/{url}",
  5. "DownstreamScheme": "http",
  6. "DownstreamHostAndPorts": [
  7. {
  8. "Host": "localhost",
  9. "Port": 5201
  10. }
  11. ],
  12. "UpstreamPathTemplate": "/service-a/{url}",
  13. "UpstreamHttpMethod": [ "Get", "Post" ],
  14. "AuthenticationOptions": {
  15. "AuthenticationProviderKey": "SampleKey",
  16. "AllowedScopes": [ "gateway_api" ]
  17. }
  18. }
  19. ],
  20. "GlobalConfiguration": {
  21. "BaseUrl": "http://localhost"
  22. }
  23. }

其中http://localhost:5201是我们ServiceA运行的地址

然后我们需要注入认证服务到容器

Install-Package IdentityServer4.AccessTokenValidation -Version 3.0.1

  1. public void ConfigureServices(IServiceCollection services)
  2. {
  3. var authenticationProviderKey = "SampleKey";
  4. Action<IdentityServerAuthenticationOptions> options = o =>
  5. {
  6. o.Authority = "http://localhost:5200";
  7. o.ApiName = "gateway_api";
  8. o.SupportedTokens = SupportedTokens.Both;
  9. o.ApiSecret = "123456";
  10. o.RequireHttpsMetadata = false;
  11. };
  12. services.AddAuthentication()
  13. .AddIdentityServerAuthentication(authenticationProviderKey, options);
  14. services.AddOcelot();
  15. }

其中http://localhost:5200是我们认证服务运行的地址

  • 通过网关直接调用
    在这里插入图片描述
    我们发现返回401未授权,这是正常的,因为我们为网关服务添加了认证服务。

  • 通过token访问

我们首先要去拿token,我们现在暂时先通过postman直接获取token
在这里插入图片描述
通过token访问
在这里插入图片描述
我们可以看到已经成功请求到了创建用户标签接口

Ocelot官方文档
.NET Core开源API网关 – Ocelot中文文档

版权声明:本文为zhaobingwang原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/zhaobingwang/p/12424708.html