Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

如何在解析下游服务时拿到下游服务名称 #1488

Open
wes-xia opened this issue Aug 12, 2024 · 2 comments
Open

如何在解析下游服务时拿到下游服务名称 #1488

wes-xia opened this issue Aug 12, 2024 · 2 comments

Comments

@wes-xia
Copy link

wes-xia commented Aug 12, 2024

No description provided.

@wes-xia wes-xia changed the title 解析下游服务时拿到下游服务名称 如何在解析下游服务时拿到下游服务名称 Aug 12, 2024
@wes-xia
Copy link
Author

wes-xia commented Aug 12, 2024

我们现在的需求是,在发现下游服务的时候如果服务的标签不匹配,那么把该服务的信息告警出来。
下面是的我代码,client注册的时候会走到下面的resover代码,resover 判断上下游服务的tag是否匹配,如果不匹配告警,
如何在resover内拿到下游服务的名称
client.go注册服务

func InitProjectClient() {
	addr := fmt.Sprintf("%s:%s", os.Getenv("CONSUL_HOST"), os.Getenv("CONSUL_PORT"))
	r, err := consul1.NewConsulResolver(addr)
	if err != nil {
		panic(err)
	}
	serviceName := "ops.cicd.client"
	c, err := projectsrv.NewClient(
		"ops.eco.cicd",
		client.WithMuxConnection(1),                       // mux
		client.WithRPCTimeout(5*time.Second),              // rpc timeout
		client.WithConnectTimeout(5*time.Second),          // conn timeout
		client.WithFailureRetry(retry.NewFailurePolicy()), // retry
		client.WithSuite(trace.NewDefaultClientSuite()),   // tracer
		client.WithResolver(r),                            // resolver
		client.WithSuite(tracing.NewClientSuite()),
		client.WithClientBasicInfo(&rpcinfo.EndpointBasicInfo{ServiceName: serviceName}),
	)
	if err != nil {
		panic(err)
	}
	ProjectClient = c
}

resolver.go

// Resolve a service info by desc.
func (c *consulResolver) Resolve(_ context.Context, desc string) (discovery.Result, error) {
	var eps []discovery.Instance
	agentServiceList, _, err := c.consulClient.Health().Service(desc, "", true, nil)
	if err != nil {
		return discovery.Result{}, err
	}
	if len(agentServiceList) == 0 {
		return discovery.Result{}, errors.New(fmt.Sprintf("no service found: %s", desc))
	}

	for _, i := range agentServiceList {
		svc := i.Service
		if svc == nil || svc.Address == "" {
			continue
		}

		eps = append(eps, discovery.NewInstance(
			defaultNetwork,
			fmt.Sprint(svc.Address, ":", svc.Port),
			svc.Weights.Passing,
			splitTags(svc.Tags),
		))
	}

	blog.Debugf("before %v", c)
	blog.Debugf("before %v", eps)
	eps = c.filterService(eps)
	blog.Debugf("after %v", eps)
	for _, ep := range eps {
		blog.Debugf("ep: %v", ep)
	}

	return discovery.Result{
		Cacheable: true,
		CacheKey:  desc,
		Instances: eps,
	}, nil
} 

@ppzqh
Copy link
Contributor

ppzqh commented Aug 15, 2024

所以是要在 Resolve 方法内拿到 NewClient 传入的那个 destService 吗?可以用这个方法

func GetCallee(ctx context.Context) (string, bool) {
	defer func() { recover() }()

	ri := rpcinfo.GetRPCInfo(ctx)
	if ri == nil {
		return "", false
	}
	return ri.To().ServiceName(), true
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants