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

个人服务器 网站建设24小时在线更新视频高清免费观看

个人服务器 网站建设,24小时在线更新视频高清免费观看,华强方特网站开发,分销怎么做网站开发分销Intro我们的 API 之前是一个单体应用#xff0c;各个模块的服务是通过 Assembly 集成在一起#xff0c;最后部署在一个 web server 下的。我们已经在拆分服务并且在 Ocelot 的基础上封装了我们自己的网关#xff0c;但是服务还没有完全拆分#xff0c;于是有这么一个需求各个模块的服务是通过 Assembly 集成在一起最后部署在一个 web server 下的。我们已经在拆分服务并且在 Ocelot 的基础上封装了我们自己的网关但是服务还没有完全拆分于是有这么一个需求对于 Ocelot 配置的路由去交给 Ocelot 去转发到真正的服务地址而那些 Ocelot 没有定义的路由则让交给 AspNetCore 去处理。实现原理实现原理是让 Ocelot 作为一个动态分支路由只有当 Ocelot 配置了对应路由的下游地址才走 Ocelot 的分支才把请求交给 Ocelot 处理。我们可以使用 MapWhen 来处理接下来就需要知道怎么样判断 Ocelot 是否配置了某一个路由Ocelot 内部的处理管道在向下游请求之前是要找到对应匹配的下游路由所以我们去看一看 Ocelot 的源码看看 Ocelot 内部是怎么找下游路由的Ocelot 找下游路由中间件源码 public async Task Invoke(DownstreamContext context) { var upstreamUrlPath context.HttpContext.Request.Path.ToString(); var upstreamQueryString context.HttpContext.Request.QueryString.ToString(); var upstreamHost context.HttpContext.Request.Headers[Host]; Logger.LogDebug($Upstream url path is {upstreamUrlPath}); var provider _factory.Get(context.Configuration); // 获取下游路由 var downstreamRoute provider.Get(upstreamUrlPath, upstreamQueryString, context.HttpContext.Request.Method, context.Configuration, upstreamHost); if (downstreamRoute.IsError) { Logger.LogWarning(${MiddlewareName} setting pipeline errors. IDownstreamRouteFinder returned {downstreamRoute.Errors.ToErrorString()}); SetPipelineError(context, downstreamRoute.Errors); return; } var downstreamPathTemplates string.Join(, , downstreamRoute.Data.ReRoute.DownstreamReRoute.Select(r r.DownstreamPathTemplate.Value)); Logger.LogDebug($downstream templates are {downstreamPathTemplates}); context.TemplatePlaceholderNameAndValues downstreamRoute.Data.TemplatePlaceholderNameAndValues; await _multiplexer.Multiplex(context, downstreamRoute.Data.ReRoute, _next); }通过上面的源码我们就可以判断 Ocelot 是否有与请求相匹配的下游路由信息实现既然找到了 Ocelot 如何找下游路由就先给 Ocelot 加一个扩展吧实现代码如下Ocelot 扩展完整代码 public static IApplicationBuilder UseOcelotWhenRouteMatch(this IApplicationBuilder app, ActionIOcelotPipelineBuilder, OcelotPipelineConfiguration builderAction) UseOcelotWhenRouteMatch(app, builderAction, new OcelotPipelineConfiguration()); public static IApplicationBuilder UseOcelotWhenRouteMatch(this IApplicationBuilder app, ActionOcelotPipelineConfiguration pipelineConfigurationAction, ActionIOcelotPipelineBuilder, OcelotPipelineConfiguration builderAction) { var pipelineConfiguration new OcelotPipelineConfiguration(); pipelineConfigurationAction?.Invoke(pipelineConfiguration); return UseOcelotWhenRouteMatch(app, builderAction, pipelineConfiguration); } public static IApplicationBuilder UseOcelotWhenRouteMatch(this IApplicationBuilder app, ActionIOcelotPipelineBuilder, OcelotPipelineConfiguration builderAction, OcelotPipelineConfiguration configuration) { app.MapWhen(context { // 获取 OcelotConfiguration var internalConfigurationResponse context.RequestServices.GetRequiredServiceIInternalConfigurationRepository().Get(); if (internalConfigurationResponse.IsError || internalConfigurationResponse.Data.ReRoutes.Count 0) { // 如果没有配置路由信息不符合分支路由的条件直接退出 return false; } var internalConfiguration internalConfigurationResponse.Data; var downstreamRouteFinder context.RequestServices .GetRequiredServiceIDownstreamRouteProviderFactory() .Get(internalConfiguration); // 根据请求以及上面获取的Ocelot配置获取下游路由 var response downstreamRouteFinder.Get(context.Request.Path, context.Request.QueryString.ToString(), context.Request.Method, internalConfiguration, context.Request.Host.ToString()); // 如果有匹配路由则满足该分支路由的条件交给 Ocelot 处理 return !response.IsError !string.IsNullOrEmpty(response.Data?.ReRoute?.DownstreamReRoute?.FirstOrDefault() ?.DownstreamScheme); }, appBuilder appBuilder.UseOcelot(builderAction, configuration).Wait()); return app; }使用在 Startup 里ConfigurationServices 配置 mvc 和 OcelotConfigure 方法里配置 ocelot 和 mvcapp.UseOcelotWhenRouteMatch((ocelotBuilder, pipelineConfiguration) { // This is registered to catch any global exceptions that are not handled // It also sets the Request Id if anything is set globally ocelotBuilder.UseExceptionHandlerMiddleware(); // This is registered first so it can catch any errors and issue an appropriate response ocelotBuilder.UseResponderMiddleware(); ocelotBuilder.UseDownstreamRouteFinderMiddleware(); ocelotBuilder.UseDownstreamRequestInitialiser(); ocelotBuilder.UseRequestIdMiddleware(); ocelotBuilder.UseMiddlewareClaimsToHeadersMiddleware(); ocelotBuilder.UseLoadBalancingMiddleware(); ocelotBuilder.UseDownstreamUrlCreatorMiddleware(); ocelotBuilder.UseOutputCacheMiddleware(); ocelotBuilder.UseMiddlewareHttpRequesterMiddleware(); // cors headers ocelotBuilder.UseMiddlewareCorsMiddleware(); });app.UseMvc();新建一个 TestController [Route(/api/[controller])] public class TestController : ControllerBase { public IActionResult Get() { return Ok(new { Tick DateTime.UtcNow.Ticks, Msg Hello Ocelot, }); } }具体代码可以参考这个 网关示例项目示例项目的 Ocelot 配置是存在 Redis 里面的配置的 ReRoutes 如下{ ReRoutes: [ { DownstreamPathTemplate: /api.php?keyfreeappid0msg{everything}, UpstreamPathTemplate: /api/chat/{everything}, UpstreamHttpMethod: [ Get, POST, PUT, PATCH, DELETE, OPTIONS ], AddHeadersToRequest: { }, RequestIdKey: RequestId, ReRouteIsCaseSensitive: false, ServiceName: , DownstreamScheme: http, DownstreamHostAndPorts: [ { Host: api.qingyunke.com, Port: 80 } ], DangerousAcceptAnyServerCertificateValidator: false } ], GlobalConfiguration: { HttpHandlerOptions: { AllowAutoRedirect: false, UseCookieContainer: false, UseTracing: false } }}运行项目进行测试访问 Ocelot 定义的路由 http://localhost:65125/api/chat/hello 返回信息如图所示访问 Mvc 定义的路由 http://localhost:65125/api/test返回信息如图所示上面正常的返回就表示我们的 Ocelot 和 Mvc 同时工作了~Referencehttps://github.com/ThreeMammals/Ocelothttps://github.com/WeihanLi/AspNetCorePlayground/tree/master/TestGateway
http://www.zqtcl.cn/news/290000/

