当前位置: 首页 > news >正文

番禺网站开发服务不受限制的搜索引擎

番禺网站开发服务,不受限制的搜索引擎,app下载我的订单,专门做评测的网站什么是Ocelot? Ocelot是一个开源的ASP.NET Core微服务网关,它提供了API网关所需的所有功能,如路由、认证、限流、监控等。 Ocelot是一个简单、灵活且功能强大的API网关,它可以与现有的服务集成,并帮助您保护、监控和扩展您的微…

什么是Ocelot?

Ocelot是一个开源的ASP.NET Core微服务网关,它提供了API网关所需的所有功能,如路由、认证、限流、监控等。

Ocelot是一个简单、灵活且功能强大的API网关,它可以与现有的服务集成,并帮助您保护、监控和扩展您的微服务。

以下是Ocelot的一些主要功能:

  1. 路由管理:Ocelot允许您定义路由规则,将请求路由到正确的微服务。
  2. 认证和授权:Ocelot支持多种认证机制,如JWT、OAuth等,并允许您定义访问控制策略,确保只有授权的用户才能访问特定的API。
  3. 限流和速率限制:Ocelot提供了一些内置的限流和速率限制功能,以确保您的服务不会受到过度的请求压力。
  4. 监控和日志:Ocelot可以收集和显示各种度量指标,帮助您了解您的服务的性能和行为。此外,它还可以将日志记录到各种日志源,以便您进行分析和故障排除。
  5. 集成:Ocelot可以与现有的服务集成,包括Kubernetes、Consul等。
  6. 易于扩展:Ocelot的设计使其易于扩展,您可以编写自己的中间件来处理特定的逻辑,例如修改请求或响应、添加自定义的认证机制等。
  7. 可扩展的配置:Ocelot使用JSON配置文件进行配置,这意味着您可以轻松地根据需要进行配置更改,而无需重新编译代码。

总之,Ocelot是一个功能强大且易于使用的API网关,可以帮助您保护、监控和扩展您的微服务。

Asp .Net Core 集成 Ocelot

要在ASP.NET Core中集成Ocelot,您可以按照以下步骤进行操作:

  1. 安装Ocelot NuGet包:
    在您的ASP.NET Core项目中,打开终端或NuGet包管理器控制台,并运行以下命令来安装Ocelot的NuGet包:
