zipkin+owin wep api集成

这里的zipkin 是应用再owin模式下的简单web api集成,结合网上资料和本人动手尝试作为本人笔记使用,如有不足的地方,欢迎各位大神提建议。

zipkin参考地址:https://github.com/openzipkin/zipkin4net

使用编程环境:

开发环境:vs2017

.net版本 4.6.1

1、创建控制台应用程序

2、这里通过NUGET引入 

a. Microsoft.AspNet.WebApi.OwinSelfHost 这个包 用来做 webapi宿主服务

b. zipkin4net 引入zipkin客户端方法

 3、添加Startup类 作为zipkin设置和宿主服务设置的主方法(这里“cxzczx” 为给本次痕迹的服务名,建议做成可配置)

using Newtonsoft.Json.Serialization;
using Owin;
using System.Web.Http;
using zipkin4net;
using zipkin4net.Tracers.Zipkin;
using zipkin4net.Transport.Http;

namespace ConsoleApp1
{
    public class Startup
    {
        public void Configuration(IAppBuilder appBuilder)
        {

            //注册成webapi
            HttpConfiguration config = new HttpConfiguration();
            config.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "api/{controller}/{action}",
                defaults: new { id = RouteParameter.Optional }
            );

            appBuilder.UseWebApi(config);
            var json = config.Formatters.JsonFormatter;
            json.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();



            var logger =new ConsoleLogger(); //实现ILogger 
            TraceManager.SamplingRate = 1.0f; //完全跟踪

            var httpSender = new HttpZipkinSender("http://localhost:9411", "application/json");//设置zipkin服务端地址
            var tracer = new ZipkinTracer(httpSender, new JSONSpanSerializer());
            TraceManager.RegisterTracer(tracer);
            TraceManager.Start(logger);

      

            //关闭
            // TraceManager.Stop();

            var trace = Trace.Create();
            trace.Record(Annotations.ServerRecv());
            trace.Record(Annotations.ServiceName("cxzczx"));
            trace.Record(Annotations.Rpc("GET"));
            trace.Record(Annotations.ServerSend());
            trace.Record(Annotations.Tag("http.url", "<url>")); //adds binary annotation

            //Trace.Current = trace;
        }
    }
}

4、在控制台的Program文件的main方法里面添加启动(baseAddress 请设置成你想设置的端口地址,注意不要与已存在的端口冲突)

    static void Main(string[] args)
        {
            string baseAddress = "http://localhost:7400/"; 

            // Start OWIN host 
            using (WebApp.Start<Startup>(url: baseAddress))
            {
                Console.WriteLine("Start server on {0}", baseAddress);
                Console.ReadLine();
            }
        }

5、再添加一个记录过程ConsoleLogger.cs

using System;
using zipkin4net;

namespace ConsoleApp1
{
    public class ConsoleLogger : ILogger
    {
        public void LogError(string message)
        {
            Console.Error.WriteLine(message);
        }

        public void LogInformation(string message)
        {
            Console.WriteLine(message);
        }

        public void LogWarning(string message)
        {
            Console.ForegroundColor = ConsoleColor.Yellow;
            Console.WriteLine(message);
            Console.ResetColor();
        }
    }
}

6、OK 此时webapi+zipkin已经注册完成,现在我们添加一个api接口来测试,新建HomeController

   public class HomeController : ApiController
    {
        [HttpGet]
        public HttpResponseMessage GetPhone(int id)
        {
            Random random = new Random();
            var product = new { id = id, name = "小米mix2", ran = random.Next(10000, 99999) };

            HttpResponseMessage result = new HttpResponseMessage();
            result.Content = new StringContent(JsonConvert.SerializeObject(product), Encoding.GetEncoding("UTF-8"), "application/json");
            return result;
        }




    }

 

7、进行测试 api调用成功!(请允许我打码掉我的收藏栏-.-)

8、查看zipkin服务端发现,调用痕迹已记录成功!

9、OK 简单的dome已完成,如需要更加详细痕迹记录请前前往https://github.com/openzipkin/zipkin4net 另外本人小菜一枚刚开始研究zipkin,结合网上各类资料自己动手试了一下,文中如有不足的地方欢迎指出,各位大神如有更好的解决方案欢迎交流。

 

posted @ 2018-02-07 11:28  爱如少年。  阅读(576)  评论(0编辑  收藏  举报