无法连接到我的容器化 gRPC 服务器。

huangapple go评论96阅读模式
英文:

Cannot connect to my containerized gRPC server

问题

我有一个具有给定文件夹结构的git仓库:

grpc-repo
|---grpc-client (:8083)
|---grpc-server (:9000)
|---docker-compose.yml
|---Dockerfile
|---Dockerfile2

现在,这个gRPC客户端-服务器项目正常无缝运行。
但是,我需要将这个项目容器化。

下面是我的docker-compose.yml文件:

version: "3.3"

services:
client:
container_name: grpc-client
build:
dockerfile: Dockerfile
context: .
volumes:
- .:/app
ports:
- 8083:8083
networks:
- my-network
server:
container_name: grpc-server
build:
dockerfile: Dockerfile2
context: .
volumes:
- .:/app
ports:
- 9000:9000
networks:
- my-network

networks:
my-network:
driver: bridge

将项目容器化后,我可以轻松连接到gRPC客户端,但是客户端无法连接到gRPC服务器。

它显示以下错误信息:

rpc error: code = Unavailable desc = connection error: desc = "transport: Error while dialing dial tcp :9000: connect: connection refused"

以下是我在我的golang代码中创建与服务器连接的方式:

conn, err := grpc.Dial(":9000", grpc.WithInsecure())
if err != nil {
log.Fatalf("could not connect: %s", err)
}

有人可以帮我看看我在这里做错了什么吗?提前感谢!

英文:

I have a git repo with the given folder structure

grpc-repo
|---grpc-client (:8083)
|---grpc-server (:9000)
|---docker-compose.yml
|---Dockerfile
|---Dockerfile2

Now, these gRPC client-server project is running seamless normally.
But then, I need to dockerize this project.

Given below is my docker-compose.yml

version: "3.3"

services:
  client:
    container_name: grpc-client
    build:
      dockerfile: Dockerfile
      context: .
    volumes:
      - .:/app
    ports:
      - 8083:8083
    networks:
      - my-network
  server:
    container_name: grpc-server
    build:
      dockerfile: Dockerfile2
      context: .
    volumes:
      - .:/app
    ports:
      - 9000:9000
    networks:
      - my-network

networks:
  my-network:
    driver: bridge

After dockerizing the project, I am easily able to connect to the gRPC client.. but the client is not able to connect to the gRPC server.

It says ...

rpc error: code = Unavailable desc = connection error: desc = \"transport: Error while dialing dial tcp :9000: connect: connection refused

This is how I am creating a connection to the server in my golang code

conn, err := grpc.Dial(":9000", grpc.WithInsecure())
if err != nil {
	log.Fatalf("could not connect: %s", err)
}

Can anyone help me with what am I doing wrong here ?
Thanks in advance !

答案1

得分: 6

要从另一个容器访问在Docker容器中运行的gRPC,您需要使用DNS解析。

grpc.Dial("dns:///grpc-server:9000", grpc.WithInsecure())

其中grpc-server是您在container_name: grpc-server中声明的容器名称。

英文:

To access a gRPC running in a docker container from another container you have to use DNS resolution.

grpc.Dial("dns:///grpc-server:9000", grpc.WithInsecure())

where grpc-server is the container name you declared in container_name: grpc-server

huangapple
  • 本文由 发表于 2021年11月29日 18:11:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/70152887.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定