Example #1
0
func (ctx *ClusterContext) initServiceDiscovery() error {
	descriptions := ctx.getSortedSDDescriptionByType()
	log.Debugf("deploying all service discovery related container. len:%d", len(descriptions))
	for _, description := range descriptions {
		log.Debugf("deploy service discovery container. group:%s", description.Group)
		ctx.deployContainersByDescription(&description)
	}

	log.Debugf("loading all container infos.")
	cinfos, err := ctx.loadAllContainers()
	if err != nil {
		return err
	}
	loadRegistry := func(group string, description *dcluster.ContainerDescription, infos []dcontainer.ContainerInfo) []string {
		ipPorts := []string{}
		for _, c := range infos {
			if c.Group != group {
				continue
			}
			if !c.IsUp() {
				log.Debugf("Container '%s' found for group '%s', but container is not up.\n", c.Name[0], c.Group)
				continue
			}
			for _, iPort := range c.IpPorts {
				if iPort.PrivatePort != description.PortBinding.ContainerPort {
					continue
				}
				ipPort := fmt.Sprintf("%s:%d", iPort.IP, iPort.PublicPort)
				ipPorts = append(ipPorts, ipPort)
			}
		}
		return ipPorts
	}

	for sdName, sdd := range ctx.clusterDesc.ServiceDiscover {
		driver, err := discovery.NewRegDriver(sdd["driver"], ctx.clusterDesc)
		if err != nil {
			return errors.New(fmt.Sprintf("Failed to create Reg Driver:'%s'. err:%s", sdName, err.Error()))
		}
		containerGroup, ok := sdd["container"]
		log.Debugf("Load registry for '%s'\n", containerGroup)
		if ok {
			fmt.Printf("Load registry for service discover. sd name: '%s', group: '%s'.\n", sdName, containerGroup)
			description, exists := ctx.clusterDesc.Container.Topology.GetDescription(containerGroup)
			if !exists {
				panic(fmt.Sprintf("No container description found for group '%s'", containerGroup))
			}
			ipPorts := loadRegistry(containerGroup, description, cinfos)
			if len(ipPorts) == 0 {
				fmt.Printf("No available container for service discovery:%s, container group:%s\n", sdName, containerGroup)
			} else {
				fmt.Printf("Service discover registry found: %+v\n", ipPorts)
				(*driver).Registry(ipPorts)
			}
		}
		fmt.Printf("Service discover registered name:%s. \n", sdName)
		ctx.serviceRegistries[sdName] = driver
	}
	return nil
}
Example #2
0
func (ctx *ClusterContext) initServiceDiscovery(cinfos []dcontainer.ContainerInfo) error {
	loadRegistry := func(group string, description dcluster.ContainerDescription, infos []dcontainer.ContainerInfo) []string {
		ipPorts := []string{}
		for _, c := range infos {
			if c.Group != group {
				continue
			}
			if !c.IsUp() {
				fmt.Printf("Container '%s' found for group '%s', but container is not up.\n", c.Name[0], c.Group)
			}
			for _, iPort := range c.IpPorts {
				if iPort.PrivatePort != description.PortBinding.ContainerPort {
					continue
				}
				ipPort := fmt.Sprintf("%s:%d", iPort.IP, iPort.PublicPort)
				ipPorts = append(ipPorts, ipPort)
			}
		}
		return ipPorts
	}

	for sdName, sdd := range ctx.clusterDesc.ServiceDiscover {
		driver, err := discovery.NewRegDriver(sdd)
		if err != nil {
			return errors.New(fmt.Sprintf("Failed to create Reg Driver:'%s'. err:%s", sdName, err.Error()))
		}
		containerGroup, ok := sdd["container"]
		fmt.Printf("Load registry for '%s'\n", containerGroup)
		if ok {
			fmt.Printf("Load registry for service discover. sd name: '%s', group: '%s'.\n", sdName, containerGroup)
			description, exists := ctx.clusterDesc.Container.Topology[containerGroup]
			if !exists {
				panic(fmt.Sprintf("No container description found for group '%s'", containerGroup))
			}
			ipPorts := loadRegistry(containerGroup, description, cinfos)
			if len(ipPorts) == 0 {
				fmt.Printf("No available container for service discovery:%s, container group:%s\n", sdName, containerGroup)
			} else {
				fmt.Printf("Service discover registry found: %+v\n", ipPorts)
				(*driver).Registry(ipPorts)
			}
		}
		fmt.Printf("Service discover registered: name:%s. \n", sdName)
		ctx.serviceRegistries[sdName] = driver
	}
	return nil
}