dongying3830 2017-06-20 01:11
浏览 740
已采纳

使用gRPC在容器之间进行通信

I'm implementing two services (server and client) running on two different containers and trying to use gRPC to communicate between them.

If I deploy the server as a container and run the client not on a container, everything works just fine. But if I deploy the client on a different container I get Error #01: could not retrieve restaurant's list: rpc error: code = Unavailable desc = grpc: the connection is unavailable error.

What steps am I missing to communicate two different containers using gRPC?

PS: I would like to try without Kubernetes for now.

SERVER

func Serve() {

    log.Println("serving...")

    port := ":50051"

    lis, err := net.Listen("tcp", port)
    if err != nil {
        log.Fatalf("failed to listen on port %s: %v", port, err)
    }

    s := grpc.NewServer()
    server := server{}
    RegisterNeo4BaconServer(s, server)

    if err := s.Serve(lis); err != nil {
        log.Fatal("could not serve: ", err)
    }
}

Makefile

image: ## build docker image and push image to registry
    @docker build -t neo4bacon -f resources/prod/Dockerfile .

run: ## deploy application container
    @docker run --rm -d --name neo4bacon neo4bacon

CLIENT

func Get() (*api.RestaurantList, error) {
    // Neo4bacon backend
    backendPort := ":50051"

    conn, err := grpc.Dial(backendPort, grpc.WithInsecure())
    if err != nil {
        &api.RestaurantList{}, fmt.Errorf("could not connect to backend %s: %s", backendPort, err)
    }
    defer conn.Close()

    client := api.NewNeo4BaconClient(conn)

    restaurantList, err := client.List(context.Background(), &api.Empty{})
    if err != nil {
        return &api.RestaurantList{}, fmt.Errorf("could not retrieve restaurant's list: %s", err)
    }
    return restaurantList, nil
}

Makefile

image: ## build docker image and push image to registry     
@docker build -t alesr/bacon-api -f resources/prod/Dockerfile .     

run: ## deploy docker container     
@docker run --rm -d -p 8080:8080 --name bacon-api bacon-api
  • 写回答

1条回答 默认 最新

  • douqujin2767 2017-06-20 01:37
    关注

    You need to include the hostname in the dial function, otherwise it's looking at localhost which is unique to each container (docker creates a separate networking namespace for containers by default). Change the following:

    backendPort := "neo4bacon:50051"
    

    Edit: you also need to setup a network and connect the containers to that network because the default bridge does not include DNS discovery:

    docker network create baconnet
    docker run --rm -d --net baconnet --name neo4bacon neo4bacon
    docker run --rm -d --net baconnet -p 8080:8080 --name bacon-api bacon-api
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 FOR循环语句显示查询超过300S错误怎么办
  • ¥15 个人通讯录管理系统 C语言 程序设计 需要能运行成功的 结构体和数组 Visual C++编译器
  • ¥15 数电设计题 没有设计思路 不知道用什么芯片进行设计 求提供设计思路
  • ¥15 在动态多目标优化问题中,第一幅图展示的是问题DF6的相关定义和绘制的POS和POF图,请问图中公式PS(t)和PF(t)是如何推导的
  • ¥60 先数学建模,接着设计一种优化算法结合案例给出智能仓储四向穿梭车的调度计划
  • ¥15 Errno2:No such file or directory,在当前文件确实没有该图片,怎么解决?
  • ¥15 博世摄像头数据存储的问题(iscsi)
  • ¥15 如何实现对学生籍贯信息管理系统的选择排序
  • ¥15 写一个51单片机的时钟代码
  • ¥15 git clone报错