Some checks are pending
/ build-services (app/admin_service/Dockerfile, admin, admin) (push) Waiting to run
/ build-services (app/auth_service/Dockerfile, auth, auth) (push) Waiting to run
/ build-services (app/ecpm_service/Dockerfile, ecpm, ecpm) (push) Waiting to run
/ build-services (app/ranking_service/Dockerfile, ranking, ranking) (push) Waiting to run
/ build-services (app/user_service/Dockerfile, user, user) (push) Waiting to run
/ start-services (push) Blocked by required conditions
76 lines
1.9 KiB
Go
76 lines
1.9 KiB
Go
package svc
|
|
|
|
import (
|
|
"context"
|
|
"gitea.youtukeji.com.cn/youtu/youtu_grpc/app/auth_service/authservice"
|
|
"gitea.youtukeji.com.cn/youtu/youtu_grpc/pkg/config"
|
|
"github.com/spf13/viper"
|
|
"github.com/zeromicro/go-zero/core/conf"
|
|
"github.com/zeromicro/go-zero/core/discov"
|
|
"github.com/zeromicro/go-zero/zrpc"
|
|
clientv3 "go.etcd.io/etcd/client/v3"
|
|
"strings"
|
|
)
|
|
|
|
type ServiceContext struct {
|
|
Config config.Config
|
|
AuthServiceClient authservice.AuthService
|
|
etcdCli *clientv3.Client
|
|
EcpmConfig *EcpmConfigCli
|
|
}
|
|
|
|
func NewServiceContext(c config.Config) *ServiceContext {
|
|
svc := &ServiceContext{
|
|
Config: c,
|
|
}
|
|
|
|
//初始化etcd客户端
|
|
svc.initEtcd()
|
|
|
|
//初始化auth_service客户端
|
|
svc.initAuthServiceClient()
|
|
|
|
return svc
|
|
}
|
|
|
|
const EcpmConfigWatchKey = "/youtu/app/ecpm/config/"
|
|
|
|
func (svc *ServiceContext) initEtcd() {
|
|
|
|
svc.EcpmConfig = NewEcpmConfig()
|
|
|
|
//初始化etcd客户端
|
|
cli, err := clientv3.NewFromURL(viper.GetString(config.EtcdAddrKey))
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
ch := make(chan config.WatchKV)
|
|
go config.EtcdGetOneAndWatch(context.Background(), cli, EcpmConfigWatchKey, ch)
|
|
|
|
go func() {
|
|
for res := range ch {
|
|
svc.EcpmConfig.SetAllByJson(strings.TrimPrefix(res.Key, EcpmConfigWatchKey), res.Value)
|
|
}
|
|
}()
|
|
|
|
svc.etcdCli = cli
|
|
}
|
|
|
|
// initAuthServiceClient inits the AuthServiceClient.
|
|
func (svc *ServiceContext) initAuthServiceClient() {
|
|
clientConf := zrpc.RpcClientConf{}
|
|
err := conf.FillDefault(&clientConf) // 填充默认值,比如 trace 透传等,参考服务配置说明
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
clientConf.Token = "auth_service.rpc.key"
|
|
clientConf.App = "auth_service.rpc"
|
|
clientConf.Etcd = discov.EtcdConf{ // 通过 etcd 服务发现
|
|
Hosts: []string{viper.GetString(config.EtcdAddrKey)},
|
|
Key: "auth_service.rpc",
|
|
}
|
|
|
|
svc.AuthServiceClient = authservice.NewAuthService(zrpc.MustNewClient(clientConf))
|
|
}
|