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

Ocelot(七)- 入门

入门

Ocelot仅适用于.NET Core,目前是为netstandard2.0构建的。如果Ocelot适合您,那么此文档可能会有用。

.NET 

安装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可能会在URL上运行http://123.12.1.1:6543但在其前面有类似nginx的响应在https上响应://api.mybusiness.com。在这种情况下,Ocelot基本网址应为https://api.mybusiness.com。

如果您正在使用容器并要求Ocelot在http://123.12.1.1:6543响应客户端, 那么您可以执行此操作,但是如果您要部署多个Ocelot,您可能希望在某种类型的命令行上传递它脚本。希望您使用的任何调度程序都可以通过IP。

程序

然后在您的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();
       }
   }

**Note:** When using ASP.NET Core 2.2 and you want to use In-Process hosting, replace **.UseIISIntegration()** with **.UseIIS()**, otherwise you'll get startup errors.

.NET 

安装NuGet包

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

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

配置

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

{
    "ReRoutes": [],
    "GlobalConfiguration": {} } 

程序

然后在您的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(); } } 

启动

使用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();
    }
}

这几乎是你入门需要的全部。

转载于:https://www.cnblogs.com/letyouknowdotnet/p/11019705.html

相关文章:

  • flask中的session
  • mk-parallel-dump 实验
  • 配置Windows环境的DNS轮询(简单的负载均衡)
  • Out of memory (Needed 16777224 bytes)的错误解决
  • Servlet、Filter、Listener总结
  • 《The One!团队》第八次作业:ALPHA冲刺(二)
  • HttpClient 4.3.3 cookie rejected的解决办法
  • 大象也能起舞,Citrix X1计划让你对笔记本电脑say good bye
  • VOA 2009/11/02 DEVELOPMENT REPORT - In Kenya, a Better Life Through Mobile Money
  • 安装 chkrootkit报错:/usr/bin/ld: cannot find -lc
  • 大型网站技术架构(八)网站的安全架构
  • 32位程序在64位系统上获取系统安装时间
  • 怎样 配置这个网络拓扑
  • 关于烂代码的那些事(中)
  • Spring学习总结(4)——Spring AOP教程
  • docker python 配置
  • Facebook AccountKit 接入的坑点
  • JavaScript/HTML5图表开发工具JavaScript Charts v3.19.6发布【附下载】
  • Lucene解析 - 基本概念
  • python大佬养成计划----difflib模块
  • Shadow DOM 内部构造及如何构建独立组件
  • Spring Security中异常上抛机制及对于转型处理的一些感悟
  • Vue全家桶实现一个Web App
  • 阿里云爬虫风险管理产品商业化,为云端流量保驾护航
  • 第2章 网络文档
  • 动态魔术使用DBMS_SQL
  • 利用DataURL技术在网页上显示图片
  • 数据结构java版之冒泡排序及优化
  • 我看到的前端
  • 小程序开发之路(一)
  • 写给高年级小学生看的《Bash 指南》
  • 一些关于Rust在2019年的思考
  • 如何用纯 CSS 创作一个菱形 loader 动画
  • # 手柄编程_北通阿修罗3动手评:一款兼具功能、操控性的电竞手柄
  • #DBA杂记1
  • (2009.11版)《网络管理员考试 考前冲刺预测卷及考点解析》复习重点
  • (C#)一个最简单的链表类
  • (Redis使用系列) Springboot 实现Redis消息的订阅与分布 四
  • (个人笔记质量不佳)SQL 左连接、右连接、内连接的区别
  • (接口自动化)Python3操作MySQL数据库
  • (一)使用IDEA创建Maven项目和Maven使用入门(配图详解)
  • (转)为C# Windows服务添加安装程序
  • *_zh_CN.properties 国际化资源文件 struts 防乱码等
  • .bat批处理(四):路径相关%cd%和%~dp0的区别
  • .net 4.0 A potentially dangerous Request.Form value was detected from the client 的解决方案
  • .net core 6 使用注解自动注入实例,无需构造注入 autowrite4net
  • .Net CoreRabbitMQ消息存储可靠机制
  • .net FrameWork简介,数组,枚举
  • .net mvc actionresult 返回字符串_.NET架构师知识普及
  • .NET导入Excel数据
  • .Net高阶异常处理第二篇~~ dump进阶之MiniDumpWriter
  • .NET国产化改造探索(三)、银河麒麟安装.NET 8环境
  • .secret勒索病毒数据恢复|金蝶、用友、管家婆、OA、速达、ERP等软件数据库恢复
  • [【JSON2WEB】 13 基于REST2SQL 和 Amis 的 SQL 查询分析器
  • [AutoSar NVM] 存储架构