main.go 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. package main
  2. import (
  3. "context"
  4. "flag"
  5. "fmt"
  6. "github.com/afex/hystrix-go/hystrix"
  7. "github.com/longjoy/micro-go-course/section24/goods/endpoint"
  8. "github.com/longjoy/micro-go-course/section24/goods/service"
  9. "github.com/longjoy/micro-go-course/section24/goods/transport"
  10. "log"
  11. "net/http"
  12. "os"
  13. "os/signal"
  14. "strconv"
  15. "syscall"
  16. )
  17. func main() {
  18. servicePort := flag.Int("service.port", 12312, "service port")
  19. flag.Parse()
  20. errChan := make(chan error)
  21. srv := service.NewGoodsServiceImpl()
  22. endpoints := endpoint.GoodsEndpoints{
  23. GoodsDetailEndpoint: endpoint.MakeGoodsDetailEndpoint(srv),
  24. }
  25. handler := transport.MakeHttpHandler(context.Background(), &endpoints)
  26. hystrix.ConfigureCommand("Comments", hystrix.CommandConfig{
  27. RequestVolumeThreshold: 4,
  28. })
  29. go func() {
  30. errChan <- http.ListenAndServe(":"+strconv.Itoa(*servicePort), handler)
  31. }()
  32. go func() {
  33. // 监控系统信号,等待 ctrl + c 系统信号通知服务关闭
  34. c := make(chan os.Signal, 1)
  35. signal.Notify(c, syscall.SIGINT, syscall.SIGTERM)
  36. errChan <- fmt.Errorf("%s", <-c)
  37. }()
  38. err := <-errChan
  39. log.Printf("listen err : %s", err)
  40. }