main.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. package main
  2. import (
  3. "mop-account-system/common"
  4. "mop-account-system/config"
  5. "mop-account-system/router"
  6. "net"
  7. //"time"
  8. "gitlab.finogeeks.club/finclip-backend/apm"
  9. "github.com/sirupsen/logrus"
  10. "github.com/gin-gonic/gin"
  11. "mop-account-system/consul"
  12. "mop-account-system/handler/organ"
  13. //"google.golang.org/grpc/keepalive"
  14. pb "mop-account-system/protobuf/golang"
  15. "google.golang.org/grpc"
  16. "google.golang.org/grpc/reflection"
  17. )
  18. func InitGrpc() {
  19. cfg := config.GetConfig()
  20. if cfg.ConsulAddr == "" {
  21. return
  22. }
  23. consul.InitConsul()
  24. lis, err := net.Listen("tcp", ":"+cfg.GrpcPort)
  25. if err != nil {
  26. panic(err)
  27. }
  28. s := grpc.NewServer()
  29. /*grpc.KeepaliveParams(keepalive.ServerParameters{
  30. MaxConnectionIdle: 10 * time.Minute, //这个连接最大的空闲时间,超过就释放,解决proxy等到网络问题(不通知grpc的client和server)
  31. })*/
  32. pb.RegisterMopAccountSystemServer(s, &organ.GrpcServer{})
  33. reflection.Register(s)
  34. if err := s.Serve(lis); err != nil {
  35. panic(err)
  36. }
  37. }
  38. func main() {
  39. cfg := config.GetConfig()
  40. go InitGrpc()
  41. gin.SetMode(cfg.Mode)
  42. g := gin.Default()
  43. apm.ApmClient().InjectHttpMiddleware(g)
  44. router.Load(g)
  45. common.GinLog.Info("start server v3")
  46. err := g.Run(":" + cfg.HttpPort)
  47. if err != nil {
  48. common.GinLog.WithFields(logrus.Fields{
  49. "error": err.Error(),
  50. "start_port": cfg.HttpPort,
  51. }).Error("server start error!")
  52. panic(err.Error())
  53. }
  54. }