如何快速掌握.Net Core微服务网关Ocelot的基础知识并成功集成?

2026-04-01 10:201阅读0评论SEO教程
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计548个文字,预计阅读时间需要3分钟。

如何快速掌握.Net Core微服务网关Ocelot的基础知识并成功集成?

网关是什么?简单来说,网关就是暴露给外部的请求入口。就像门卫一样,外面的人想要进来,必须先经过门卫。网关并不一定必需,后端服务可以通过http协议很好地向客户端提供服务。

网关是什么

简单来说,网关就是暴露给外部的请求入口。就和门卫一样,外面的人想要进来,必须要经过门卫。当然,网关并不一定是必须的,后端服务通过github.com/ThreeMammals/Ocelot

ocelot除了十分契合.net开发者以外,功能强大,包含:路由、认证、请求聚合、限流熔断、服务发现、鉴权,还有内置负载均衡器、Consul集成等等。

当然了,api网关不止这一款,市面上还有kong之类的,随自己喜好就好。

ocelot集成

首先明确一点,网关应该作为独立进程存在。那么我们先新建一个.net core3.1项目,然后添加nuget包:

如何快速掌握.Net Core微服务网关Ocelot的基础知识并成功集成?

关于版本,选择当前所能支持的最新版即可。

添加好nuget包以后,需要修改StartUp:

// This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { services.AddOcelot(); //services.AddControllers(); } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { app.UseOcelot().Wait(); //if (env.IsDevelopment()) //{ // app.UseDeveloperExceptionPage(); //} //app.UseHttpsRedirection(); //app.UseRouting(); //app.UseAuthorization(); //app.UseEndpoints(endpoints => //{ // endpoints.MapControllers(); //}); }

这里不要惊讶,因为走了网关就不会再走默认的管道了。UseOcelot().Wait() 表示设置ocelot所有的中间件,而ocelot也提供了很多集成中间件的库,就像这些:

现在,想要让ocelot成功运行,还需要新增配置文件,并在Program新增配置文件的引用:

public static IHostBuilder CreateHostBuilder(string[] args) => Host.CreateDefaultBuilder(args) .ConfigureAppConfiguration(config => { config.AddJsonFile("ocelotConfig.json", optional: false, reloadOnChange: true); }) .ConfigureWebHostDefaults(webBuilder => { webBuilder.UseStartup<Startup>(); });

配置文件:

{ "Routes": [ { "DownstreamPathTemplate": "/{url}", //服务地址--url变量 "DownstreamScheme": "123.123.123.123:5050/所有】并回传结果,支持ocelot.readthedocs.io/en/latest/

到此这篇关于.Net Core微服务网关Ocelot基础介绍及集成的文章就介绍到这了。希望对大家的学习有所帮助,也希望大家多多支持易盾网络。

本文共计548个文字,预计阅读时间需要3分钟。

如何快速掌握.Net Core微服务网关Ocelot的基础知识并成功集成?

网关是什么?简单来说,网关就是暴露给外部的请求入口。就像门卫一样,外面的人想要进来,必须先经过门卫。网关并不一定必需,后端服务可以通过http协议很好地向客户端提供服务。

网关是什么

简单来说,网关就是暴露给外部的请求入口。就和门卫一样,外面的人想要进来,必须要经过门卫。当然,网关并不一定是必须的,后端服务通过github.com/ThreeMammals/Ocelot

ocelot除了十分契合.net开发者以外,功能强大,包含:路由、认证、请求聚合、限流熔断、服务发现、鉴权,还有内置负载均衡器、Consul集成等等。

当然了,api网关不止这一款,市面上还有kong之类的,随自己喜好就好。

ocelot集成

首先明确一点,网关应该作为独立进程存在。那么我们先新建一个.net core3.1项目,然后添加nuget包:

如何快速掌握.Net Core微服务网关Ocelot的基础知识并成功集成?

关于版本,选择当前所能支持的最新版即可。

添加好nuget包以后,需要修改StartUp:

// This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { services.AddOcelot(); //services.AddControllers(); } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { app.UseOcelot().Wait(); //if (env.IsDevelopment()) //{ // app.UseDeveloperExceptionPage(); //} //app.UseHttpsRedirection(); //app.UseRouting(); //app.UseAuthorization(); //app.UseEndpoints(endpoints => //{ // endpoints.MapControllers(); //}); }

这里不要惊讶,因为走了网关就不会再走默认的管道了。UseOcelot().Wait() 表示设置ocelot所有的中间件,而ocelot也提供了很多集成中间件的库,就像这些:

现在,想要让ocelot成功运行,还需要新增配置文件,并在Program新增配置文件的引用:

public static IHostBuilder CreateHostBuilder(string[] args) => Host.CreateDefaultBuilder(args) .ConfigureAppConfiguration(config => { config.AddJsonFile("ocelotConfig.json", optional: false, reloadOnChange: true); }) .ConfigureWebHostDefaults(webBuilder => { webBuilder.UseStartup<Startup>(); });

配置文件:

{ "Routes": [ { "DownstreamPathTemplate": "/{url}", //服务地址--url变量 "DownstreamScheme": "123.123.123.123:5050/所有】并回传结果,支持ocelot.readthedocs.io/en/latest/

到此这篇关于.Net Core微服务网关Ocelot基础介绍及集成的文章就介绍到这了。希望对大家的学习有所帮助,也希望大家多多支持易盾网络。