add gateway
This commit is contained in:
parent
ca00b34e24
commit
93daa022bc
10
conf/new.go
10
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
|
||||
}
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -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],
|
||||
|
|
|
@ -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() {
|
||||
|
|
Loading…
Reference in New Issue