From 93daa022bc5a71cc6b7a2f3e5bc11cee39135d7f Mon Sep 17 00:00:00 2001 From: "david.yan" Date: Sun, 30 Mar 2025 13:23:46 +0800 Subject: [PATCH] add gateway --- conf/new.go | 10 +++++----- conf/types.go | 3 ++- service/meta.go | 11 +++++++++++ service/service.go | 43 +++++++++++++++++++++++++++++++++++-------- 4 files changed, 53 insertions(+), 14 deletions(-) diff --git a/conf/new.go b/conf/new.go index ffc0182..c3069d5 100644 --- a/conf/new.go +++ b/conf/new.go @@ -59,17 +59,17 @@ func NotNil(values ...string) { } } -func PrintInfo(ip string, port int) { +func PrintInfo(addr string) { print.Success("[BSM - %s] Config Check Success.", vars.ServiceKey) print.Info("[BSM - %s] Service Name: %s", vars.ServiceKey, vars.ServiceKey) print.Info("[BSM - %s] Runtime Mode: %s", vars.ServiceKey, env.Runtime.Mode) - print.Info("[BSM - %s] Listen Addr: %s:%d", vars.ServiceKey, ip, port) } -func CheckPort(port int) int { - if port <= 0 || port >= 65535 { +func CheckPort(port string) string { + if port == "" { r := rand.New(rand.NewPCG(1000, uint64(time.Now().UnixNano()))) - return r.IntN(65535-1024) + 1024 // 生成1024到65535之间的随机端口 + p := r.IntN(65535-1024) + 1024 // 生成1024到65535之间的随机端口 + return utils.Int2String(p) } return port } diff --git a/conf/types.go b/conf/types.go index 6352262..a119def 100644 --- a/conf/types.go +++ b/conf/types.go @@ -2,10 +2,11 @@ package conf type Base struct { Service string `yaml:"Service"` // 服务名称 - Port int `yaml:"Port"` // 服务监听端口,0为自动随机端口 + Port string `yaml:"Port"` // 服务监听端口,0为自动随机端口 Cache string `yaml:"Cache"` // REDIS缓存 SecretKey string `yaml:"SecretKey"` // 服务秘钥 BindIP string `yaml:"BindIP"` // 绑定IP + Addr string `yaml:"Addr"` } type DBConf struct { diff --git a/service/meta.go b/service/meta.go index 4fe4fd8..d193ea5 100644 --- a/service/meta.go +++ b/service/meta.go @@ -30,6 +30,17 @@ func ParseMetaCtx(ctx context.Context, opts *ParseOptions) (*Meta, error) { return nil, errcode.ErrJWTAuthNotFound } + // 安全获取 metadata 中的值 + identityValues := md.Get("authorization_identity") + clientValues := md.Get("client") + + if len(identityValues) == 0 { + return nil, errcode.ErrJWTAuthNotFound + } + if len(clientValues) == 0 { + return nil, errcode.ErrJWTAuthNotFound + } + meta := &Meta{ IDENTITY: md["authorization_identity"][0], CLIENT: md["client"][0], diff --git a/service/service.go b/service/service.go index 3a9e74d..b7aafff 100644 --- a/service/service.go +++ b/service/service.go @@ -1,12 +1,17 @@ package service import ( + "context" "log" "net" "os" "strconv" "strings" + "net/http" + + gwRuntime "github.com/grpc-ecosystem/grpc-gateway/v2/runtime" + "git.apinb.com/bsm-sdk/core/conf" "git.apinb.com/bsm-sdk/core/env" "git.apinb.com/bsm-sdk/core/print" @@ -25,9 +30,12 @@ type ( } Options struct { - Addr string - Conf *conf.MicroServiceConf - EtcdClient *clientv3.Client + Addr string + EtcdClient *clientv3.Client + MsConf *conf.MicroServiceConf + GatewayConf *conf.GatewayConf + GatewayCtx context.Context + GatewayMux *gwRuntime.ServeMux } ) @@ -43,11 +51,12 @@ func (s *Service) Start() { print.Info("[BSM - %s] Service Starting ...", vars.ServiceKey) // register to etcd. - if s.Opts.Conf != nil && s.Opts.Conf.Enable { + if s.Opts.MsConf != nil && s.Opts.MsConf.Enable { if s.Opts.EtcdClient == nil { print.Error("[BSM Register] Etcd Client is nil.") os.Exit(1) } + print.Info("[BSM - %s] Registering Service to Etcd ...", vars.ServiceKey) // get methods methods := FoundGrpcMethods(s.GrpcSrv) @@ -62,7 +71,7 @@ func (s *Service) Start() { anonKey := vars.ServiceRootPrefix + "Router/" + env.Runtime.Workspace + "/" - register.SetAnonymous(anonKey, s.Opts.Conf.Anonymous) + register.SetAnonymous(anonKey, s.Opts.MsConf.Anonymous) // service register lease go register.ListenLeaseRespChan() @@ -74,11 +83,29 @@ func (s *Service) Start() { panic(err) } - print.Success("[BSM - %s] Service Grpc Register & Runing Success!", vars.ServiceKey) - if err := s.GrpcSrv.Serve(tcpListen); err != nil { - panic(err) + go func() { + if err := s.GrpcSrv.Serve(tcpListen); err != nil { + panic(err) + } + }() + print.Success("[BSM - %s] Grpc %s Runing Success !", vars.ServiceKey, s.Opts.Addr) + + if s.Opts.GatewayConf != nil && s.Opts.GatewayConf.Enable { + addr := Addr("0.0.0.0", s.Opts.GatewayConf.Port) + go s.Gateway(s.Opts.Addr, addr) + + print.Success("[BSM - %s] Http %s Runing Success!", vars.ServiceKey, addr) } + select {} +} + +func (s *Service) Gateway(grpcAddr string, httpAddr string) { + // 1. 定义一个context + _, cancel := context.WithCancel(s.Opts.GatewayCtx) + defer cancel() + + http.ListenAndServe(httpAddr, s.Opts.GatewayMux) } func (s *Service) Stop() {