我正在使用Go语言框架Go-Zero开发一个个人项目。在 Golang 端,有四个服务:BFF(API 服务),以及三个 RPC 服务,分别为 postsRpc、gauthRpc、usersRpc,BBF 依赖于三个 Rpc 服务,如下:
Name: app
Host: 0.0.0.0
Port: 8888
UserRpc:
Etcd:
Hosts:
- 127.0.0.1:2379
Key: users.rpc
GauthRpc:
Etcd:
Hosts:
- 127.0.0.1:2379
Key: gauth.rpc
PostsRpc:
Etcd:
Hosts:
- 127.0.0.1:2379
Key: posts.rpc
authRpc
Name: gauth.rpc
ListenOn: 0.0.0.0:8080
Etcd:
Hosts:
- 127.0.0.1:2379
Key: gauth.rpc
帖子 Rpc
Name: posts.rpc
ListenOn: 0.0.0.0:8082
Etcd:
Hosts:
- 127.0.0.1:2379
Key: posts.rpc
用户Rpc
Name: users.rpc
ListenOn: 0.0.0.0:8081
Etcd:
Hosts:
- 127.0.0.1:2379
Key: users.rpc
我在本地机器上运行 docker-compose,yaml 文件内容如下:
version: '3.1'
services:
globaldb:
image: mysql:8.0
restart: always
command: '--default-authentication-plugin=mysql_native_password'
healthcheck:
test: ['CMD-SHELL', 'mysqladmin ping -h 127.0.0.1 --password="$$(cat /run/secrets/db-password)" --silent']
interval: 3s
retries: 5
ports:
- "23306:3306"
volumes:
- ./db/db-data:/var/lib/mysql
environment:
MYSQL_DATABASE: globalfun
MYSQL_ROOT_PASSWORD: root
networks:
- globalnet
globalrd:
image: redis:latest
restart: always
ports:
- "26379:6379"
command: redis-server --save 20 1 --loglevel warning --requirepass globalroot
volumes:
- ./rd:/data
networks:
- globalnet
globaletcd:
image: bitnami/etcd:latest
restart: always
ports:
- "2379:2379"
environment:
- ALLOW_NONE_AUTHENTICATION=yes
networks:
- globalnet
globalmongo:
image: mongo:7.0.3
container_name: globalmongo
ports:
- 27017:27017
environment:
- MONGO_INITDB_DATABASE=globalfun
# - MONGO_INITDB_ROOT_USERNAME=admin
# - MONGO_INITDB_ROOT_PASSWORD=globalroot
- MONGO_INITDB_ROOT_USERNAME=root
- MONGO_INITDB_ROOT_PASSWORD=globalroot
volumes:
# seeding scripts
- ./mongo-entrypoint:/docker-entrypoint-initdb.d
# named volumes
- ./mongodata/mongodb:/data/db
- ./mongodata/mongoconfig:/data/configdb
networks:
- globalnet
networks:
globalnet:
driver: bridge
当我运行三个RPC服务时,ETCD有以下三个键:
@35458f0c3201:/opt/bitnami/etcd$ etcdctl get "" --prefix
gauth.rpc/7587875305045080324
192.168.31.119:8080
posts.rpc/7587875305045080330
192.168.31.119:8082
users.rpc/7587875305045080327
192.168.31.119:8081
但是当我运行 BFF 主 go 文件时:
app.go
,我感到恐慌:
API server listening at: 127.0.0.1:2913
2023/12/13 21:05:21 rpc dial: etcd://127.0.0.1:2379/users.rpc, error: context deadline exceeded, make sure rpc service "users.rpc" is already started
goroutine 1 [running]:
runtime/debug.Stack()
C:/Program Files/Go/src/runtime/debug/stack.go:24 +0x6b
github.com/zeromicro/go-zero/core/logx.Must({0x34b0ac0, 0xc0005b8050})
C:/Users/vipda/go/pkg/mod/github.com/zeromicro/[email protected]/core/logx/logs.go:225 +0x57
github.com/zeromicro/go-zero/zrpc.MustNewClient({{{0xc00049ab30, 0x1, 0x1}, {0xc0005b1620, 0x9}, 0x0, {0x0, 0x0}, {0x0, 0x0}, ...}, ...}, ...)
C:/Users/vipda/go/pkg/mod/github.com/zeromicro/[email protected]/zrpc/client.go:45 +0x13d
globalfun/globalfun/app/api/internal/svc.NewServiceContext({{{{0xc0005b15bc, 0x3}, {{...}, {...}, {...}, {...}, {...}, {...}, 0x0, 0x0, ...}, ...}, ...}, ...})
C:/Users/person/projects/realworld/globalfun-go/globalfun/app/api/internal/svc/servicecontext.go:29 +0x65
main.main()
C:/Users/person/projects/realworld/globalfun-go/globalfun/app/api/app.go:26 +0x187
{"@timestamp":"2023-12-13T21:05:21.849+08:00","content":"rpc dial: etcd://127.0.0.1:2379/users.rpc, error: context deadline exceeded, make sure rpc service \"users.rpc\" is already started\n\ngoroutine 1 [running]:\nruntime/debug.Stack()\n\tC:/Program Files/Go/src/runtime/debug/stack.go:24 +0x6b\ngithub.com/zeromicro/go-zero/core/logx.Must({0x34b0ac0, 0xc0005b8050})\n\tC:/Users/vipda/go/pkg/mod/github.com/zeromicro/[email protected]/core/logx/logs.go:225 +0x57\ngithub.com/zeromicro/go-zero/zrpc.MustNewClient({{{0xc00049ab3
0, 0x1, 0x1}, {0xc0005b1620, 0x9}, 0x0, {0x0, 0x0}, {0x0, 0x0}, ...}, ...}, ...)\n\tC:/Users/vipda/go/pkg/mod/github.com/zeromicro/[email protected]/zrpc/client.go:45 +0x13d\nglo
balfun/globalfun/app/api/internal/svc.NewServiceContext({{{{0xc0005b15bc, 0x3}, {{...}, {...}, {...}, {...}, {...}, {...}, 0x0, 0x0, ...}, ...}, ...}, ...})\n\tC:/Users/person
/projects/realworld/globalfun-go/globalfun/app/api/internal/svc/servicecontext.go:29 +0x65\nmain.main()\n\tC:/Users/person/projects/realworld/globalfun-go/globalfun/app/api/app.go:26 +0x187\n","level":"fatal"}
Debugger finished with the exit code 0
但是
users.rpc
密钥已经在etcd中,为什么BFF api服务找不到密钥?
这真的让我感到沮丧,如果有人能找到恐慌原因,请给我一些建议。
我找到了BFF找不到
users.rpc
键的原因,是我本机代理环境http_proxy/https_proxy设置了值127.0.0.1:7890
导致的,而代理ip与docker容器ip不同,所以当我删除http_proxy/https_proxy路径时, 有效!
谢谢大家。