update
All checks were successful
/ build-services (app/ranking/Dockerfile, ranking, ranking) (push) Successful in 39s
/ build-services (app/user/Dockerfile, user, user) (push) Successful in 39s
/ start-services (push) Successful in 4s
/ build-services (app/auth/Dockerfile, auth, auth) (push) Successful in 40s
/ build-services (app/ecpm/Dockerfile, ecpm, ecpm) (push) Successful in 37s

This commit is contained in:
xiabin 2025-02-14 11:46:37 +08:00
parent 435fd2f37b
commit 207a6bbb95
7 changed files with 118 additions and 3 deletions

View File

@ -14,7 +14,7 @@ import (
"google.golang.org/grpc/reflection"
)
const ServiceName = "douyin_ecpm"
const ServiceName = "ecpm"
func main() {
flag.Parse()

26
cmd/etcd_config.go Normal file
View File

@ -0,0 +1,26 @@
package main
import (
"fmt"
"gitea.youtukeji.com.cn/youtu/youtu_grpc/pkg/config"
"os"
"strings"
)
// 连接etcd服务器并将./etc目录下的.json文件推送到etcd中
func main() {
dir, err := os.ReadDir("./etc")
if err != nil {
panic(err)
}
for _, i := range dir {
fmt.Println(i.Name())
b, err := os.ReadFile("./etc/" + i.Name())
if err != nil {
panic(err)
}
serverName, _ := strings.CutSuffix(i.Name(), ".json")
err = config.SetConfig(string(b), serverName)
}
}

18
etc/auth.json Normal file
View File

@ -0,0 +1,18 @@
{
"RpcServerConf": {
"ListenOn": "0.0.0.0:8081",
"Etcd": {
"Hosts": [
"192.168.0.47:2379"
],
"Key": "auth.rpc"
}
},
"Mode": "dev",
"Mysql": "root:youtu!0113@tcp(192.168.0.47:3306)/ecpm?charset=utf8mb4&parseTime=True&loc=Local",
"Redis": [
{
"host": "192.168.0.47:6379"
}
]
}

19
etc/ecpm.json Normal file
View File

@ -0,0 +1,19 @@
{
"RpcServerConf": {
"ListenOn": "0.0.0.0:8083",
"Etcd": {
"Hosts": [
"192.168.0.47:2379"
],
"Key": "ecpm.rpc"
}
},
"Mode": "dev",
"Mysql": "root:youtu!0113@tcp(192.168.0.47:3306)/ecpm?charset=utf8mb4&parseTime=True&loc=Local",
"Redis": [
{
"host": "192.168.0.47:6379",
"password": ""
}
]
}

19
etc/ranking.json Normal file
View File

@ -0,0 +1,19 @@
{
"RpcServerConf": {
"ListenOn": "0.0.0.0:8082",
"Etcd": {
"Hosts": [
"192.168.0.47:2379"
],
"Key": "ranking.rpc"
}
},
"Mode": "dev",
"Mysql": "root:youtu!0113@tcp(192.168.0.47:3306)/ecpm?charset=utf8mb4&parseTime=True&loc=Local",
"Redis": [
{
"host": "192.168.0.47:6379",
"password": ""
}
]
}

20
etc/user.json Normal file
View File

@ -0,0 +1,20 @@
{
"RpcServerConf": {
"ListenOn": "0.0.0.0:8080",
"Etcd": {
"Hosts": [
"192.168.0.47:2379"
],
"Key": "user.rpc"
},
"Auth": false
},
"Mode": "dev",
"Mysql": "root:youtu!0113@tcp(192.168.0.47:3306)/ecpm?charset=utf8mb4&parseTime=True&loc=Local",
"Redis": [
{
"host": "192.168.0.47:6379",
"password": ""
}
]
}

View File

@ -1,9 +1,12 @@
package config
import (
"context"
"fmt"
"github.com/spf13/viper"
_ "github.com/spf13/viper/remote"
"github.com/zeromicro/go-zero/zrpc"
clientv3 "go.etcd.io/etcd/client/v3"
)
const (
@ -11,7 +14,7 @@ const (
TypeEtcd = "etcd"
TypeEnv = "env"
EtcdPrefix = "/youtu/"
Prefix = "/youtu/config/" // 配置文件前缀
EtcdAddrKey = "ETCD_ADDR"
)
@ -39,7 +42,8 @@ func init() {
}
func GetConfig(c *Config, serverName string) (err error) {
err = viper.AddRemoteProvider("etcd3", viper.GetString(EtcdAddrKey), EtcdPrefix+serverName+".rpc")
err = viper.AddRemoteProvider("etcd3", viper.GetString(EtcdAddrKey), Prefix+serverName+".rpc")
fmt.Println(Prefix + serverName + ".rpc")
if err != nil {
return
}
@ -56,3 +60,12 @@ func GetConfig(c *Config, serverName string) (err error) {
c.RpcServerConf.ServiceConf.Mode = c.Mode
return
}
func SetConfig(b, serverName string) (err error) {
etcdCli, err := clientv3.NewFromURL(viper.GetString(EtcdAddrKey))
if err != nil {
return
}
_, err = etcdCli.Put(context.Background(), Prefix+serverName+".rpc", b)
return
}