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

[Question]kubernetes/registry.go的discovery问题 #2203

Closed
guihouchang opened this issue Jul 16, 2022 · 23 comments
Closed

[Question]kubernetes/registry.go的discovery问题 #2203

guihouchang opened this issue Jul 16, 2022 · 23 comments
Labels
question Further information is requested

Comments

@guihouchang
Copy link
Contributor

guihouchang commented Jul 16, 2022

请问kubernetes/registry.go (http|grpc).WithEndpoint要怎么写? discovery://namespace/appname 这样子?还是 discovery:///appname 或者 直接是appname? 有什么快速的办法验证吗

@guihouchang guihouchang added the question Further information is requested label Jul 16, 2022
@rogerogers
Copy link
Contributor

server跟client一样就好了,感觉最好加上namespace避免client获取到其他其他同名的项目的pod
我做了一个demo #go-kratos/examples#19

@guihouchang
Copy link
Contributor Author

server跟client一样就好了,感觉最好加上namespace避免client获取到其他其他同名的项目的pod 我做了一个demo #go-kratos/examples#19

我现在遇到一个问题r.GetService能获取到服务节点,但是直接使用discovery:///service-name 拨号的时候就会超时。

@rogerogers
Copy link
Contributor

yaml方便贴一下吗

@guihouchang
Copy link
Contributor Author

yaml方便贴一下吗

apiVersion: v1
kind: List
items:
- apiVersion: apps/v1
  kind: Deployment
  metadata:
    name: authorizer
    labels:
      app: authorizer
      system: test
  spec:
    replicas: 2
    selector:
      matchLabels:
        app: authorizer
    template:
      metadata:
        name: authorizer
        namespace: default
        labels:
          app: authorizer
          system: test
      spec:
        serviceAccountName: default
        containers:
          - name: authorizer
            image: ${IMAGE}
            imagePullPolicy: IfNotPresent
            resources:
              requests:
                cpu: '250m'
                memory: '512Mi'
            ports:
              - name: grpc
                containerPort: 4001
                protocol: TCP
              - name: http
                containerPort: 4000
                protocol: TCP
            readinessProbe:
              tcpSocket:
                port: 4001
              initialDelaySeconds: 10     # 延迟探测时间(秒)【 在k8s第一次探测前等待秒 】
              periodSeconds: 2            # 执行探测频率(秒) 【 每隔秒执行一次 】
              timeoutSeconds: 1           # 超时时间
              successThreshold: 1         # 健康阀值
              failureThreshold: 3         # 不健康阀值
            livenessProbe:
              tcpSocket:
                port: 4001
              initialDelaySeconds: 10     # 延迟探测时间(秒)【 在k8s第一次探测前等待秒 】
              periodSeconds: 2            # 执行探测频率(秒) 【 每隔秒执行一次 】
              timeoutSeconds: 1           # 超时时间
              successThreshold: 1         # 健康阀值
              failureThreshold: 3         # 不健康阀值

@rogerogers
Copy link
Contributor

pod的yaml,注册信息在pod的注解里

@guihouchang
Copy link
Contributor Author

你要的看的是Pod的labels对吧?

labels:
  app: member
  kratos-service-app: test-member
  kratos-service-id: member-57dddbf94-d6b66
  kratos-service-version: 1b91d9e
  pod-template-hash: 57dddbf94
  system: test

@guihouchang
Copy link
Contributor Author

image

@guihouchang
Copy link
Contributor Author

pod的yaml,注册信息在pod的注解里

现在的问题是,GetService能获取到endpoints。 很奇怪拿去拨号就不行

@rogerogers
Copy link
Contributor

image

上面端口不对吧,这里是4021

@guihouchang
Copy link
Contributor Author

guihouchang commented Jul 17, 2022

这个是服务A 请求 服务B的 4021接口,端口没问题的,毕竟直连 podIP:4021 能请求到的

@rogerogers
Copy link
Contributor

不是很理解,client跟server的pod yaml都贴下。

@guihouchang
Copy link
Contributor Author

image

image

@guihouchang
Copy link
Contributor Author

不是很理解,client跟server的pod yaml都贴下。

member 是server authorizer 是client

@rogerogers
Copy link
Contributor

看上去没什么问题,client连接就是超时吗

@shenqidebaozi
Copy link
Sponsor Member

discovery:///appname

@shenqidebaozi
Copy link
Sponsor Member

是三个斜杠不是两个

@guihouchang
Copy link
Contributor Author

是三个斜杠不是两个

好像还是连接超时

@rogerogers
Copy link
Contributor

这要看代码了,curl结果先看下。

@guihouchang
Copy link
Contributor Author

jeager 上面错误的信息