dotnet add package Ocelot
  1. 添加Ocelot配置文件:
{  "Routes": [ //这里注意一下版本(旧版本用ReRoutes){"DownstreamPathTemplate": "/api/{controller}", //下游路径模板"DownstreamScheme": "http", //下游方案//"DownstreamHostAndPorts": [//  {//    "Host": "localhost",//    "Port": "5014"//  }//], //下游主机和端口"UpstreamPathTemplate": "/api/product/{controller}", //上游路径模板"UpstreamHttpMethod": [], //上游请求方法,可以设置特定的 HTTP 方法列表或设置空列表以允许其中任何方法"ServiceName": "api-product-service", //请求服务名称"LoadBalancerOptions": {"Type": "LeastConnection" //负载均衡算法:目前 Ocelot 有RoundRobin 和LeastConnection算法}}],"GlobalConfiguration": {"BaseUrl": "http://localhost:5015", //进行标头查找和替换以及某些管理配置"ServiceDiscoveryProvider": {"Scheme": "http","Host": "127.0.0.1", //你的Consul的ip地址"Port": 8500, //你的Consul的端口"Type": "Consul" //类型//"ConfigurationKey": "Consul" //指定配置键,键入您的配置}}
}
  1. 配置Ocelot服务:
builder.Services.AddOcelot();

Configure方法中配置请求管道并添加Ocelot中间件:

app.UseOcelot().Wait();

Ocelot 集成 Consul

要将Ocelot集成到Consul中,您可以按照以下步骤进行操作:

  1. 安装依赖项:
    在您的ASP.NET Core项目中,安装Ocelot和Consul的NuGet包。您可以使用以下命令来安装这些包:
dotnet add package Ocelot.Provider.Consul
  1. 配置Ocelot:
    在Ocelot的配置中,您需要指定Consul作为服务发现和配置的提供者。在Ocelot的配置文件(例如appsettings.json)中,添加以下内容:
{  "GlobalConfiguration": {"BaseUrl": "http://localhost:5015", //进行标头查找和替换以及某些管理配置"ServiceDiscoveryProvider": {"Scheme": "http","Host": "127.0.0.1", //你的Consul的ip地址"Port": 8500, //你的Consul的端口"Type": "Consul" //类型//"ConfigurationKey": "Consul" //指定配置键,键入您的配置}} 
}

确保将ConsulHostConsulPort设置为Consul服务器的正确地址和端口。

  1. 启动Ocelot:
    在您的ASP.NET Core应用程序中启动Ocelot。您可以在Startup.cs文件中添加以下代码:
builder.Services.AddOcelot().AddConsul(); 

全部代码

Consul相关代码

namespace MCode.Common.Extensions.Consul
{public class ConsulOptions{/// <summary>/// 当前应用IP/// </summary>public string IP { get; set; }/// <summary>/// 当前应用端口/// </summary>public int Port { get; set; }/// <summary>/// 当前服务名称/// </summary>public string ServiceName { get; set; }/// <summary>/// Consul集群IP/// </summary>public string ConsulIP { get; set; }/// <summary>/// Consul集群端口/// </summary>public int ConsulPort { get; set; }/// <summary>/// 权重/// </summary>public int? Weight { get; set; }}
}
using Consul.AspNetCore;
using Consul;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Configuration;
using Microsoft.AspNetCore.Builder;namespace MCode.Common.Extensions.Consul
{public static class ConsulServiceExtensions{/// <summary>/// 向容器中添加Consul必要的依赖注入/// </summary>/// <param name="services"></param>/// <param name="configuration"></param>/// <returns></returns>public static IServiceCollection AddMCodeConsul(this IServiceCollection services){var configuration = services.BuildServiceProvider().GetRequiredService<IConfiguration>();// 配置consul服务注册信息var consulOptions = configuration.GetSection("Consul").Get<ConsulOptions>();// 通过consul提供的注入方式注册consulClientservices.AddConsul(options => options.Address = new Uri($"http://{consulOptions.ConsulIP}:{consulOptions.ConsulPort}"));// 通过consul提供的注入方式进行服务注册var httpCheck = new AgentServiceCheck(){DeregisterCriticalServiceAfter = TimeSpan.FromSeconds(5),//服务启动多久后注册Interval = TimeSpan.FromSeconds(10),//健康检查时间间隔,或者称为心跳间隔HTTP = $"http://{consulOptions.IP}:{consulOptions.Port}/health",//健康检查地址Timeout = TimeSpan.FromSeconds(10)};// Register service with consulservices.AddConsulServiceRegistration(options =>{options.Checks = new[] { httpCheck };options.ID = Guid.NewGuid().ToString();options.Name = consulOptions.ServiceName;options.Address = consulOptions.IP;options.Port = consulOptions.Port;options.Meta = new Dictionary<string, string>() { { "Weight", consulOptions.Weight.HasValue ? consulOptions.Weight.Value.ToString() : "1" } };options.Tags = new[] { $"urlprefix-/{consulOptions.ServiceName}" }; //添加});return services;}/// <summary>/// 配置Consul检查服务/// </summary>/// <param name="app"></param>/// <returns></returns>public static IApplicationBuilder UseConsulCheckService(this IApplicationBuilder app){app.Map("/health", app =>{app.Run(async context =>{await Task.Run(() => context.Response.StatusCode = 200);});});return app;}}
}

Cors

using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace MCode.Common.Extensions.Cors
{public static class CorsServiceExtensions{private readonly static string PolicyName = "MCodeCors";/// <summary>/// 添加跨域/// </summary>/// <param name="services">服务集合</param>/// <returns></returns>public static IServiceCollection AddMCodeCors(this IServiceCollection services){if (services == null) throw new ArgumentNullException(nameof(services));//origin microsoft.aspnetcore.cors      return services.AddCors(options =>{options.AddPolicy(PolicyName, policy =>{policy.SetIsOriginAllowed(_ => true).AllowAnyMethod().AllowAnyHeader().AllowCredentials();});});}/// <summary>/// 使用跨域/// </summary>/// <param name="app">应用程序建造者</param>/// <returns></returns>public static IApplicationBuilder UseMCodeCors(this IApplicationBuilder app){return app.UseCors(PolicyName);}}
}

Swagger

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace MCode.Common.Extensions.Swagger
{/// <summary>/// 网关Swagger配置/// </summary>public class OcelotSwaggerOptions{public List<SwaggerEndPoint> SwaggerEndPoints { get; set; }}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace MCode.Common.Extensions.Swagger
{/// <summary>/// 网关Swagger配置/// </summary>public class OcelotSwaggerOptions{public List<SwaggerEndPoint> SwaggerEndPoints { get; set; }}
}
using Microsoft.OpenApi.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace MCode.Common.Extensions.Swagger
{/// <summary>/// Swagger配置/// </summary>public class SwaggerOptions{/// <summary>/// 服务名称/// </summary>public string ServiceName { get; set; }/// <summary>/// API信息/// </summary>public OpenApiInfo ApiInfo { get; set; }/// <summary>/// Xml注释文件/// </summary>public string[] XmlCommentFiles { get; set; }/// <summary>/// 构造函数/// </summary>/// <param name="serviceName">服务名称</param>/// <param name="apiInfo">API信息</param>/// <param name="xmlCommentFiles">Xml注释文件</param>public SwaggerOptions(string serviceName, OpenApiInfo apiInfo, string[] xmlCommentFiles = null){ServiceName = !string.IsNullOrWhiteSpace(serviceName) ? serviceName : throw new ArgumentException("serviceName parameter not config.");ApiInfo = apiInfo;XmlCommentFiles = xmlCommentFiles;}}
}
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.OpenApi.Models;namespace MCode.Common.Extensions.Swagger
{/// <summary>/// Swagger 服务扩展/// </summary>public static class SwaggerServiceExtensions{/// <summary>/// 添加 Swagger 服务/// </summary>/// <param name="services"></param>/// <param name="swaggerOptions"></param>/// <returns></returns>public static IServiceCollection AddMCodeSwagger(this IServiceCollection services, SwaggerOptions swaggerOptions){services.AddSingleton(swaggerOptions);SwaggerGenServiceCollectionExtensions.AddSwaggerGen(services, c =>{c.SwaggerDoc(swaggerOptions.ServiceName, swaggerOptions.ApiInfo);if (swaggerOptions.XmlCommentFiles != null){foreach (string xmlCommentFile in swaggerOptions.XmlCommentFiles){string str = Path.Combine(AppContext.BaseDirectory, xmlCommentFile);if (File.Exists(str)) c.IncludeXmlComments(str, true);}}SwaggerGenOptionsExtensions.CustomSchemaIds(c, x => x.FullName);c.AddSecurityDefinition("bearerAuth", new OpenApiSecurityScheme{Type = SecuritySchemeType.Http,Scheme = "bearer",BearerFormat = "JWT",Description = "请输入 bearer 认证"});c.AddSecurityRequirement(new OpenApiSecurityRequirement{{new OpenApiSecurityScheme{Reference = new OpenApiReference { Type = ReferenceType.SecurityScheme, Id = "bearerAuth" }},new string[] {}}});});return services;}/// <summary>/// 使用 Swagger UI/// </summary>/// <param name="app"></param>/// <returns></returns>public static IApplicationBuilder UseMCodeSwagger(this IApplicationBuilder app){string serviceName = app.ApplicationServices.GetRequiredService<SwaggerOptions>().ServiceName;SwaggerUIBuilderExtensions.UseSwaggerUI(SwaggerBuilderExtensions.UseSwagger(app), c =>{c.SwaggerEndpoint("/swagger/" + serviceName + "/swagger.json", serviceName);});return app;}public static IServiceCollection AddMCodeOcelotSwagger(this IServiceCollection services, OcelotSwaggerOptions ocelotSwaggerOptions){services.AddSingleton(ocelotSwaggerOptions);SwaggerGenServiceCollectionExtensions.AddSwaggerGen(services);return services;}public static IApplicationBuilder UseMCodeOcelotSwagger(this IApplicationBuilder app){OcelotSwaggerOptions ocelotSwaggerOptions = app.ApplicationServices.GetService<OcelotSwaggerOptions>();if (ocelotSwaggerOptions == null || ocelotSwaggerOptions.SwaggerEndPoints == null){return app;}SwaggerUIBuilderExtensions.UseSwaggerUI(SwaggerBuilderExtensions.UseSwagger(app), c =>{foreach (SwaggerEndPoint swaggerEndPoint in ocelotSwaggerOptions.SwaggerEndPoints){c.SwaggerEndpoint(swaggerEndPoint.Url, swaggerEndPoint.Name);}});return app;}}
}

MCode.ApiGateway.Program

using MCode.Common.Extensions.Swagger;
using MCode.Common.Extensions.Consul;
using MCode.Common.Extensions.Cors;
using Ocelot.DependencyInjection;
using Ocelot.Middleware;
using Ocelot.Provider.Consul;
using Microsoft.AspNetCore.Hosting;var builder = WebApplication.CreateBuilder(args);builder.WebHost.UseUrls(builder.Configuration.GetValue<string>("Url") ?? "");//builder.Configuration.SetBasePath(Directory.GetCurrentDirectory()).AddJsonFile("ocelot.json", false, true);// Add services to the container.
builder.Services.AddMCodeConsul();builder.Services.AddMCodeCors();builder.Services.AddMCodeOcelotSwagger(new OcelotSwaggerOptions { SwaggerEndPoints = new List<SwaggerEndPoint> { new SwaggerEndPoint { Name = "api-product-service", Url= "http://localhost:5014/swagger/api-product-service/swagger.json" } } });builder.Services.AddOcelot().AddConsul();builder.Services.AddControllers();var app = builder.Build();app.UseMCodeCors();app.UseMCodeOcelotSwagger();app.UseConsulCheckService();// Configure the HTTP request pipeline.app.UseAuthorization();app.UseOcelot().Wait();app.Run();

appsettings.json

{"Logging": {"LogLevel": {"Default": "Information","Microsoft.AspNetCore": "Warning"}},"Routes": [ //这里注意一下版本(旧版本用ReRoutes){"DownstreamPathTemplate": "/api/{controller}", //下游路径模板"DownstreamScheme": "http", //下游方案//"DownstreamHostAndPorts": [//  {//    "Host": "localhost",//    "Port": "5014"//  }//], //下游主机和端口"UpstreamPathTemplate": "/api/product/{controller}", //上游路径模板"UpstreamHttpMethod": [], //上游请求方法,可以设置特定的 HTTP 方法列表或设置空列表以允许其中任何方法"ServiceName": "api-product-service", //请求服务名称"LoadBalancerOptions": {"Type": "LeastConnection" //负载均衡算法:目前 Ocelot 有RoundRobin 和LeastConnection算法}}],"GlobalConfiguration": {"BaseUrl": "http://localhost:5015", //进行标头查找和替换以及某些管理配置"ServiceDiscoveryProvider": {"Scheme": "http","Host": "127.0.0.1", //你的Consul的ip地址"Port": 8500, //你的Consul的端口"Type": "Consul" //类型//"ConfigurationKey": "Consul" //指定配置键,键入您的配置}},"Url": "http://localhost:5015","Consul": {"ConsulIP": "127.0.0.1","ConsulPort": "8500","ServiceName": "api-gateway","Ip": "localhost","Port": "5015","Weight": 1},"AllowedHosts": "*"
}

MCode.Product.Api.Program

using MCode.Product.Api;
using MCode.Common.Extensions.Consul;
using MCode.Common.Extensions.Cors;
using MCode.Common.Extensions.Swagger;var builder = WebApplication.CreateBuilder(args);// Add services to the container.builder.WebHost.UseUrls(builder.Configuration.GetValue<string>("Url") ?? "");builder.Services.AddControllers();builder.Services.AddMCodeConsul();builder.Services.AddMCodeCors();builder.Services.AddMCodeSwagger(new SwaggerOptions("api-product-service", new Microsoft.OpenApi.Models.OpenApiInfo { Title = "产品服务" }, new string[] { "MCode.Product.Api.xml" }));var app = builder.Build();app.UseAuthorization();app.UseMCodeCors();app.UseMCodeSwagger();app.UseConsulCheckService();app.MapControllers();app.Run();

appsettings.json

{"Logging": {"LogLevel": {"Default": "Information","Microsoft.AspNetCore": "Warning"}},"AllowedHosts": "*","Url": "http://localhost:5014","Consul": {"ConsulIP": "127.0.0.1","ConsulPort": "8500","ServiceName": "api-product-service","Ip": "localhost","Port": "5014","Weight": 1}
}

效果

image

image


文章转载自:
http://fratching.c7510.cn
http://hypothecation.c7510.cn
http://plaintive.c7510.cn
http://acclivity.c7510.cn
http://shelduck.c7510.cn
http://reappointment.c7510.cn
http://fazenda.c7510.cn
http://solderable.c7510.cn
http://crumble.c7510.cn
http://itabira.c7510.cn
http://ere.c7510.cn
http://proggins.c7510.cn
http://coptis.c7510.cn
http://intruder.c7510.cn
http://igraine.c7510.cn
http://licit.c7510.cn
http://siding.c7510.cn
http://harmotome.c7510.cn
http://siffleur.c7510.cn
http://tehuantepec.c7510.cn
http://eyewall.c7510.cn
http://cunner.c7510.cn
http://darksome.c7510.cn
http://homothermal.c7510.cn
http://wanting.c7510.cn
http://constrict.c7510.cn
http://compressor.c7510.cn
http://spiritualisation.c7510.cn
http://unminished.c7510.cn
http://monoglot.c7510.cn
http://dissolving.c7510.cn
http://posteriorly.c7510.cn
http://abbe.c7510.cn
http://extortionary.c7510.cn
http://unclear.c7510.cn
http://leisured.c7510.cn
http://athwart.c7510.cn
http://deacidify.c7510.cn
http://crissum.c7510.cn
http://ofay.c7510.cn
http://photobiotic.c7510.cn
http://handless.c7510.cn
http://smartdrive.c7510.cn
http://diacritical.c7510.cn
http://dosage.c7510.cn
http://celanese.c7510.cn
http://woald.c7510.cn
http://religiousness.c7510.cn
http://map.c7510.cn
http://hornstone.c7510.cn
http://electrosensory.c7510.cn
http://heady.c7510.cn
http://kist.c7510.cn
http://depone.c7510.cn
http://vincible.c7510.cn
http://rapscallion.c7510.cn
http://wiener.c7510.cn
http://undigested.c7510.cn
http://dispersibility.c7510.cn
http://whipsaw.c7510.cn
http://awane.c7510.cn
http://virtuosity.c7510.cn
http://nodal.c7510.cn
http://pikake.c7510.cn
http://redrive.c7510.cn
http://manipulator.c7510.cn
http://synaptosome.c7510.cn
http://fellable.c7510.cn
http://remediless.c7510.cn
http://urethra.c7510.cn
http://windowpane.c7510.cn
http://philippi.c7510.cn
http://regedit.c7510.cn
http://denitrator.c7510.cn
http://fudge.c7510.cn
http://hpna.c7510.cn
http://immateriality.c7510.cn
http://squish.c7510.cn
http://decontamination.c7510.cn
http://overtime.c7510.cn
http://swayback.c7510.cn
http://walpurgisnacht.c7510.cn
http://defoliant.c7510.cn
http://seducible.c7510.cn
http://patty.c7510.cn
http://granule.c7510.cn
http://esse.c7510.cn
http://shirty.c7510.cn
http://almsgiver.c7510.cn
http://reassociate.c7510.cn
http://atkins.c7510.cn
http://id.c7510.cn
http://capricious.c7510.cn
http://garganey.c7510.cn
http://sword.c7510.cn
http://disequilibrium.c7510.cn
http://macedon.c7510.cn
http://humanitarian.c7510.cn
http://misknowledge.c7510.cn
http://eclamptic.c7510.cn
http://www.zhongyajixie.com/news/93590.html

相关文章:

  • 供应长沙手机网站建设怎么在网上销售
  • 国外免费服务器申请对网站外部的搜索引擎优化
  • 绵阳的网站建设公司湖南网站建设效果
  • 想开发一个网站需要怎样做站长工具排行榜
  • 网站建设与管理教学视频下载福州百度关键词优化
  • 优秀企业建站网络营销策略实施的步骤
  • 做网站什么内容河南网站建设制作
  • 有什么网站是做名片印刷的今天最新新闻
  • wamp和wordpress北京网站seo设计
  • 青岛网站设计案例代运营公司前十名
  • 网站模板的修改线上销售渠道有哪几种
  • discuz可以做公司网站搜狗网址导航
  • 怀柔网站制作公司如何设置友情链接
  • 网页设计的风格可分为两大类黄冈seo顾问
  • 如何在阿里网站做外单网站如何推广运营
  • wordpress pdf 显示seo品牌
  • 单页网站做cpa网站优化 推广
  • 安徽中色十二冶金建设有限公司网站三叶草gw9356
  • 温州做网站厉害的公司有哪些湖南企业竞价优化服务
  • 自己做的网页怎么连接到网站百度seo在线优化
  • 北京专业做网站设计公司广州知名网络推广公司
  • 网站做过备案后能改别的公司吗常德seo
  • 东莞建站公司快荐全网天下特别好seo诊断方案
  • wordpress分权限浏览超级优化空间
  • 做网站公司三年财务预算表网站seo如何优化
  • 网站建设发展现状免费刷赞网站推广免费
  • 网站源码asp发布软文平台
  • 我要浏览国外网站怎么做网站检测工具
  • wordpress建立购物网站百度网盘网页
  • 开发微信公众号公司官网seo哪家公司好