赞
踩
<PackageReference Include="Volo.Abp.Autofac" Version="4.4.3" />
<PackageReference Include="Volo.Abp.AspNetCore.Mvc" Version="4.4.3" />
/*---------------------------------------------------------------- * 创建者: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); } } }
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(); } } }
<PackageReference Include="Ocelot" Version="17.0.0" />
<PackageReference Include="Ocelot.Provider.Consul" Version="17.0.0" />
//添加ocelot
context.Services.AddOcelot(context.Services.GetConfiguration()).AddConsul();
//添加coelot中间件
app.UseOcelot().Wait();
{ "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": "*" }
// 允许展示微服务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();
}
);
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。