最近在看微服务,跟大佬们请教了下很多微服务的知识,然后看教程自己实践,在此特别感谢0.01km(QQ昵称)大佬的帮助
本节主要是针对与go-micro服务注册的过程中源码阅读的分享,
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
}
好了,下节继续分享其他部分
有疑问加站长微信联系(非本文作者)