当前位置:   article > 正文

千万级秒杀系统-4.Api网关层_volo.abp中使用ocelot

volo.abp中使用ocelot

系统架构层

在这里插入图片描述

对之前创建的API网关层进行改造使之进行正常的通信

1、引入abp nuget包

在这里插入图片描述

	  <PackageReference Include="Volo.Abp.Autofac" Version="4.4.3" />
	  <PackageReference Include="Volo.Abp.AspNetCore.Mvc" Version="4.4.3" />
  • 1
  • 2

2、创建BackendApiGatewayModule并把Startup代码拿到BackendApiGatewayModule然后再Startup使用BackendApiGatewayModule 让abp注册

在这里插入图片描述

/*----------------------------------------------------------------
 * 创建者:WangBenChi
 * 创建时间:2023/4/15 16:29:47
 *----------------------------------------------------------------*/

using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.OpenApi.Models;
using Volo.Abp;
using Volo.Abp.AspNetCore.Mvc;
using Volo.Abp.Autofac;
using Volo.Abp.Modularity;

namespace BackendApiGateway
{
    [DependsOn(typeof(AbpAutofacModule),
        typeof(AbpAspNetCoreMvcModule))]
    public class BackendApiGatewayModule : AbpModule
    {
        public override void ConfigureServices(ServiceConfigurationContext context)
        {
            context.Services.AddControllers();
            context.Services.AddSwaggerGen(c =>
            {
                c.SwaggerDoc("v1", new OpenApiInfo { Title = "BackendApiGateway", Version = "v1" });
            });
            //base.ConfigureServices(context);
        }

        public override void OnApplicationInitialization(ApplicationInitializationContext context)
        {
            var app = context.GetApplicationBuilder();
            if (context.GetEnvironment().IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
                app.UseSwagger();
                app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "BackendApiGateway v1"));
            }

            app.UseHttpsRedirection();

            app.UseRouting();

            app.UseAuthorization();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();
            });
            //base.OnApplicationInitialization(context);
        }
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;

namespace BackendApiGateway
{
    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.AddApplication<BackendApiGatewayModule>();
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            app.InitializeApplication();
        }
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29

3、将api网关项目转换成能够访问到微服务的项目

引入Ocelot、Ocelot.Provider.Consul nuget包

在这里插入图片描述

		<PackageReference Include="Ocelot" Version="17.0.0" />
		<PackageReference Include="Ocelot.Provider.Consul" Version="17.0.0" />
  • 1
  • 2

BackendApiGatewayModule加入服务及中间件

在这里插入图片描述

//添加ocelot
context.Services.AddOcelot(context.Services.GetConfiguration()).AddConsul();

//添加coelot中间件
app.UseOcelot().Wait();
  • 1
  • 2
  • 3
  • 4
  • 5

在appsettings.json加入配置项

{
  "Routes": [
    {
      "UpstreamPathTemplate": "/api/ProductService/{everything}",
      "UpstreamHttpMethod": [ "Put", "Delete", "Get", "Post" ],
      "DownstreamPathTemplate": "/api/ProductService/{everything}",
      "DownstreamScheme": "http",
      "ServiceName": "productservices",
      "LoadBalancerOptions": {
        "Type": "RoundRobin"
      }
    },
    {
      "UpstreamPathTemplate": "/api/OrderService/{everything}",
      "UpstreamHttpMethod": [ "Put", "Delete", "Get", "Post" ],
      "DownstreamPathTemplate": "/api/OrderService/{everything}",
      "DownstreamScheme": "http",
      "ServiceName": "orderservices",
      "LoadBalancerOptions": {
        "Type": "RoundRobin"
      }
    },
    {
      "UpstreamPathTemplate": "/api/PaymentService/{everything}",
      "UpstreamHttpMethod": [ "Put", "Delete", "Get", "Post" ],
      "DownstreamPathTemplate": "/api/PaymentService/{everything}",
      "DownstreamScheme": "http",
      "ServiceName": "paymentservices",
      "LoadBalancerOptions": {
        "Type": "RoundRobin"
      }
    },
    {
      "UpstreamPathTemplate": "/api/SeckillService/{everything}",
      "UpstreamHttpMethod": [ "Put", "Delete", "Get", "Post" ],
      "DownstreamPathTemplate": "/api/SeckillService/{everything}",
      "DownstreamScheme": "http",
      "ServiceName": "seckillservices",
      "LoadBalancerOptions": {
        "Type": "RoundRobin"
      }
    },
    {
      "UpstreamPathTemplate": "/api/UserService/{everything}",
      "UpstreamHttpMethod": [ "Put", "Delete", "Get", "Post" ],
      "DownstreamPathTemplate": "/api/UserService/{everything}",
      "DownstreamScheme": "http",
      "ServiceName": "userservices",
      "LoadBalancerOptions": {
        "Type": "RoundRobin"
      }
    }
  ],
  "GlobalConfiguration": {
    "BaseUrl": "http://localhost:5000",
    "ServiceDiscoveryProvider": {
      "Scheme": "http",
      "Host": "localhost",
      "Port": 8500,
      "Type": "Consul"
    }
  },
  "Logging": {
    "LogLevel": {
      "Default": "Warning"
    }
  },
  "AllowedHosts": "*"
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69

4、将api网关项目集成微服务api项目

添加项目依赖引入各微服务的HttpApi

在这里插入图片描述

BackendApiGatewayModule进行修改使之能够显示微服务项目的相关api

在这里插入图片描述

// 允许展示微服务API
c.DocInclusionPredicate((docName, description) => true);
// 防止多个项目冲突
c.CustomSchemaIds(type => type.FullName);

//显示api路由
app.MapWhen(
    ctx =>
        ctx.Request.Path.ToString().StartsWith("/api/abp/"),
   app2 =>
	  {
	      app2.UseRouting();
	      app2.UseConfiguredEndpoints();
	 }
);
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

Program加入依赖注入

在这里插入图片描述

测试是否能够正常访问微服务api

在这里插入图片描述

调用接口查看能否正常对数据进行操作

这里显示连接下游时发生异常,推测时由于https引起的所以将微服务的改为http并重新启动

在这里插入图片描述

api网关层访问微服务成功

在这里插入图片描述

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/从前慢现在也慢/article/detail/141393
推荐阅读
相关标签
  

闽ICP备14008679号