feat: refactor.

This commit is contained in:
tx7do
2024-05-06 10:26:59 +08:00
parent 7ce746e181
commit 72daf43151
13 changed files with 100 additions and 81 deletions

View File

@@ -16,38 +16,24 @@ import (
conf "github.com/tx7do/kratos-bootstrap/api/gen/go/conf/v1"
)
const remoteConfigSourceConfigFile = "remote.yaml"
type Type string
const (
LocalFileType Type = "file"
NacosType Type = "nacos"
ConsulType Type = "consul"
EtcdType Type = "etcd"
ApolloType Type = "apollo"
KubernetesType Type = "kubernetes"
PolarisType Type = "polaris"
)
// NewRemoteConfigSource 创建一个远程配置源
func NewRemoteConfigSource(c *conf.RemoteConfig) config.Source {
switch Type(c.Type) {
default:
fallthrough
case LocalFileType:
case LocalFile:
return nil
case NacosType:
case Nacos:
return nacos.NewConfigSource(c)
case ConsulType:
case Consul:
return consul.NewConfigSource(c)
case EtcdType:
case Etcd:
return etcd.NewConfigSource(c)
case ApolloType:
case Apollo:
return apollo.NewConfigSource(c)
case KubernetesType:
case Kubernetes:
return kubernetes.NewConfigSource(c)
case PolarisType:
case Polaris:
return polaris.NewConfigSource(c)
}
}

46
config/flag.go Normal file
View File

@@ -0,0 +1,46 @@
package config
import (
"flag"
"github.com/tx7do/kratos-bootstrap/utils"
)
// CommandFlags 命令传参
type CommandFlags struct {
Conf string // 引导配置文件路径,默认为:../../configs
Env string // 开发环境dev、debug……
ConfigHost string // 远程配置服务端地址
ConfigType string // 远程配置服务端类型
Daemon bool // 是否转为守护进程
}
func NewCommandFlags() *CommandFlags {
f := &CommandFlags{
Conf: "",
Env: "",
ConfigHost: "",
ConfigType: "",
Daemon: false,
}
f.defineFlag()
return f
}
func (f *CommandFlags) defineFlag() {
flag.StringVar(&f.Conf, "conf", "../../configs", "config path, eg: -conf ../../configs")
flag.StringVar(&f.Env, "env", "dev", "runtime environment, eg: -env dev")
flag.StringVar(&f.ConfigHost, "chost", "127.0.0.1:8500", "config server host, eg: -chost 127.0.0.1:8500")
flag.StringVar(&f.ConfigType, "ctype", "consul", "config server host, eg: -ctype consul")
flag.BoolVar(&f.Daemon, "d", false, "run app as a daemon with -d=true.")
}
func (f *CommandFlags) Init() {
flag.Parse()
if f.Daemon {
utils.BeDaemon("-d")
}
}

15
config/options.go Normal file
View File

@@ -0,0 +1,15 @@
package config
const remoteConfigSourceConfigFile = "remote.yaml"
type Type string
const (
LocalFile Type = "file"
Nacos Type = "nacos"
Consul Type = "consul"
Etcd Type = "etcd"
Apollo Type = "apollo"
Kubernetes Type = "kubernetes"
Polaris Type = "polaris"
)

30
config/service_info.go Normal file
View File

@@ -0,0 +1,30 @@
package config
import "os"
type ServiceInfo struct {
Name string
Version string
Id string
Metadata map[string]string
}
func NewServiceInfo(name, version, id string) *ServiceInfo {
if id == "" {
id, _ = os.Hostname()
}
return &ServiceInfo{
Name: name,
Version: version,
Id: id,
Metadata: map[string]string{},
}
}
func (s *ServiceInfo) GetInstanceId() string {
return s.Id + "." + s.Name
}
func (s *ServiceInfo) SetMataData(k, v string) {
s.Metadata[k] = v
}