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

网站建设去超速云建站太原论坛

网站建设去超速云建站,太原论坛,网页设计作业成品下载,网站在线开放端口网关介绍网关其实就是将我们写好的API全部放在一个统一的地址暴露在公网#xff0c;提供访问的一个入口。在 .NET Core下可以使用Ocelot来帮助我们很方便的接入API 网关。与之类似的库还有ProxyKit#xff0c;微软也发布了一个反向代理的库YARP。关于网关的介绍不多说了… 网关介绍网关其实就是将我们写好的API全部放在一个统一的地址暴露在公网提供访问的一个入口。在 .NET Core下可以使用Ocelot来帮助我们很方便的接入API 网关。与之类似的库还有ProxyKit微软也发布了一个反向代理的库YARP。关于网关的介绍不多说了网上文章也挺多的这些都是不错的选择听说后期Ocelot将会使用YARP来重写。本篇主要实践一下在.NET Core环境下使用Ocelot。Ocelot官网https://threemammals.com/ocelotOcelot文档https://ocelot.readthedocs.ioGitHubhttps://github.com/ThreeMammals/OcelotOcelot资源汇总https://www.cnblogs.com/shanyou/p/10363360.html接入使用接口示例先创建几个项目用于测试创建两个默认的API项目Api_A和Api_B在创建一个网关项目Api_Gateway网关项目可以选择空的模板。现在分别在Api_A和Api_B中写几个api将默认的WeatherForecastController中返回模型WeatherForecast添加一个字段Source用于区分是哪个API返回的数据。using System;namespace Api_A {public class WeatherForecast{public string Source { get; set; }  Api_A;public DateTime Date { get; set; }public int TemperatureC { get; set; }public int TemperatureF  32  (int)(TemperatureC / 0.5556);public string Summary { get; set; }} }using System;namespace Api_B {public class WeatherForecast{public string Source { get; set; }  Api_B;public DateTime Date { get; set; }public int TemperatureC { get; set; }public int TemperatureF  32  (int)(TemperatureC / 0.5556);public string Summary { get; set; }} } 直接使用WeatherForecastController默认方法在路由中添加api前缀。using Microsoft.AspNetCore.Mvc; using System; using System.Collections.Generic; using System.Linq;namespace Api_A.Controllers {[ApiController][Route(api/[controller])]public class WeatherForecastController : ControllerBase{private static readonly string[] Summaries  new[]{Freezing, Bracing, Chilly, Cool, Mild, Warm, Balmy, Hot, Sweltering, Scorching};[HttpGet]public IEnumerableWeatherForecast Get(){var rng  new Random();return Enumerable.Range(1, 5).Select(index  new WeatherForecast{Date  DateTime.Now.AddDays(index),TemperatureC  rng.Next(-20, 55),Summary  Summaries[rng.Next(Summaries.Length)]}).ToArray();}} }using Microsoft.AspNetCore.Mvc; using System; using System.Collections.Generic; using System.Linq;namespace Api_B.Controllers {[ApiController][Route(api/[controller])]public class WeatherForecastController : ControllerBase{private static readonly string[] Summaries  new[]{Freezing, Bracing, Chilly, Cool, Mild, Warm, Balmy, Hot, Sweltering, Scorching};[HttpGet]public IEnumerableWeatherForecast Get(){var rng  new Random();return Enumerable.Range(1, 5).Select(index  new WeatherForecast{Date  DateTime.Now.AddDays(index),TemperatureC  rng.Next(-20, 55),Summary  Summaries[rng.Next(Summaries.Length)]}).ToArray();}} } 再分别在Api_A和Api_B中添加两个控制器ApiAController、ApiBController然后加上几个简单的restful api。using Microsoft.AspNetCore.Mvc; using System.Collections.Generic;namespace Api_A.Controllers {[Route(api/[controller])][ApiController]public class ApiAController : ControllerBase{[HttpGet]public IEnumerablestring Get(){return new string[] { value1, value2 };}[HttpGet({id})]public string Get(int id){return $Get{id};}[HttpPost]public string Post([FromForm] string value){return $Post:{value};}[HttpPut({id})]public string Put(int id, [FromForm] string value){return $Put:{id}:{value};}[HttpDelete({id})]public string Delete(int id){return $Delete:{id};}} } using Microsoft.AspNetCore.Mvc; using System.Collections.Generic;namespace Api_B.Controllers {[Route(api/[controller])][ApiController]public class ApiBController : ControllerBase{[HttpGet]public IEnumerablestring Get(){return new string[] { value1, value2 };}[HttpGet({id})]public string Get(int id){return $Get{id};}[HttpPost]public string Post([FromForm] string value){return $Post:{value};}[HttpPut({id})]public string Put(int id, [FromForm] string value){return $Put:{id}:{value};}[HttpDelete({id})]public string Delete(int id){return $Delete:{id};}} } 方便查看接口这里添加一下swagger组件这样我们Api_A和Api_B项目分别就有了6个接口。接着打包docker镜像放在docker中运行这两个api项目。这一步可以用任何你熟悉的方式run起来即可。docker build -t api_a:dev -f ./Api_A/Dockerfile . docker build -t api_b:dev -f ./Api_B/Dockerfile . build成功后指定两个端口运行api项目。docker run -d -p 5050:80 --name api_a api_a:dev docker run -d -p 5051:80 --name api_b api_b:dev Api_A指定了5050端口通过 http://localhost:5050/swagger打开可以看到swagger文档界面Api_B指定了5051端口通过 http://localhost:5051/swagger打开可以看到swagger文档界面这样就大功告成了接下来才是重点将两个api项目配置到Api_Gateway网关项目中。配置网关在网关项目Api_Gateway中都添加Ocelot组件包。Install-Package Ocelot Ocelot中最关键的就是配置路由信息新建一个ocelot.json配置文件将我们的两个API接口匹配规则放进去。{Routes: [//ApiA{DownstreamPathTemplate: /api/WeatherForecast,DownstreamScheme: http,DownstreamHostAndPorts: [{Host: localhost,Port: 5050}],UpstreamPathTemplate: /ApiA/WeatherForecast,UpstreamHttpMethod: [ Get ]},{DownstreamPathTemplate: /api/ApiA,DownstreamScheme: http,DownstreamHostAndPorts: [{Host: localhost,Port: 5050}],UpstreamPathTemplate: /ApiA,UpstreamHttpMethod: [ Get, POST ]},{DownstreamPathTemplate: /api/ApiA/{id},DownstreamScheme: http,DownstreamHostAndPorts: [{Host: localhost,Port: 5050}],UpstreamPathTemplate: /ApiA/{id},UpstreamHttpMethod: [ Get, Put, Delete ]},//ApiB{DownstreamPathTemplate: /api/WeatherForecast,DownstreamScheme: http,DownstreamHostAndPorts: [{Host: localhost,Port: 5051}],UpstreamPathTemplate: /ApiB/WeatherForecast,UpstreamHttpMethod: [ Get ]},{DownstreamPathTemplate: /api/ApiB,DownstreamScheme: http,DownstreamHostAndPorts: [{Host: localhost,Port: 5051}],UpstreamPathTemplate: /ApiB,UpstreamHttpMethod: [ Get, POST ]},{DownstreamPathTemplate: /api/ApiB/{id},DownstreamScheme: http,DownstreamHostAndPorts: [{Host: localhost,Port: 5051}],UpstreamPathTemplate: /ApiB/{id},UpstreamHttpMethod: [ Get, Put, Delete ]}],GlobalConfiguration: {BaseUrl: https://localhost:44335} } 关于配置文件中的各项具体含义可以参考官方文档中的介绍。主要就是将DownstreamPathTemplate模板内容转换为UpstreamPathTemplate模板内容进行接口的访问同时可以指定HTTP请求的方式等等。GlobalConfiguration中的BaseUrl为我们暴漏出去的网关地址。设置好ocelot.json后需要在代码中使用它在Program.cs中添加配置文件。using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Hosting;namespace Api_Gateway {public class Program{public static void Main(string[] args){CreateHostBuilder(args).Build().Run();}public static IHostBuilder CreateHostBuilder(string[] args) Host.CreateDefaultBuilder(args).ConfigureAppConfiguration((context, config) {config.AddJsonFile(ocelot.json, optional: false, reloadOnChange: true);}).ConfigureWebHostDefaults(webBuilder {webBuilder.UseStartupStartup();});} } 在Startup.cs中使用Ocelot。using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Http; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using Ocelot.DependencyInjection; using Ocelot.Middleware;namespace Api_Gateway {public class Startup{public void ConfigureServices(IServiceCollection services){services.AddOcelot();}public void Configure(IApplicationBuilder app, IWebHostEnvironment env){if (env.IsDevelopment()){app.UseDeveloperExceptionPage();}app.UseRouting();app.UseEndpoints(endpoints {endpoints.MapGet(/, async context {await context.Response.WriteAsync(Hello World!);});});app.UseOcelot().Wait();}} } 完成以上操作后我们试着去调用接口看看能否正确获取预期数据。curl -X GET https://localhost:44335/ApiA curl -X GET https://localhost:44335/ApiBcurl -X POST https://localhost:44335/ApiA -H Content-Type: multipart/form-data -F valueApiA curl -X POST https://localhost:44335/ApiB -H Content-Type: multipart/form-data -F valueApiBcurl -X GET https://localhost:44335/ApiA/12345 curl -X GET https://localhost:44335/ApiB/12345curl -X PUT https://localhost:44335/ApiA/12345 -H Content-Type: multipart/form-data -F valueApiA curl -X PUT https://localhost:44335/ApiB/12345 -H Content-Type: multipart/form-data -F valueApiBcurl -X DELETE https://localhost:44335/ApiA/12345 curl -X DELETE https://localhost:44335/ApiB/12345curl -X GET https://localhost:44335/ApiA/WeatherForecast curl -X GET https://localhost:44335/ApiB/WeatherForecast 可以看到两个项目中的接口全部可以通过网关项目暴露的地址进行中转是不是很方便本篇只是简单的应用对于Ocelot的功能远不止于此它非常强大还可以实现请求聚合、服务发现、认证、鉴权、限流熔断、并内置了负载均衡器而且这些功能都是只需要简单的配置即可完成。就不一一描述了如有实际开发需求和问题可以查看官方文档和示例。
http://www.yutouwan.com/news/257398/

