使用go-kit组件进行服务注册与发现和健康检查

2021年3月27日 16点热度 0条评论 来源: 鹿灏楷silves

在go的微服务架构中,使用go-kit组件进行开发微服务

type Reg struct { 
	Host string
	Port int
	Client consul.Client
}
func MakeReg (host string , port int) (*Reg , error) { 
	reg := api.DefaultConfig()
	reg.Address = host + ":" + strconv.Itoa(port)
	apiclient , err = api.NewClient(reg)
	if err != nil { 
		return nil , err
	}
	client := consul.NewClient(apiclient)
	return &Reg{ Host : host , Port : port ,Client : client} , nil
}
func (r Reg) Resiter (servicename , tag , host , seviceid ,checkinter ,healthcheckhttp ,deregisterafter string , port int) bool { 
	congig := api.AgentServiceRegistration{ 
		Port : port ,
		Address : host ,
		Name := servicename,
		ID := serviceid,
		Ckeck : &api.AgentServiceCheck{ 
			Interval : checkinter,
			HTTP : "http://" + host + ":" + healthcheckhttp ,
			DeregisterCriticalServiceAfter : deregisterafter,
		}
	}
	if err := r.Client.Register(&config) ; err != nil { 
		fmt.Println(err)
		return false
	}
	return true
}
func (r Reg) Deregister (serviceid string) bool { 
	dreg := api.AgentServiceRegistration{ ID : serviceid}
	if err != r.Client.Deregister(&config) ; err != nil { 
		fmt.Println(err)
		return false
	}
	return true
}
func (r Reg) discover (servicename , tag string ,passingonly bool) ( []*api.ServiceEntry ,error ) { 
	if entries ,_ ,err := r.Client.Service(servicename , tag , passingonly , nil) ;err != nil { 
		return nil ,err
	}else{ 
		return entries , nil
	}
}
    原文作者:鹿灏楷silves
    原文地址: https://blog.csdn.net/Xiang_lhh/article/details/115249056
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系管理员进行删除。