官方文档:http://ocelot.readthedocs.io/en/latest/introduction/gettingstarted.html

新建两个Asp.net core API项目

API1使用5001端口

API2使用5002端口,为了有所区别,改下返回数据

// GET api/values
        [HttpGet]
        public IEnumerable<string> Get()
        {
            return new string[] { "value1", "value2","value3" };
        }

新建asp.net core 空项目APIGate

修改Program

public static IWebHost BuildWebHost(string[] args) =>
            WebHost.CreateDefaultBuilder(args)
                .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("configuration.json")
                        .AddEnvironmentVariables();
                })
                .UseStartup<Startup>()
                .Build();

修改Startup

public void ConfigureServices(IServiceCollection services)
        {
            services.AddOcelot();
        }

        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            app.UseOcelot().Wait();
        }

添加configuration.json

{
  "ReRoutes": [
    {
      "DownstreamPathTemplate": "/api/values",
      "DownstreamScheme": "http",
      "UpstreamPathTemplate": "/api/values",
      "UpstreamHttpMethod": [ "Get" ],
      "DownstreamHostAndPorts": [
        {
          "Host": "localhost",

        }
      ]
    },
    {
      "DownstreamPathTemplate": "/api/values",
      "DownstreamScheme": "http",
      "UpstreamPathTemplate": "/api/values2",
      "UpstreamHttpMethod": [ "Get" ],
      "DownstreamHostAndPorts": [
        {
          "Host": "localhost",

        }
      ]
    }
  ],
  "GlobalConfiguration": {
  }
}

打开浏览器http://localhost:56235/api/values

打开http://localhost:56235/api/values2

结合Butterfly实现微服务跟踪

下载Butterfly:https://github.com/ButterflyAPM/butterfly/releases

解压后执行

dotnet Butterfly.Web.dll --EnableHttpCollector=true

这里要主要加上后面的参数,否则是监控不到Http的请求的

接下来在API网关和API服务处添加Butterfly包

Install-Package Butterfly.Client.AspNetCore

在API网关Startup中添加服务

public void ConfigureServices(IServiceCollection services)
{
   //your other code
  services.AddButterfly(option =>
  {
      option.CollectorUrl = "http://localhost:9618";
      option.Service = "my service";
  });
}

在API服务中添加代码同上,只是服务名不同

修改configuration.json,开启tracing

{
  "ReRoutes": [
    {
      "DownstreamPathTemplate": "/api/values",
      "DownstreamScheme": "http",
      "UpstreamPathTemplate": "/api/values",
      "UpstreamHttpMethod": [ "Get" ],
      "DownstreamHostAndPorts": [
        {
          "Host": "localhost",

        }
      ],
      "HttpHandlerOptions": {
        "UseTracing": true
      }
    },
    {
      "DownstreamPathTemplate": "/api/values",
      "DownstreamScheme": "http",
      "UpstreamPathTemplate": "/api/values2",
      "UpstreamHttpMethod": [ "Get" ],
      "DownstreamHostAndPorts": [
        {
          "Host": "localhost",

        }
      ],
      "HttpHandlerOptions": {
        "UseTracing": true
      }
    }
  ],
  "GlobalConfiguration": {
  }
}

按上文请求http://localhost:56235/api/values2,然后打开http://localhost:9618/tracing/traces

可以看到已经跟踪到了请求

点击进去可以看到服务的调用

接下来按https://github.com/ButterflyAPM/butterfly-csharp 开启5001和5002,然后访问http://localhost:56235/api/values

