用Dockerated GRPC生成PHP库 [英] Generating PHP library with Dockerized gRPC

查看:16
本文介绍了用Dockerated GRPC生成PHP库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在docker中构建一个GRPC PHP客户端和GRPC NodeJS服务器。但问题是我无法在我的扩展底座服务器上安装protoc-gen-php-grpc。当我尝试运行此命令时,请运行此生成文件:

proto_from_within_container:
    # PHP
    protoc /var/www/protos/smellycat.proto 
        --php_out=/var/www/php-client/src 
        $(: 👇 generate server interface) 
        --php-grpc_out=/var/www/php-client/src 
        $(: 👇 generates the client code) 
        --grpc_out=/var/www/php-client/src 
        --plugin=protoc-gen-grpc=/protobuf/grpc/bins/opt/grpc_php_plugin 
        --proto_path /var/www/protos

proto:
    powershell rm -r -fo php-client/src -ErrorAction SilentlyContinue
    powershell New-Item -ItemType Directory -Path php-client/src -Force -ErrorAction SilentlyContinue
    docker-compose run grpc-server make proto_from_within_container

使用此命令:make proto

生成停靠容器后收到此错误消息:

protoc /var/www/protos/smellycat.proto 

--php_out=/var/www/php-client/src 



--php-grpc_out=/var/www/php-client/src 



--grpc_out=/var/www/php-client/src 

--plugin=protoc-gen-grpc=/protobuf/grpc/bins/opt/grpc_php_plugin 

--proto_path /var/www/protos

protoc-gen-php-grpc: program not found or is not executable

Please specify a program using absolute path or make sure the program is available in your PATH system variable

--php-grpc_out: protoc-gen-php-grpc: Plugin failed with status code 1.

Makefile:4: recipe for target 'proto_from_within_container' failed

make: *** [proto_from_within_container] Error 1

这是我的docker合成文件

version: "3"

services:
  grpc-server:
    container_name: grpc-server
    build:
      context: .
      dockerfile: Dockerfile-server
    working_dir: /var/www
    volumes:
      - .:/var/www

  grpc-client:
    image: php:7.4-cli
    container_name: grpc-client
    build:
      context: .
      dockerfile: Dockerfile-client
    working_dir: /var/www
    volumes:
      - .:/var/www
    command: bash -c [php php_client.php && composer install]

这是我的GRPC-服务器扩展坞文件:

FROM node:latest

ENV DEBIAN_FRONTEND=noninteractive

#Versions
ARG PROTOBUF_VERSION=3.14.0
ARG PHP_GRPC_VERSION=1.34.0

# Utils
RUN apt-get update -yqq 
  && apt-get install -yqq wget unzip zlib1g-dev git autoconf libtool automake build-essential software-properties-common curl zip 
  && rm -rf /var/lib/apt/lists/*

# Protobuf
RUN mkdir -p /protobuf
RUN cd /protobuf 
    && wget https://github.com/protocolbuffers/protobuf/releases/download/v${PROTOBUF_VERSION}/protoc-${PROTOBUF_VERSION}-linux-x86_64.zip -O protobuf.zip 
    && unzip protobuf.zip && rm protobuf.zip

# grpc PHP (generate client)
RUN apt-get update -yqq && apt-get upgrade -yqq
RUN apt-get install php php-dev php-pear phpunit zlib1g-dev -yqq
RUN pecl install grpc-${PHP_GRPC_VERSION}
RUN cd /protobuf && git clone -b v${PHP_GRPC_VERSION} https://github.com/grpc/grpc 
    && cd /protobuf/grpc && git submodule update --init
RUN cd /protobuf/grpc && make grpc_php_plugin

ENV PATH "/protobuf/bin:${PATH}"
ENV PATH "/protobuf/grpc/bins/opt:${PATH}"

# NPM Installation
WORKDIR /var/www
COPY . /var/www
RUN npm install

CMD ["node", "server.js"]

您有什么建议吗?

推荐答案

经过大量搜索和阅读,我终于成功地构建了一个相互通信的完整应用程序。 问题出在生成文件中,在此步骤中:

--plugin=protoc-gen-grpc=/protobuf/grpc/bins/opt/grpc_php_plugin

我为grpc_php_plugin分配了错误的路径。

这是我的新dockerfile:

FROM php:7.4-cli

# Environment variables
ENV DEBIAN_FRONTEND=noninteractive

# Utils
RUN apt-get update -yqq && 
    apt-get upgrade -yqq &&  
    apt-get install -y unzip build-essential git software-properties-common curl pkg-config zip zlib1g-dev

# Composer installation
COPY --from=composer:latest /usr/bin/composer /usr/local/bin/composer

# Install grpc and probuf with pecl
RUN pecl install grpc && pecl install protobuf

# Enable grpc and protobuf extensions in php.ini file
RUN echo starting && 
    docker-php-ext-enable grpc && 
    docker-php-ext-enable protobuf

# Install cmake
RUN apt-get update -yqq && apt-get -y install cmake

# Install grpc_php_plugin and protoc
RUN git clone -b v1.36.2 https://github.com/grpc/grpc && 
    cd grpc && git submodule update --init && 
    mkdir cmake/build && cd cmake/build && 
    cmake ../.. && make protoc grpc_php_plugin

# Setting node, protoc and grpc_php_plugin paths
ENV PATH "/grpc/cmake/build:${PATH}"
ENV PATH "/grpc/cmake/build/third_party/protobuf:${PATH}"


# Moving client folder to vm
WORKDIR /var/www
COPY ./client /var/www

# Packages
RUN composer install

# Generate php libraries from proto file
RUN make proto

CMD [ "php", "./handler.php" ]

对于我的完整应用程序,click

这篇关于用Dockerated GRPC生成PHP库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