抽离配置到nacos里
This commit is contained in:
+2
-1
@@ -1 +1,2 @@
|
||||
etc/admin.yaml
|
||||
etc/admin.yaml
|
||||
run.toml
|
||||
+65
-41
@@ -3,7 +3,6 @@ package main
|
||||
import (
|
||||
"admin/admin"
|
||||
"flag"
|
||||
"fmt"
|
||||
"net"
|
||||
"os"
|
||||
"strconv"
|
||||
@@ -19,7 +18,6 @@ import (
|
||||
"google.golang.org/grpc"
|
||||
"google.golang.org/grpc/reflection"
|
||||
"pkg.local/discovery"
|
||||
"pkg.local/log"
|
||||
"pkg.local/modelbase"
|
||||
"pkg.local/mysql"
|
||||
"pkg.local/redis"
|
||||
@@ -30,46 +28,64 @@ import (
|
||||
var configFile = flag.String("f", "etc/admin.yaml", "the config file")
|
||||
|
||||
func main() {
|
||||
|
||||
flag.Parse()
|
||||
|
||||
var c config.Config
|
||||
conf.MustLoad(*configFile, &c)
|
||||
|
||||
if err := log.Init(log.Config{
|
||||
Path: c.AppLog.Path,
|
||||
InfoFile: c.AppLog.InfoFile,
|
||||
ErrorFile: c.AppLog.ErrorFile,
|
||||
FatalFile: c.AppLog.FatalFile,
|
||||
MaxSize: c.AppLog.MaxSize,
|
||||
MaxBackups: c.AppLog.MaxBackups,
|
||||
MaxAge: c.AppLog.MaxAge,
|
||||
}); err != nil {
|
||||
fmt.Fprintf(os.Stderr, "log init: %v\n", err)
|
||||
os.Exit(1)
|
||||
// Nacos配置拉取初始化
|
||||
nacosParam := utils.NacosConfig{
|
||||
Hosts: c.Nacos.Hosts,
|
||||
NamespaceId: c.Nacos.NamespaceId,
|
||||
Group: c.Nacos.Group,
|
||||
ConfigID: c.Nacos.ConfigID,
|
||||
}
|
||||
utils.InitConfig(nacosParam)
|
||||
|
||||
// 日志配置
|
||||
logConf := logx.LogConf{
|
||||
ServiceName: utils.GetConfigString("log.serviceName"),
|
||||
Mode: utils.GetConfigString("log.mode"),
|
||||
Encoding: utils.GetConfigString("log.encoding"),
|
||||
Level: utils.GetConfigString("log.level"),
|
||||
Path: utils.GetConfigString("log.path"),
|
||||
KeepDays: utils.GetConfigInt("log.keepDays"),
|
||||
MaxSize: utils.GetConfigInt("log.maxSize"),
|
||||
MaxBackups: utils.GetConfigInt("log.maxBackups"),
|
||||
Compress: utils.GetConfigBool("log.compress"),
|
||||
}
|
||||
logx.SetUp(logConf)
|
||||
logx.AddWriter(logx.NewWriter(os.Stdout))
|
||||
|
||||
listenOn := utils.GetConfigString("base.listenOn")
|
||||
mode := utils.GetConfigString("base.mode")
|
||||
serviceName := utils.GetConfigString("base.name")
|
||||
registerIP := utils.GetConfigString("base.registerIP")
|
||||
|
||||
// 初始化服务发现
|
||||
if err := discovery.Init(discovery.Config{
|
||||
Hosts: c.Nacos.Hosts,
|
||||
NamespaceId: c.Nacos.NamespaceId,
|
||||
Group: c.Nacos.Group,
|
||||
}); err != nil {
|
||||
logx.Errorf("nacos init: %v", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
registerIP := c.Nacos.RegisterIP
|
||||
port, err := listenPort(c.ListenOn)
|
||||
port, err := listenPort(listenOn)
|
||||
if err != nil {
|
||||
logx.Errorf("parse ListenOn: %v", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
// 注册服务
|
||||
if err := discovery.Register(discovery.Instance{
|
||||
ServiceName: c.Name,
|
||||
ServiceName: serviceName,
|
||||
IP: registerIP,
|
||||
Port: port,
|
||||
Group: c.Nacos.Group,
|
||||
}); err != nil {
|
||||
logx.Errorf("nacos register: %v", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
defer func() {
|
||||
@@ -78,52 +94,60 @@ func main() {
|
||||
}
|
||||
}()
|
||||
|
||||
// 初始化MySQL
|
||||
db, err := mysql.New(mysql.Config{
|
||||
Host: c.Mysql.Host,
|
||||
Port: c.Mysql.Port,
|
||||
User: c.Mysql.User,
|
||||
Password: c.Mysql.Password,
|
||||
Database: c.Mysql.Database,
|
||||
Charset: c.Mysql.Charset,
|
||||
Prefix: c.Mysql.Prefix,
|
||||
ReadHost: c.Mysql.ReadHost,
|
||||
ReadPort: c.Mysql.ReadPort,
|
||||
ReadUser: c.Mysql.ReadUser,
|
||||
ReadPassword: c.Mysql.ReadPassword,
|
||||
ReadDatabase: c.Mysql.ReadDatabase,
|
||||
Host: utils.GetConfigString("mysql.host"),
|
||||
Port: utils.GetConfigInt("mysql.port"),
|
||||
User: utils.GetConfigString("mysql.user"),
|
||||
Password: utils.GetConfigString("mysql.password"),
|
||||
Database: utils.GetConfigString("mysql.database"),
|
||||
Charset: utils.GetConfigString("mysql.charset"),
|
||||
Prefix: utils.GetConfigString("mysql.prefix"),
|
||||
ReadHost: utils.GetConfigString("mysql_read.host"),
|
||||
ReadPort: utils.GetConfigInt("mysql_read.port"),
|
||||
ReadUser: utils.GetConfigString("mysql_read.user"),
|
||||
ReadPassword: utils.GetConfigString("mysql_read.password"),
|
||||
ReadDatabase: utils.GetConfigString("mysql_read.database"),
|
||||
})
|
||||
if err != nil {
|
||||
logx.Errorf("mysql init: %v", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
// 初始化Redis
|
||||
if err := redis.Init(redis.Config{
|
||||
Host: c.BizRedis.Host,
|
||||
Port: c.BizRedis.Port,
|
||||
Password: c.BizRedis.Password,
|
||||
DB: c.BizRedis.DB,
|
||||
Host: utils.GetConfigString("redis.host"),
|
||||
Port: utils.GetConfigInt("redis.port"),
|
||||
Password: utils.GetConfigString("redis.password"),
|
||||
DB: utils.GetConfigInt("redis.db"),
|
||||
}); err != nil {
|
||||
logx.Errorf("redis init: %v", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
debug := utils.GetConfigBool("mysql.debug")
|
||||
modelbase.Init(db, modelbase.Config{Prefix: c.Mysql.Prefix, Debug: debug})
|
||||
modelbase.Init(db, modelbase.Config{Prefix: utils.GetConfigString("mysql.prefix"), Debug: debug})
|
||||
|
||||
// 修正语法错误,手动组装RPC配置
|
||||
rpcConf := zrpc.RpcServerConf{
|
||||
ListenOn: listenOn,
|
||||
}
|
||||
rpcConf.Mode = mode
|
||||
// 构建上下文
|
||||
ctx := svc.NewServiceContext(c, db)
|
||||
|
||||
s := zrpc.MustNewServer(c.RpcServerConf, func(grpcServer *grpc.Server) {
|
||||
// 启动rpc服务,使用拼装好的rpcConf,不再使用本地空的c.RpcServerConf
|
||||
s := zrpc.MustNewServer(rpcConf, func(grpcServer *grpc.Server) {
|
||||
admin.RegisterAdminServer(grpcServer, server.NewAdminServer(ctx))
|
||||
|
||||
if c.Mode == service.DevMode || c.Mode == service.TestMode {
|
||||
if mode == service.DevMode || mode == service.TestMode {
|
||||
reflection.Register(grpcServer)
|
||||
}
|
||||
})
|
||||
defer s.Stop()
|
||||
|
||||
logx.AddWriter(logx.NewWriter(os.Stdout))
|
||||
|
||||
s.AddUnaryInterceptors(validate.UnaryServerInterceptor(validate.MustNew()))
|
||||
|
||||
logx.Infof("Starting rpc server at %s...", c.ListenOn)
|
||||
logx.Infof("Starting rpc server at %s...", listenOn)
|
||||
s.Start()
|
||||
}
|
||||
|
||||
|
||||
@@ -1,13 +1,7 @@
|
||||
package config
|
||||
|
||||
import "github.com/zeromicro/go-zero/zrpc"
|
||||
|
||||
type Config struct {
|
||||
zrpc.RpcServerConf
|
||||
Nacos NacosConf
|
||||
Mysql MysqlConf
|
||||
BizRedis RedisConf
|
||||
AppLog LogConf
|
||||
Nacos NacosConf
|
||||
}
|
||||
|
||||
type NacosConf struct {
|
||||
@@ -15,37 +9,5 @@ type NacosConf struct {
|
||||
NamespaceId string `json:",optional"`
|
||||
Group string `json:",optional"`
|
||||
RegisterIP string `json:",optional"`
|
||||
}
|
||||
|
||||
type MysqlConf struct {
|
||||
Host string
|
||||
Port int
|
||||
User string
|
||||
Password string
|
||||
Database string
|
||||
Charset string `json:",default=utf8mb4"`
|
||||
Prefix string `json:",optional"`
|
||||
|
||||
ReadHost string `json:",optional"`
|
||||
ReadPort int `json:",optional"`
|
||||
ReadUser string `json:",optional"`
|
||||
ReadPassword string `json:",optional"`
|
||||
ReadDatabase string `json:",optional"`
|
||||
}
|
||||
|
||||
type RedisConf struct {
|
||||
Host string
|
||||
Port int `json:",default=6379"`
|
||||
Password string `json:",optional"`
|
||||
DB int `json:",optional"`
|
||||
}
|
||||
|
||||
type LogConf struct {
|
||||
Path string `json:",default=logs"`
|
||||
InfoFile string `json:",default=info.log"`
|
||||
ErrorFile string `json:",default=error.log"`
|
||||
FatalFile string `json:",default=fatal.log"`
|
||||
MaxSize int `json:",default=100"`
|
||||
MaxBackups int `json:",default=10"`
|
||||
MaxAge int `json:",default=30"`
|
||||
ConfigID string `json:",optional"`
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"admin/internal/config"
|
||||
|
||||
"gorm.io/gorm"
|
||||
"pkg.local/utils"
|
||||
)
|
||||
|
||||
type ServiceContext struct {
|
||||
@@ -16,6 +17,6 @@ func NewServiceContext(c config.Config, db *gorm.DB) *ServiceContext {
|
||||
return &ServiceContext{
|
||||
Config: c,
|
||||
DB: db,
|
||||
Prefix: c.Mysql.Prefix,
|
||||
Prefix: utils.GetConfigString("mysql.prefix"),
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user