Ocelot简易教程(七)之配置文件数据库存储插件源码解析
Ocelot是为.net core量身定作的,目前是基于 netstandard2.0进行构建的。html
使用nuget安装Ocelot及其依赖项。您须要建立一个netstandard2.0项目并将其Package安装到项目中。而后按照下面的“启动”和“ 配置”节点启动并运行。
安装命令 Install-Package Ocelot
你能够经过下面的连接查看Ocelot的历史版本https://www.nuget.org/packages/Ocelot/ 目前最新版是10.0.4。最新版最近正在进行重构,更新比较频繁。nginx
如下配置是一个很是基础的Ocelot.json配置,他不会作任何事情,但却可让ocelot正常运行。数据库
{ "ReRoutes": [], "GlobalConfiguration": { "BaseUrl": "https://api.yilezhu.cn" } }
这个配置里面最重要的是BaseUrl。Ocelot须要知道它正在运行的URL,以便执行Header查找和替换以及某些管理配置。设置此URL时,它应该是客户端将看到Ocelot运行的外部URL,例如,若是您正在运行容器,则Ocelot可能会在URL上运行http://123.12.1.1:6543但在其前面有相似nginx的响应在https://api.yilezhu.cn。在这种状况下,Ocelot基本网址应为https://api.yilezhu.cn。json
若是因为某种缘由你正在使用容器而且但愿Ocelot在http://123.12.1.1:6543上响应客户端的请求, 那么你能够这样作可是若是要部署多个Ocelot,你可能但愿在命令行中传递它某种脚本。但愿您使用的任何调度程序均可以传递IP。c#
特别须要注意的是,这里的Ocelot.json配置文件须要在VS中右键修改成“始终复制”属性。api
官方文档是按照下面进行配置的。不过我的仍是习惯在Sartup.cs文件中进行相关的配置。博主就先贴出官方文档给出的配置方法。
而后在你的Program.cs你将按照如何代码进行配置。这里最主要的是AddOcelot() 添加 ocelot 服务), UseOcelot().Wait() (使用 Ocelot中间件).app
public class Program { public static void Main(string[] args) { new WebHostBuilder() .UseKestrel() .UseContentRoot(Directory.GetCurrentDirectory()) .ConfigureAppConfiguration((hostingContext, config) => { config .SetBasePath(hostingContext.HostingEnvironment.ContentRootPath) .AddJsonFile("appsettings.json", true, true) .AddJsonFile($"appsettings.{hostingContext.HostingEnvironment.EnvironmentName}.json", true, true) .AddJsonFile("ocelot.json") .AddEnvironmentVariables(); }) .ConfigureServices(s => { s.AddOcelot(); }) .ConfigureLogging((hostingContext, logging) => { //add your logging }) .UseIISIntegration() .Configure(app => { app.UseOcelot().Wait(); }) .Build() .Run(); }
我我的也比较习惯在Startup.cs中进行配置,不习惯在Program.cs中配置。下面是我配置的一种方式,固然你也能够自由发挥。async
public void ConfigureServices(IServiceCollection services) { services.AddMvc(); services.AddOcelot(new ConfigurationBuilder() .AddJsonFile("ocelot.json") .Build()); } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public async void Configure(IApplicationBuilder app, IHostingEnvironment env) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } await app.UseOcelot(); app.UseMvc(); }
今天只是给你们介绍Ocelot的很是很是简单地使用,能够说零配置,并介绍了官方的使用方法以及我平时的使用方式,只为了快速开始Ocelot,让项目可以跑起来。接下来咱们会详细的介绍Ocelot的配置。优化