最近在看微服務,跟大佬們請教了下不少微服務的知識,而後看教程本身實踐,在此特別感謝0.01km(QQ暱稱)大佬的幫助markdown
本節主要是針對與go-micro服務註冊的過程當中源碼閱讀的分享,ide
s := micro.NewService( micro.Name("go.micro.srv.user"), micro.Version("latest"), ) s.Init() 這裏作了不少事 func newOptions(opts ...Option) Options { opt := Options{ Broker: broker.DefaultBroker, Cmd: cmd.DefaultCmd, Client: client.DefaultClient, Server: server.DefaultServer, Registry: registry.DefaultRegistry, Transport: transport.DefaultTransport, Context: context.Background(), } } 這塊初始化Options結構體,若是想要修改consul默認的註冊地址,必需要修改NewRegistry函數,裏面賦值 type Options struct { Addrs []string //註冊ip地址:192.168.1.2:8080 Timeout time.Duration Secure bool TLSConfig *tls.Config // Other options for implementations of the interface // can be stored in a context Context context.Context } //這裏是獲取默認的配置 func configure(c *consulRegistry, opts ...Option) { // set opts for _, o := range opts { o(&c.opts) } // use default config config := consul.DefaultConfig() //consul默認配置 func defaultConfig(transportFn func() *http.Transport) *Config { config := &Config{ Address: "127.0.0.1:8500", Scheme: "http", Transport: transportFn(), } if addr := os.Getenv(HTTPAddrEnvName); addr != "" { config.Address = addr } if token := os.Getenv(HTTPTokenEnvName); token != "" { config.Token = token } if auth := os.Getenv(HTTPAuthEnvName); auth != "" { var username, password string if strings.Contains(auth, ":") { split := strings.SplitN(auth, ":", 2) username = split[0] password = split[1] } else { username = auth }
好了,下節繼續分享其餘部分函數