Docker Swarm 中的 Golang API 无法从 etcd 检索“users.rpc”密钥,但直接使用 etcdctl 查询时密钥存在

问题描述 投票:0回答:1

我需要使用 go-zero 框架部署 Docker Swarm 应用程序堆栈。该架构包含一个 RESTful API 服务作为请求的入口点。然后API请求RPC服务来实现业务逻辑。 API通过ETCD查询对应的RPC服务的端点。我的服务定义如下:

version: '3.8'

services:
  g_api:
    image: <Host IP>:5000/g-api:1.0.5
    ports:
      - "8888:8888"
    environment:
      - TZ=Asia/Shanghai
    deploy:
      replicas: 2
      update_config:
        parallelism: 1
        delay: 10s
      restart_policy:
        condition: on-failure
    depends_on:
      - g_etcd
      - g_authrpc
      - g_commentrpc
      - g_userrpc
      - g_msgrpc
      - g_postrpc
    networks:
      - gnet
  g_authrpc:
    image: <Host IP>:5000/g-authrpc:1.0.5
    ports:
      - "8080:8080"
    environment:
      - TZ=Asia/Shanghai
    deploy:
      replicas: 2
      update_config:
        parallelism: 1
        delay: 10s
      restart_policy:
        condition: on-failure
    depends_on:
      - g_etcd
    networks:
      - gnet
  g_userrpc:
    image: <Host IP>:5000/g-userrpc:1.0.5
    ports:
      - "8081:8081"
    environment:
      - TZ=Asia/Shanghai
    command: "./main"
    deploy:
      replicas: 2
      update_config:
        parallelism: 1
        delay: 10s
      restart_policy:
        condition: on-failure
    depends_on:
      - g_etcd
    networks:
      - gnet
  g_msgrpc:
    image: <Host IP>:5000/g-msgrpc:1.0.5
    ports:
      - "8086:8086"
    environment:
      - TZ=Asia/Shanghai
    deploy:
      replicas: 2
      update_config:
        parallelism: 1
        delay: 10s
      restart_policy:
        condition: on-failure
    depends_on:
      - g_etcd
    networks:
      - gnet
  g_postrpc:
    image: <Host IP>:5000/g-postrpc:1.0.5
    ports:
      - "8082:8082"
    environment:
      - TZ=Asia/Shanghai
    deploy:
      replicas: 2
      update_config:
        parallelism: 1
        delay: 10s
      restart_policy:
        condition: on-failure
    depends_on:
      - g_etcd
    networks:
      - gnet
  g_commentrpc:
    image: <Host IP>:5000/g-commentrpc:1.0.5
    ports:
      - "8152:8152"
    environment:
      - TZ=Asia/Shanghai
    deploy:
      replicas: 2
      update_config:
        parallelism: 1
        delay: 10s
      restart_policy:
        condition: on-failure
    depends_on:
      - g_etcd
    networks:
      - gnet
  g_etcd:
    image: <Host IP>:5000/etcd:3.5
    ports:
      - "2379:2379"
    environment:
      - TZ=Asia/Shanghai
      - ALLOW_NONE_AUTHENTICATION=yes
      - ETCD_ADVERTISE_CLIENT_URLS=http://g_etcd:2379
    deploy:
      replicas: 1
      update_config:
        parallelism: 1
        delay: 10s
      restart_policy:
        condition: on-failure
    networks:
      - gnet
networks:
  gnet:
    driver: overlay

<Host IP>
是云服务器的IP。

当我在Swarm管理服务器上启动这个Swarm集群时,所有其他服务都处于健康状态,但API服务不断报错并退出。

enter image description here

通过检查g_api服务的日志,发现一直报错并退出,然后循环重启。错误信息如下:

