猿问

docker 容器之间的 gRPC 通信

我的本地(没有 docker)很好,工作没有任何问题。


本地设置工作正常。


Server-Jar:运行于 8081


Client-War:运行在 8000,可以毫无问题地连接到 Server Jar。


DOCKER 设置docker-compose.yml


服务器


employee:

    image: openjdk:jdk-alpine

    container_name: "employee"

    ports:

      - 9081:8080

      - 9991:9990

      - 65193:65193

    volumes:

      - ./Employee/target/Employee-1.0-SNAPSHOT-jar-with-dependencies.jar:/deployments/Employee-1.0-SNAPSHOT-jar-with-dependencies.jar

    environment:

      - JAVA_OPTS=-agentlib:jdwp=transport=dt_socket,address=0.0.0.0:65193,suspend=n,server=y -Djava.net.preferIPv4Stack=true

      - GRPC_PORT=8080

    command: java -jar /deployments/Employee-1.0-SNAPSHOT-jar-with-dependencies.jar






client:

    image: jboss/wildfly

    container_name: "client"

    ports:

      - 9080:8080

      - 9990:9990

      - 65193:65193

    volumes:

      - ./Service/target/Service.war:/opt/jboss/wildfly/standalone/deployments/Service.war

    environment:

      - JAVA_OPTS=-agentlib:jdwp=transport=dt_socket,address=0.0.0.0:65193,suspend=n,server=y -Djava.net.preferIPv4Stack=true

      - EMPLOYEE_HOST=localhost:9081

    command: >

      bash -c "/opt/jboss/wildfly/bin/add-user.sh admin Admin#007 --silent && /opt/jboss/wildfly/bin/standalone.sh -b 0.0.0.0 -bmanagement 0.0.0.0"

links:

  - employee

depends_on:

  - employee

员工服务器代码


Server server = ServerBuilder.forPort(Integer.parseInt(env.get("GRPC_PORT")))

        .addService(new Employee())

        .build();

try {

    server.start();

    System.out.println("Server listening at: " + env.get("GRPC_PORT"));

    server.awaitTermination();

} catch (IOException | InterruptedException e) {

    e.printStackTrace();

}

客户端连接代码。


ManagedChannel channel = ManagedChannelBuilder.forTarget(env.get("EMPLOYEE_HOST")).usePlaintext().build();

EmployeeServiceGrpc.EmployeeServiceBlockingStub employee = EmployeeServiceGrpc.newBlockingStub(channel);

当我进行 gRPC 调用时抛出错误。


您认为 docker 设置正确吗?如果那么我可以在其他地方寻找问题


喵喔喔
浏览 280回答 1
1回答

Qyouu

看到这个人为的例子:version: "3"services:  server:    image: nginx:1.17.3    restart: always    container_name: nginx    expose:    - "80"    ports:    - 8888:80  client:    image: busybox:1.31.0    restart: always    depends_on:    - server    container_name: busybox    command:    - ash    - -c    - |      while true;      do        wget --spider http://server:80 2>&1        sleep 5s;      done;client通过端口访问 Nginx 容器80,但它用作server主机地址以解析到适当的 (Nginx) 容器。如果您要替换server为localhostin client,则客户端会尝试wget对自己执行命令,但会失败。因为server发布8888到主机,当 Docker Compose 启动时,从主机(!),你可以wget http://localhost:8888并且这将起作用(200)。
随时随地看视频慕课网APP

相关分类

Java
我要回答