pc.system "grpc"  
rpc.service "api.authorizer.v1.Authorizer"  
rpc.method "Test"  
send_msg.size 0  
rpc.status_code 504  
recv_msg.size 0  
otel.library.name "kratos"  
span.kind "client"  
error true  
otel.status_code "ERROR"  
otel.status_description "rpc error: code = DeadlineExceeded desc = context deadline exceeded"

@snoad
Copy link

snoad commented Jul 18, 2022

我也遇见过这种情况,不过我是consul做的服务注册,我通过一个麻烦的办法解决了你可以参考一下

`

c := api.DefaultConfig()
clients, _ := api.NewClient(c)

dis := consul.New(clients)
//第一次请求,拿到服务列表
conn, _ := grpc.Dial(context.Background(), grpc.WithEndpoint("discovery:///grpctester"), grpc.WithDiscovery(dis))

service, _ := dis.GetService(context.Background(), "grpctester")

defer conn.Close()
//第二次请求,获取数据
var endpoint string
for i := 0; i < len(service[0].Endpoints); i++ {
	if strings.HasPrefix(service[0].Endpoints[i], "grpc://") {
		endpoint = service[0].Endpoints[i]
	}
}
//截取服务ip地址
endpoint = strings.Split(endpoint, "grpc://")[1]
fmt.Println("解析后:", endpoint)
connTrue, _ := grpc.DialInsecure(context.Background(), grpc.WithEndpoint(endpoint), grpc.WithDiscovery(dis))

defer connTrue.Close()
client := hpb.NewTesterClient(connTrue)
reply, err := client.CreateTester(context.Background(), &hpb.CreateTesterRequest{Name: NBCODE})
if err != nil {
	return err
}

`

@shenqidebaozi
Copy link
Sponsor Member

我也遇见过这种情况,不过我是consul做的服务注册,我通过一个麻烦的办法解决了你可以参考一下

`

c := api.DefaultConfig()
clients, _ := api.NewClient(c)

dis := consul.New(clients)
//第一次请求,拿到服务列表
conn, _ := grpc.Dial(context.Background(), grpc.WithEndpoint("discovery:///grpctester"), grpc.WithDiscovery(dis))

service, _ := dis.GetService(context.Background(), "grpctester")

defer conn.Close()
//第二次请求,获取数据
var endpoint string
for i := 0; i < len(service[0].Endpoints); i++ {
	if strings.HasPrefix(service[0].Endpoints[i], "grpc://") {
		endpoint = service[0].Endpoints[i]
	}
}
//截取服务ip地址
endpoint = strings.Split(endpoint, "grpc://")[1]
fmt.Println("解析后:", endpoint)
connTrue, _ := grpc.DialInsecure(context.Background(), grpc.WithEndpoint(endpoint), grpc.WithDiscovery(dis))

defer connTrue.Close()
client := hpb.NewTesterClient(connTrue)
reply, err := client.CreateTester(context.Background(), &hpb.CreateTesterRequest{Name: NBCODE})
if err != nil {
	return err
}

`

这样失去了使用服务发现的意义

@snoad
Copy link

snoad commented Jul 18, 2022

我也遇见过这种情况,不过我是consul做的服务注册,我通过一个麻烦的办法解决了你可以参考一下
`

c := api.DefaultConfig()
clients, _ := api.NewClient(c)

dis := consul.New(clients)
//第一次请求,拿到服务列表
conn, _ := grpc.Dial(context.Background(), grpc.WithEndpoint("discovery:///grpctester"), grpc.WithDiscovery(dis))

service, _ := dis.GetService(context.Background(), "grpctester")

defer conn.Close()
//第二次请求,获取数据
var endpoint string
for i := 0; i < len(service[0].Endpoints); i++ {
	if strings.HasPrefix(service[0].Endpoints[i], "grpc://") {
		endpoint = service[0].Endpoints[i]
	}
}
//截取服务ip地址
endpoint = strings.Split(endpoint, "grpc://")[1]
fmt.Println("解析后:", endpoint)
connTrue, _ := grpc.DialInsecure(context.Background(), grpc.WithEndpoint(endpoint), grpc.WithDiscovery(dis))

defer connTrue.Close()
client := hpb.NewTesterClient(connTrue)
reply, err := client.CreateTester(context.Background(), &hpb.CreateTesterRequest{Name: NBCODE})
if err != nil {
	return err
}

`

这样失去了使用服务发现的意义

是的,不过当时没有深入去研究。现在看起来没法通过服务名直接请求但是ip可以请求,像是dns的问题?

@guihouchang
Copy link
Contributor Author

oint = service[0].Endpoints[i]

嗯,这个方案我也想过,不过如果能加上负载均衡的算法那就更好了

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

4 participants