Ocelot中文文档入门

时间:2023-03-09 15:14:34
Ocelot中文文档入门

入门

Ocelot仅适用于.NET Core,目前是根据netstandard2.0构建的,如果Ocelot适合您,这个文档可能会有用。

.NET Core 2.1

安装NuGet包

使用nuget安装Ocelot及其依赖项。 您需要创建一个netstandard2.0项目并将其打包到其中。 然后按照下面的“启动”和“配置”部分启动并运行。

Install-Package Ocelot

所有版本都可以在这里找到

配置

以下是一个非常基本的ocelot.json。 它不会做任何事情,但应该让Ocelot开始。

{
"ReRoutes": [],
"GlobalConfiguration": {
"BaseUrl": "https://api.mybusiness.com"
}
}

这里要注意的最重要的是BaseUrl。 Ocelot需要知道它正在运行的URL,以便执行Header查找和替换以及某些管理配置。 设置此URL时,它应该是客户端将看到运行Ocelot的外部URL,例如 如果你正在运行容器,Ocelot可能会在网址http://123.12.1.1:6543上运行,但在https://api.mybusiness.com上响应它之前有类似nginx的东西。 在这种情况下,Ocelot基本网址应为https://api.mybusiness.com。

如果由于某种原因你正在使用容器并且希望Ocelot在http://123.12.1.1:6543上响应客户端,那么你可以这样做但是如果要部署多个Ocelot,你可能希望在命令行中传递它 某种脚本。 希望您使用的任何调度程序都可以传递IP。
Program.cs文件
然后在Program.cs中,您将需要以下内容。 需要注意的主要事项是AddOcelot()(添加ocelot服务),UseOcelot()。Wait()(设置所有Ocelot中间件)。
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();
}
}

.NET Core 1.0

安装NuGet包

使用nuget安装Ocelot及其依赖。 您需要创建一个netcoreapp1.0 + projct并将包带入其中。 然后按照下面的“启动”和“配置”部分启动并运行。 请注意,您需要从NuGet Feed中选择一个Ocelot包。

所有版本都可以在这里找到

配置
 
以下是一个非常基本的ocelot.json。 它不会做任何事情,但应该让Ocelot开始。
{
{ "ReRoutes": [],
"GlobalConfiguration": {}
}
Program.cs文件
 
然后在Program.cs中,您将需要以下内容。
public class Program
{
public static void Main(string[] args)
{
IWebHostBuilder builder = new WebHostBuilder(); builder.ConfigureServices(s => {
}); builder.UseKestrel()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseStartup<Startup>(); var host = builder.Build(); host.Run();
}
}
Startup.cs文件 使用json文件进行配置的示例启动如下所示。 public class Startup
{
public Startup(IHostingEnvironment env)
{
var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
.AddJsonFile("ocelot.json")
.AddEnvironmentVariables(); Configuration = builder.Build();
} public IConfigurationRoot Configuration { get; } public void ConfigureServices(IServiceCollection services)
{
services.AddOcelot(Configuration);
} public void Configure(IApplicationBuilder app)
{
app.UseOcelot().Wait();
}
}
以上这些就是Ocelot基本入门内容。喜欢就收藏此文。版权所有,禁止未经授权的复制转载。详细的中文文档查阅 http://nopapp.com/Blog/Article/Ocelot-Basic