global_g_api.1.yv6gvbm93sq7@b3saglvue9qgqseg    | 2024/07/01 03:05:57 rpc dial: etcd://141.2.18.177:2379/gauth.rpc, error: context deadline exceeded, make sure rpc service "gauth.rpc" is already started
global_g_api.1.yv6gvbm93sq7@b3saglvue9qgqseg    | 
global_g_api.1.yv6gvbm93sq7@b3saglvue9qgqseg    | goroutine 1 [running]:
global_g_api.1.yv6gvbm93sq7@b3saglvue9qgqseg    | runtime/debug.Stack()
global_g_api.1.yv6gvbm93sq7@b3saglvue9qgqseg    |   C:/Program Files/Go/src/runtime/debug/stack.go:24 +0x5e
global_g_api.1.yv6gvbm93sq7@b3saglvue9qgqseg    | github.com/zeromicro/go-zero/core/logx.Must({0x20288e0?, 0xc00038a480?})
global_g_api.1.yv6gvbm93sq7@b3saglvue9qgqseg    |   C:/Users/vipda/go/pkg/mod/github.com/zeromicro/[email protected]/core/logx/logs.go:225 +0x4b
global_g_api.1.yv6gvbm93sq7@b3saglvue9qgqseg    | github.com/zeromicro/go-zero/zrpc.MustNewClient({{{0xc000403220, 0x1, 0x1}, {0xc000406810, 0x9}, 0x0, {0x0, 0x0}, {0x0, 0x0}, ...}, ...}, ...)
global_g_api.1.yv6gvbm93sq7@b3saglvue9qgqseg    |   C:/Users/vipda/go/pkg/mod/github.com/zeromicro/[email protected]/zrpc/client.go:45 +0x65
global_g_api.1.yv6gvbm93sq7@b3saglvue9qgqseg    | globalfun/app/api/internal/svc.NewServiceContext({{{{0xc000406846, 0x3}, {{...}, {...}, {...}, {...}, {...}, {...}, 0x0, 0x0, ...}, ...}, ...}, ...})
global_g_api.1.yv6gvbm93sq7@b3saglvue9qgqseg    |   C:/Users/person/projects/realworld/globalfun-go/globalfun/app/api/internal/svc/servicecontext.go:36 +0xc9
global_g_api.1.yv6gvbm93sq7@b3saglvue9qgqseg    | main.main()
global_g_api.1.yv6gvbm93sq7@b3saglvue9qgqseg    |   C:/Users/person/projects/realworld/globalfun-go/globalfun/app/api/app.go:25 +0x137
global_g_api.1.yv6gvbm93sq7@b3saglvue9qgqseg    | {"@timestamp":"2024-07-01T03:05:57.451Z","content":"rpc dial: etcd://141.2.18.177:2379/gauth.rpc, error: context deadline exceeded, make sure rpc service \"gauth.rpc\" is already started\n\ngoroutine 1 [running]:\nruntime/debug.Stack()\n\tC:/Program Files/Go/src/runtime/debug/stack.go:24 +0x5e\ngithub.com/zeromicro/go-zero/core/logx.Must({0x20288e0?, 0xc00038a480?})\n\tC:/Users/vipda/go/pkg/mod/github.com/zeromicro/[email protected]/core/logx/logs.go:225 +0x4b\ngithub.com/zeromicro/go-zero/zrpc.MustNewClient({{{0xc000403220, 0x1, 0x1}, {0xc000406810, 0x9}, 0x0, {0x0, 0x0}, {0x0, 0x0}, ...}, ...}, ...)\n\tC:/Users/vipda/go/pkg/mod/github.com/zeromicro/[email protected]/zrpc/client.go:45 +0x65\nglobalfun/app/api/internal/svc.NewServiceContext({{{{0xc000406846, 0x3}, {{...}, {...}, {...}, {...}, {...}, {...}, 0x0, 0x0, ...}, ...}, ...}, ...})\n\tC:/Users/person/projects/realworld/globalfun-go/globalfun/app/api/internal/svc/servicecontext.go:36 +0xc9\nmain.main()\n\tC:/Users/person/projects/realworld/globalfun-go/globalfun/app/api/app.go:25 +0x137\n","level":"fatal"}

错误消息似乎表明它找不到密钥

gauth.rpc
,我相信该密钥是在 RPC 服务启动时向 ETCD 注册的。 因此,我使用etcdctl获取所有密钥并检索到以下内容:

commentrpc.rpc/7587879719612179989
10.0.7.12:8152
commentrpc.rpc/7587879719612180006
10.0.7.13:8152
gauth.rpc/7587879719612179977
10.0.7.20:8080
gauth.rpc/7587879719612179981
10.0.7.21:8080
msg.rpc/7587879719612179993
10.0.7.7:8086
msg.rpc/7587879719612179997
10.0.7.6:8086
posts.rpc/7587879719612179973
10.0.7.9:8082
posts.rpc/7587879719612179985
10.0.7.10:8082
users.rpc/7587879719612180001
10.0.7.4:8081
users.rpc/7587879719612180011
10.0.0.3:8081

显然,

gauth.rpc
已经在ETCD中注册,但API无法检索它。我已经解决这个问题几天了,但仍然没有任何线索。我希望得到一些帮助。谢谢大家。

go networking grpc docker-swarm etcd
1个回答
0
投票

我已经解决了这个问题。请原谅我的疏忽。在代码的配置文件中配置服务名称时,我省略了Docker堆栈前缀,直接从YAML文件中写入服务名称。现在,添加 Docker 堆栈服务前缀后,它可以正常工作。 enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.