diff --git a/doc/wiki-cn/config-paladin.md b/doc/wiki-cn/config-paladin.md index 0becb02cf..28d8c1cc9 100644 --- a/doc/wiki-cn/config-paladin.md +++ b/doc/wiki-cn/config-paladin.md @@ -46,9 +46,10 @@ func TestMain(t *testing.M) { ### example main ```go -# main.go +// main.go func main() { - # 初始化paladin + flag.Parse() + // 初始化paladin if err := paladin.Init(); err != nil { panic(err) } @@ -58,28 +59,31 @@ func main() { ``` ### example HTTP/gRPC -```go +```toml # http.toml [server] addr = "0.0.0.0:9000" timeout = "1s" -# server.go +``` + +```go +// server.go func NewServer() { - # 默认配置用nil,这时读取HTTP/gRPC构架中的flag或者环境变量(可能是docker注入的环境变量,默认端口:8000/9000) - engine := bm.DefaultServer(nil) - - # 除非自己要替换了配置,用http.toml - var bc struct { - Server *bm.ServerConfig - } - if err = paladin.Get("http.toml").UnmarshalTOML("server", &bc); err != nil { - // 不存在时,将会为nil使用默认配置 - if err != paladin.ErrNotExist { - panic(err) - } - } - engine := bm.DefaultServer(conf) + // 默认配置用nil,这时读取HTTP/gRPC构架中的flag或者环境变量(可能是docker注入的环境变量,默认端口:8000/9000) + engine := bm.DefaultServer(nil) + + // 除非自己要替换了配置,用http.toml + var bc struct { + Server *bm.ServerConfig + } + if err := paladin.Get("http.toml").UnmarshalTOML(&bc); err != nil { + // 不存在时,将会为nil使用默认配置 + if err != paladin.ErrNotExist { + panic(err) + } + } + engine := bm.DefaultServer(bc.Server) } ``` @@ -87,25 +91,28 @@ func NewServer() { ```go # service.go type Service struct { - ac *paladin.Map + ac *paladin.Map } + func New() *Service { - # paladin.Map 通过atomic.Value支持自动热加载 - var ac = new(paladin.TOML) - if err := paladin.Watch("application.toml", ac); err != nil { - panic(err) - } - s := &Service{ - ac : ac; - } - return s + // paladin.Map 通过atomic.Value支持自动热加载 + var ac = new(paladin.TOML) + if err := paladin.Watch("application.toml", ac); err != nil { + panic(err) + } + s := &Service{ + ac: ac, + } + return s } + func (s *Service) Test() { - switch, err := s.ac.Bool("switch") - if err != nil { - // TODO - } - # or use default value - switch := paladin.Bool(s.ac.Value("switch"), false) + sw, err := s.ac.Get("switch").Bool() + if err != nil { + // TODO + } + + // or use default value + sw := paladin.Bool(s.ac.Get("switch"), false) } ``` diff --git a/pkg/naming/discovery/discovery.go b/pkg/naming/discovery/discovery.go index d5c24f9f9..b74061ef7 100644 --- a/pkg/naming/discovery/discovery.go +++ b/pkg/naming/discovery/discovery.go @@ -102,7 +102,7 @@ type appInfo struct { } func fixConfig(c *Config) error { - if len(c.Nodes) == 0 { + if len(c.Nodes) == 0 && env.DiscoveryNodes != "" { c.Nodes = strings.Split(env.DiscoveryNodes, ",") } if c.Region == "" { diff --git a/tool/kratos-protoc/protoc.go b/tool/kratos-protoc/protoc.go index accd6c518..57b3bec19 100644 --- a/tool/kratos-protoc/protoc.go +++ b/tool/kratos-protoc/protoc.go @@ -144,14 +144,7 @@ func latestKratos() (string, error) { } func gopath() (gp string) { - var gopaths []string - - switch runtime.GOOS { - case "windows": - gopaths = strings.Split(os.Getenv("GOPATH"), ";") - default: - gopaths = strings.Split(os.Getenv("GOPATH"), ":") - } + gopaths := strings.Split(os.Getenv("GOPATH"), string(filepath.ListSeparator)) if len(gopaths) == 1 && gopaths[0] != "" { return gopaths[0] diff --git a/tool/kratos/new.go b/tool/kratos/new.go index cd7f2a5ca..2b2b08ad7 100644 --- a/tool/kratos/new.go +++ b/tool/kratos/new.go @@ -5,6 +5,7 @@ import ( "fmt" "os" "path" + "path/filepath" "github.com/urfave/cli" ) @@ -25,6 +26,7 @@ func runNew(ctx *cli.Context) error { pwd, _ := os.Getwd() p.Path = path.Join(pwd, p.Name) } + p.Path = filepath.FromSlash(p.Path) // Create a project if err := create(); err != nil { return err diff --git a/tool/kratos/template.go b/tool/kratos/template.go index c43ba8efb..683677a2a 100644 --- a/tool/kratos/template.go +++ b/tool/kratos/template.go @@ -76,6 +76,8 @@ import ( "{{.ModuleName}}/internal/service" "github.com/bilibili/kratos/pkg/conf/paladin" "github.com/bilibili/kratos/pkg/log" + + _ "go.uber.org/automaxprocs" ) func main() { @@ -127,6 +129,8 @@ import ( "{{.ModuleName}}/internal/service" "github.com/bilibili/kratos/pkg/conf/paladin" "github.com/bilibili/kratos/pkg/log" + + _ "go.uber.org/automaxprocs" ) func main() { diff --git a/tool/kratos/tool.go b/tool/kratos/tool.go index 3b410d4c9..cc7f0c258 100644 --- a/tool/kratos/tool.go +++ b/tool/kratos/tool.go @@ -186,14 +186,7 @@ func (t Tool) installed() bool { } func gopath() (gp string) { - var gopaths []string - - switch runtime.GOOS { - case "windows": - gopaths = strings.Split(os.Getenv("GOPATH"), ";") - default: - gopaths = strings.Split(os.Getenv("GOPATH"), ":") - } + gopaths := strings.Split(os.Getenv("GOPATH"), string(filepath.ListSeparator)) if len(gopaths) == 1 && gopaths[0] != "" { return gopaths[0]