本文主要研究一下dubbo-go的kubernetesRegistry

kubernetesRegistry

dubbo-go-v1.4.2/registry/kubernetes/registry.go

var (
    processID = ""
    localIP   = ""
)

const (
    Name         = "kubernetes"
    ConnDelay    = 3
    MaxFailTimes = 15
)

func init() {
    processID = fmt.Sprintf("%d", os.Getpid())
    localIP, _ = gxnet.GetLocalIP()
    extension.SetRegistry(Name, newKubernetesRegistry)
}

type kubernetesRegistry struct {
    registry.BaseRegistry
    cltLock        sync.RWMutex
    client         *kubernetes.Client
    listenerLock   sync.Mutex
    listener       *kubernetes.EventListener
    dataListener   *dataListener
    configListener *configurationListener
}
  • kubernetesRegistry定义了cltLock、client、listenerLock、listener、dataListener、configListener属性

newKubernetesRegistry

dubbo-go-v1.4.2/registry/kubernetes/registry.go

func newKubernetesRegistry(url *common.URL) (registry.Registry, error) {

    // actually, kubernetes use in-cluster config,
    r := &kubernetesRegistry{}

    r.InitBaseRegistry(url, r)

    if err := kubernetes.ValidateClient(r); err != nil {
        return nil, perrors.WithStack(err)
    }

    r.WaitGroup().Add(1)
    go r.HandleClientRestart()
    r.InitListeners()

    logger.Debugf("the kubernetes registry started")

    return r, nil
}
  • newKubernetesRegistry方法实例化kubernetesRegistry,然后执行InitBaseRegistry、InitListeners

InitListeners

dubbo-go-v1.4.2/registry/kubernetes/registry.go

func (r *kubernetesRegistry) InitListeners() {
    r.listener = kubernetes.NewEventListener(r.client)
    r.configListener = NewConfigurationListener(r)
    r.dataListener = NewRegistryDataListener(r.configListener)
}
  • InitListeners方法执行kubernetes.NewEventListener、NewConfigurationListener、NewRegistryDataListener

DoRegister

dubbo-go-v1.4.2/registry/kubernetes/registry.go

func (r *kubernetesRegistry) DoRegister(root string, node string) error {
    return r.client.Create(path.Join(root, node), "")
}
  • DoRegister执行r.client.Create(path.Join(root, node), "")

DoSubscribe

dubbo-go-v1.4.2/registry/kubernetes/registry.go

func (r *kubernetesRegistry) DoSubscribe(svc *common.URL) (registry.Listener, error) {

    var (
        configListener *configurationListener
    )

    r.listenerLock.Lock()
    configListener = r.configListener
    r.listenerLock.Unlock()
    if r.listener == nil {
        r.cltLock.Lock()
        client := r.client
        r.cltLock.Unlock()
        if client == nil {
            return nil, perrors.New("kubernetes client broken")
        }

        r.listenerLock.Lock()
        if r.listener == nil {
            // double check
            r.listener = kubernetes.NewEventListener(r.client)
        }
        r.listenerLock.Unlock()
    }

    //register the svc to dataListener
    r.dataListener.AddInterestedURL(svc)
    for _, v := range strings.Split(svc.GetParam(constant.CATEGORY_KEY, constant.DEFAULT_CATEGORY), ",") {
        go r.listener.ListenServiceEvent(fmt.Sprintf("/dubbo/%s/"+v, svc.Service()), r.dataListener)
    }

    return configListener, nil
}
  • DoSubscribe方法在r.listener为nil时则通过kubernetes.NewEventListener(r.client)创建listener;之后执行r.dataListener.AddInterestedURL(svc);最后遍历category,执行r.listener.ListenServiceEvent(fmt.Sprintf("/dubbo/%s/"+v, svc.Service()), r.dataListener)

小结

kubernetesRegistry定义了cltLock、client、listenerLock、listener、dataListener、configListener属性;InitListeners方法执行kubernetes.NewEventListener、NewConfigurationListener、NewRegistryDataListener

doc


codecraft
11.9k 声望2k 粉丝

当一个代码的工匠回首往事时,不因虚度年华而悔恨,也不因碌碌无为而羞愧,这样,当他老的时候,可以很自豪告诉世人,我曾经将代码注入生命去打造互联网的浪潮之巅,那是个很疯狂的时代,我在一波波的浪潮上留下...


引用和评论

0 条评论