相关文章:

  • 公司做网站推广的价格wordpress微信支付购买课程
  • 哈尔滨网站搜索优化沈阳做网站的公司排名
  • 巡视组 住房与城乡建设部网站易优cms收费吗
  • 企业推广的网站安康微信公众平台
  • 网站后台密码怎么修改做网站的联系方式
  • 丹东网站推广建设网站如何索要素材
  • 什么网站可以做投票app设计模板网站
  • 网站开发记科目建设网站的英语怎么说
  • 电子商务类网站2008服务器网站
  • 赣州建设网站公司世界互联网峰会时间
  • 北京网站建设是什么大数据培训课程
  • 网站制作一般需要多少钱?企业网站优化服务商
  • 一个微信可以做两个网站支付宝吗海尔集团企业网站建设分析
  • 个人建网站首选什么域名好wordpress地址和找点地址
  • 海兴县网站建设公司软件工程大学排名
  • 做外贸网站公司哪家好seo视频教程
  • 佛山网站提升排名长沙手机网站建设公司
  • wordpress网站欣赏百度收录好最快的网站
  • 网站建设公司话术宁德市人社局
  • 服务器做多个网站中国建筑网址
  • 壁纸网站模板临沧网站建设ynyue
  • 购物网站怎么做SEO做游戏小网站是啥
  • 网站建设二级分销网站开发部门工资会计分录
  • 海丰建设局网站营销网站开发规划
  • 云南seo简单整站优化怎么在阿里云上做网站
  • 设计师可以做兼职的网站有哪些辽宁建设工程信息网直接发包工程
  • 深圳网站建设公司服务电子科技学校网站建设
  • godaddy 网站上传小公司做网站需要
  • 万州区建设局官方网站上传的网站怎么打开
  • 徐州建设工程交易网站找人做建筑施工的网站