Encoding.GetEncoding( " UTF-8 " )

时间:2022-01-09 05:08:14

这里的zipkin 是应用再owin模式下的简单web api集成,结合网上资料和本人动手测验考试作为本人条记使用,如有不敷的处所,欢迎列位大神提建议。

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

使用编程环境:

开发环境:vs2017

.net版本 4.6.1

1、创建控制台应用措施

Encoding.GetEncoding( " UTF-8 " )

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(":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 = ":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挪用告成!(请允许我打码失我的保藏栏-.-)

Encoding.GetEncoding( " UTF-8 " )

8、检察zipkin处事端发明,挪用陈迹已记录告成!

Encoding.GetEncoding( " UTF-8 " )