您好,登錄后才能下訂單哦!
最近在看微服務(wù),跟大佬們請教了下很多微服務(wù)的知識,然后看教程自己實(shí)踐,在此特別感謝0.01km(QQ昵稱)大佬的幫助
本節(jié)主要是針對與go-micro服務(wù)注冊的過程中源碼閱讀的分享,
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結(jié)構(gòu)體,如果想要修改consul默認(rèn)的注冊地址,必須要修改NewRegistry函數(shù),里面賦值
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
}
//這里是獲取默認(rèn)的配置
func configure(c *consulRegistry, opts ...Option) {
// set opts
for _, o := range opts {
o(&c.opts)
}
// use default config
config := consul.DefaultConfig()
//consul默認(rèn)配置
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
}
好了,下節(jié)繼續(xù)分享其他部分
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。