相关文章:

  • 如何制作手机免费网站模板下载用户体验设计师是什么
  • php网站建设基本流程基于php网站开发设计
  • 建设一个网站需要哪些软硬件条件无做弊的棋牌游戏网站
  • 最有设计感的网站扬中网站建设 优帮云
  • 企业建设银行网站登录不了wordpress需要ftp
  • 广州营销型网站建设团队专业建设内涵包括哪些内容
  • 网站如何做响应式布局外国网站上做Task
  • 知乎网站建设入门书大渡口集团网站建设
  • 免费网站建设是什么宁波网站建设的价格表
  • 网站设计导航栏高度网站设计的经营范围
  • 帮别人建设网站多少利润北京网站建设公司华网制作作
  • 微信网站需要备案吗瑞安商业网站建设
  • 做网站如何计算工资wordpress stheme
  • 网站建设销售人才简历wordpress 搜索tag
  • 设计网站专业云南旅行社网站开发
  • 小规模开普票网站建设几个点张浦专业做网站
  • 点击图片跳转到网站怎么做链接网址后缀名大全
  • php网站开发优化crm客户系统
  • 韩国网站免费模板wordpress数据库名称
  • 如何修改网站发布时间贵阳网站建设报价
  • 东莞网站推广培训免费云电脑
  • 湖北网站建设详细方案脑叶公司手机版下载
  • 淄博网站制作平台形象怎样建设旅游网站
  • 广州花都网站建设网站改版协议
  • 中国建设协会网站首页工信部网站备案被删除
  • 丹阳网站建设案例dedecms 购物网站
  • 网站上怎么做动画广告视频下载seo黑帽是什么意思
  • 服装网站建设网综合社区网站开发费用
  • 做网站预付款 怎么做账做律师网站的网络公司
  • 购物网站开发模板小程序注册拉新