Ocelot 使用的更多相关文章

  1. 编译器开发系列--Ocelot语言7.中间代码

    Ocelot的中间代码是仿照国外编译器相关图书Modern Compiler Implementation 中所使用的名为Tree 的中间代码设计的.顾名思义,Tree 是一种树形结构,其特征是简单, ...

  2. 编译器开发系列--Ocelot语言6.静态类型检查

    关于"静态类型检查",想必使用C 或Java 的各位应该非常熟悉了.在此过程中将检查表达式的类型,发现类型不正确的操作时就会报错.例如结构体之间无法用+ 进行加法运算,指针和数值之 ...

  3. 编译器开发系列--Ocelot语言1.抽象语法树

    从今天开始研究开发自己的编程语言Ocelot,从<自制编译器>出发,然后再自己不断完善功能并优化. 编译器前端简单,就不深入研究了,直接用现成的一款工具叫JavaCC,它可以生成抽象语法树 ...

  4. API网关Ocelot 使用Polly 处理部分失败问题

    在实现API Gateway过程中,另外一个需要考虑的问题就是部分失败.这个问题发生在分布式系统中当一个服务调用另外一个服务超时或者不可用的情况.API Gateway不应该被阻断并处于无限期等待下游 ...

  5. Ocelot API网关的实现剖析

    在微软Tech Summit 2017 大会上和大家分享了一门课程<.NET Core 在腾讯财付通的企业级应用开发实践>,其中重点是基于ASP.NET Core打造可扩展的高性能企业级A ...

  6. Asp.Net Core API网关Ocelot

    首先,让我们简单了解下什么是API网关? API网关是一个服务器,是系统的唯一入口.从面向对象设计的角度看,它与外观模式类似.API网关封装了系统内部架构,为每个客户端提供一个定制的API.它可能还具 ...

  7. Ocelot网关

    Ocelot是一个.net core框架下的网关的开源项目,下图是官方给出的基础实现图,即把后台的多个服务统一到网关处,前端应用:桌面端,web端,app端都只用访问网关即可. Ocelot的实现原理 ...

  8. Ocelot 集成Butterfly 实现分布式跟踪

    微服务,通常都是用复杂的.大规模分布式集群来实现的.微服务构建在不同的软件模块上,这些软件模块,有可能是由不同的团队开发.可能使用不同的编程语言来实现.有可能布在了几千台服务器,横跨多个不同的数据中心 ...

  9. 给Ocelot做一个Docker 镜像

    写在前面 在微服务架构中,ApiGateway起到了承前启后,不仅可以根据客户端进行分类,也可以根据功能业务进行分类,而且对于服务调用服务也起到了很好的接口作用.目前在各个云端中,基本上都提供了Api ...

  10. .NET Core开源API网关 – Ocelot中文文档

    Ocelot是一个用.NET Core实现并且开源的API网关,它功能强大,包括了:路由.请求聚合.服务发现.认证.鉴权.限流熔断.并内置了负载均衡器与Service Fabric.Butterfly ...

随机推荐

  1. NET Core小细节杂记

    1.中文编码问题: 01.在net core中,使用中文的编码,要先进行注册: //CodePagesEncodingProvider在包System.Text.Encoding.CodePages中 ...

  2. odoo研究学习:刷新本地模块列表都干了什么事?

    模块信息存储在ir.module.module 数据表中 平时在开发过程中经常会刷新本地模块列表,例如:新增了模块.更新了模块基础信息.更换了模块图标等等,在点击‘更新’按钮的时候odoo平台到底干了 ...

  3. github windows配置以及ssh生成 Permission denied (publickey)

    1:进入cmd命令下,或者可以使用GIt工具   (如果出现了 Permission denied 或者配置多个SSH Key跳第6步) git工具  下载地址:https://git-scm.com ...

  4. $.fn.extend() 问:我来这个世上到底是干嘛的?

    好好好 乖  本宝宝来告诉你  你来是干嘛的啊~ 话不多说 直接上码 当然如下代码是在jquery环境下运行的 HTML JS 完事~~~ 当你点击div元素的时候  你会发现弹出来“我被单击了”这句 ...

  5. 通过Solr所提供的Dataimporthandler实现数据源的导入

    如需要使用到Solr中的dataimporthandler增量导入功能,则还需要引入两个所依赖的jar包,在上一篇随笔中所提到的下载的Solr项目文件solr-4.10.3\dist目录下可以找到所依 ...

  6. 地址重写 No input file specified的解决方法

    转载自:http://blog.csdn.net/williamsblog/article/details/37532737 (一)IIS Noinput file specified 方法一:改PH ...

  7. js 新增标签、标签属性

    var table = document.getElementById('content_list'); console.log(ret.data.length); ;i<ret.data.le ...

  8. Mingw下载

    http://ismdeep.oss-cn-shenzhen.aliyuncs.com/x86_64-5.3.0-release-posix-seh-rt_v4-rev0.7z

  9. js-function复制变量值和传递参数

    <title>function复制变量值</title></head><body> <script> var a={ num:10 } // ...

  10. subarray sum

    public class Solution { /* * @param nums: A list of integers * @return: A list of integers includes ...