微服务网关从零搭建——(六)ocelot配置追踪功能

butterfly 准备工作

首先下载buterfly release版本 

解压并通过命令启动:dotnet Butterfly.Web.dll --EnableHttpCollector=true

可以采用bat文件的方式 

cd C:UsersLenovoDesktoputterfly-web-preview-0.0.8
dotnet Butterfly.Web.dll --EnableHttpCollector=true

其中进入路径需要自行替换 

代码配置

测试demo中 12.0和 7.0 使用方法不完全一致 

7.0的ocelot 使用时 只需要在 startup中的 ConfigureServices 

添加如下代码

 services.AddOcelot(Configuration)
                .AddOpenTracing(option =>
                {
                    //this is the url that the butterfly collector server is running on...
                    option.CollectorUrl = "http://192.168.66.241:9618";
                    option.Service = "Ocelot";
                });

而 12.0 的时候如果这么写 会出现 找不到AddOpenTracing 方法的问题 

需要添加nuget包 Butterfly.Client.AspNetCore   

引用0.0.8时可能会出现无法追踪到请求的问题 引用0.0.7时 可能出现 依赖视图无法显示问题  具体使用哪个需要自行权衡

使用代码依然在startup中配置 

  services.AddButterfly(option =>
            {
                //增加日志
                //this is the url that the butterfly collector server is running on...
                option.CollectorUrl = Configuration.GetSection("Setting:Butterfly:CollectorUrl").Value;
                option.Service = Configuration.GetSection("Setting:Butterfly:Service").Value;
            });

然后在appsettings.json 中加入

 //Butterfly日志追踪
    "Butterfly": {
      "CollectorUrl": "http://localhost:9618", //追踪地址
      "Service": "OcelotButterfly" //显示名称
    }

如图 :

最后在 configuration.json 中 ReRoutes内 

为每个服务 增加内容

 "HttpHandlerOptions": {
        "UseTracing": true // use butterfly to tracing request chain
      }

如图所示:

测试效果

 访问 http://localhost:9618即可

原文地址:https://www.cnblogs.com/nontracey/p/9969490.html