0386740c3336cc15daabe3c77d6b536fad2eeda5.svn-base 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. package main
  2. import (
  3. "context"
  4. "flag"
  5. "fmt"
  6. "github.com/go-kit/kit/log"
  7. kitzipkin "github.com/go-kit/kit/tracing/zipkin"
  8. "github.com/longjoy/micro-go-course/section35/zipkin-kit/pb"
  9. edpts "github.com/longjoy/micro-go-course/section35/zipkin-kit/string-service/endpoint"
  10. "github.com/longjoy/micro-go-course/section35/zipkin-kit/string-service/service"
  11. "github.com/openzipkin/zipkin-go"
  12. zipkinhttp "github.com/openzipkin/zipkin-go/reporter/http"
  13. "google.golang.org/grpc"
  14. "net"
  15. "net/http"
  16. "os"
  17. "os/signal"
  18. "syscall"
  19. )
  20. func main() {
  21. var (
  22. consulHost = flag.String("consul.host", "114.67.98.210", "consul ip address")
  23. consulPort = flag.String("consul.port", "8500", "consul port")
  24. serviceHost = flag.String("service.host", "localhost", "service ip address")
  25. servicePort = flag.String("service.port", "9009", "service port")
  26. zipkinURL = flag.String("zipkin.url", "http://114.67.98.210:9411/api/v2/spans", "Zipkin server url")
  27. grpcAddr = flag.String("grpc", ":9008", "gRPC listen address.")
  28. )
  29. flag.Parse()
  30. ctx := context.Background()
  31. errChan := make(chan error)
  32. var logger log.Logger
  33. {
  34. logger = log.NewLogfmtLogger(os.Stderr)
  35. logger = log.With(logger, "ts", log.DefaultTimestampUTC)
  36. logger = log.With(logger, "caller", log.DefaultCaller)
  37. }
  38. var zipkinTracer *zipkin.Tracer
  39. {
  40. var (
  41. err error
  42. hostPort = *serviceHost + ":" + *servicePort
  43. serviceName = "string-service"
  44. useNoopTracer = (*zipkinURL == "")
  45. reporter = zipkinhttp.NewReporter(*zipkinURL)
  46. )
  47. defer reporter.Close()
  48. zEP, _ := zipkin.NewEndpoint(serviceName, hostPort)
  49. zipkinTracer, err = zipkin.NewTracer(
  50. reporter, zipkin.WithLocalEndpoint(zEP), zipkin.WithNoopTracer(useNoopTracer),
  51. )
  52. if err != nil {
  53. logger.Log("err", err)
  54. os.Exit(1)
  55. }
  56. if !useNoopTracer {
  57. logger.Log("tracer", "Zipkin", "type", "Native", "URL", *zipkinURL)
  58. }
  59. }
  60. var svc service.Service
  61. svc = service.StringService{}
  62. // add logging middleware to service
  63. svc = LoggingMiddleware(logger)(svc)
  64. endpoint := edpts.MakeStringEndpoint(ctx, svc)
  65. endpoint = kitzipkin.TraceEndpoint(zipkinTracer, "string-endpoint")(endpoint)
  66. //创建健康检查的Endpoint
  67. healthEndpoint := edpts.MakeHealthCheckEndpoint(svc)
  68. healthEndpoint = kitzipkin.TraceEndpoint(zipkinTracer, "health-endpoint")(healthEndpoint)
  69. //把算术运算Endpoint和健康检查Endpoint封装至StringEndpoints
  70. endpts := edpts.StringEndpoints{
  71. StringEndpoint: endpoint,
  72. HealthCheckEndpoint: healthEndpoint,
  73. }
  74. //创建http.Handler
  75. r := MakeHttpHandler(ctx, endpts, zipkinTracer, logger)
  76. //创建注册对象
  77. registar := Register(*consulHost, *consulPort, *serviceHost, *servicePort, logger)
  78. go func() {
  79. fmt.Println("Http Server start at port:" + *servicePort)
  80. //启动前执行注册
  81. registar.Register()
  82. handler := r
  83. errChan <- http.ListenAndServe(":"+*servicePort, handler)
  84. }()
  85. //grpc server
  86. go func() {
  87. fmt.Println("grpc Server start at port" + *grpcAddr)
  88. listener, err := net.Listen("tcp", *grpcAddr)
  89. if err != nil {
  90. errChan <- err
  91. return
  92. }
  93. serverTracer := kitzipkin.GRPCServerTrace(zipkinTracer, kitzipkin.Name("string-grpc-transport"))
  94. handler := NewGRPCServer(ctx, endpts, serverTracer)
  95. gRPCServer := grpc.NewServer()
  96. pb.RegisterStringServiceServer(gRPCServer, handler)
  97. errChan <- gRPCServer.Serve(listener)
  98. }()
  99. go func() {
  100. c := make(chan os.Signal, 1)
  101. signal.Notify(c, syscall.SIGINT, syscall.SIGTERM)
  102. errChan <- fmt.Errorf("%s", <-c)
  103. }()
  104. error := <-errChan
  105. //服务退出取消注册
  106. registar.Deregister()
  107. fmt.Println(error)
  108. }