func()

in pkg/adapter/dubboregistry/registry/zookeeper/application_service_listener.go [132:189]


func (asl *applicationServiceListener) handleEvent(children []string) {
	fetchChildren, err := asl.client.GetChildren(asl.servicePath)
	if err != nil {
		logger.Warnf("Error when retrieving newChildren in path: %s, Error:%s", asl.servicePath, err.Error())
		// disable the API
		for _, url := range asl.urls {
			bkConf, _, _, _ := registry.ParseDubboString(url.String())
			apiPattern := registry.GetAPIPattern(bkConf)

			if err := asl.adapterListener.OnDeleteRouter(config.Resource{Path: apiPattern}); err != nil {
				logger.Errorf("Error={%s} when try to remove API by path: %s", err.Error(), apiPattern)
			}
		}
		return
	}

	asl.urls = asl.getUrls(fetchChildren[0])
	for _, url := range asl.urls {
		bkConfig, _, location, err := registry.ParseDubboString(url.String())
		if err != nil {
			logger.Warnf("Parse dubbo interface provider %s failed; due to %s", url.String(), err.Error())
			continue
		}
		if len(bkConfig.ApplicationName) == 0 || len(bkConfig.Interface) == 0 {
			continue
		}
		methods, err := asl.getMethods(bkConfig.Interface)
		if err != nil {
			logger.Warnf("Get methods of interface %s failed; use prefix pattern to match url, due to %s", bkConfig.Interface, err.Error())
		}

		apiPattern := registry.GetAPIPattern(bkConfig)
		mappingParams := []config.MappingParam{
			{
				Name:  "requestBody.values",
				MapTo: "opt.values",
			},
			{
				Name:  "requestBody.types",
				MapTo: "opt.types",
			},
		}
		if len(methods) != 0 {
			for i := range methods {
				api := registry.CreateAPIConfig(apiPattern, location, bkConfig, methods[i], mappingParams)
				if err := asl.adapterListener.OnAddAPI(api); err != nil {
					logger.Errorf("Error={%s} happens when try to add api %s", err.Error(), api.Path)
				}
			}
		} else {
			// can't fetch methods, use http prefix pattern
			api := registry.CreateAPIConfig(apiPattern, location, bkConfig, constant.AnyValue, mappingParams)
			if err := asl.adapterListener.OnAddAPI(api); err != nil {
				logger.Errorf("Error={%s} happens when try to add api %s", err.Error(), api.Path)
			}
		}
	}
}