【发布时间】:2018-02-15 16:37:11
【问题描述】:
背景:我的团队正在努力使我们的 Java 服务套件实现容器化和动态可扩展。为了实现这一点,我们的计划是使用由 etcd 支持的 Envoy,使用 Envoy documentation 中描述的自定义构建 Endpoint Discovery Service,使用 v2 gRPC-based API。然后,我们将为每个服务生成 Docker 映像,并使用 Kubernetes 部署/管理它们。
我们使用 Maven 作为构建系统。我非常精通 Maven,但这是我第一次使用 gRPC 或协议缓冲区。
我使用 Spring Boot 为我的服务创建了一个存根,Jetty 提供了一些 REST 和 JMX 端点进行管理。在引入 protobuf 之前,存根构建并运行得很好。
我已经下载了Envoy data-plane-api 并将 API 定义文件 (**/*.proto) 检查到我的项目中的 src/main/proto 下,保留了下载目录结构(例如 src/main/proto/特使/api/v2/eds.proto)。 (附带问题:我需要 BUILD 文件吗?)
最后,我想要一个独立的 Maven 构建,它可以读取这些文件并生成 Java 类。该构建需要在 Windows 和 OS X 机器上运行,以便它适用于开发人员,并在 Linux 机器上运行,以便它在我们的 CI(Bamboo)中运行。它应该只需要一个 JDK、一个 Maven 安装和一个 Maven 存储库。 (我们有一个 Artifactory 实例,如果需要,我可以在其中上传其他方式无法在线获得的工件。)
到目前为止,我所拥有的似乎可以实现我的可移植性目标:
pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>discovery-service</artifactId>
<packaging>jar</packaging>
<name>Discovery Service</name>
<version>1.0.0</version>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
<configuration>
<verbose>true</verbose>
<fork>true</fork>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.xolstice.maven.plugins</groupId>
<artifactId>protobuf-maven-plugin</artifactId>
<version>0.5.1</version>
<configuration>
<protocArtifact>com.google.protobuf:protoc:3.5.1-1:exe:${os.detected.classifier}</protocArtifact>
<checkStaleness>true</checkStaleness>
</configuration>
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>test-compile</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
<extensions>
<!-- provides os.detected.classifier (i.e. linux-x86_64, osx-x86_64) property -->
<extension>
<groupId>kr.motd.maven</groupId>
<artifactId>os-maven-plugin</artifactId>
<version>1.4.1.Final</version>
</extension>
</extensions>
</build>
<dependencies>
<dependency>
<groupId>io.hydrosphere</groupId>
<artifactId>envoy-data-plane-api_2.11</artifactId>
<version>v1.5.0_1</version>
</dependency>
<dependency>
<groupId>com.google.protobuf</groupId>
<artifactId>protobuf-java</artifactId>
<version>3.5.1</version>
</dependency>
<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-core</artifactId>
<version>1.9.1</version>
</dependency>
<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-protobuf</artifactId>
<version>1.9.1</version>
</dependency>
<!--Unrelated deps (Spring Boot, Jetty, logging, etc) omitted for brevity -->
</dependencies>
</project>
当我使用mvn compile 构建这个项目时,Maven 正确下载了protoc 并调用它,但是由于找不到一些外部依赖项而出现了 protoc 错误:
[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] Detecting the operating system and CPU architecture
[INFO] ------------------------------------------------------------------------
[INFO] os.detected.name: osx
[INFO] os.detected.arch: x86_64
[INFO] os.detected.classifier: osx-x86_64
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building Discovery Service 1.0.0
[INFO] ------------------------------------------------------------------------
Downloading: http://devutl1.sircon.com:8081/artifactory/libs-snapshot/org/glassfish/javax.el/maven-metadata.xml
Downloading: http://devutl1.sircon.com:8081/artifactory/libs-release/org/glassfish/javax.el/maven-metadata.xml
Downloaded: http://devutl1.sircon.com:8081/artifactory/libs-release/org/glassfish/javax.el/maven-metadata.xml (882 B at 547 B/s)
Downloaded: http://devutl1.sircon.com:8081/artifactory/libs-snapshot/org/glassfish/javax.el/maven-metadata.xml (882 B at 547 B/s)
[INFO]
[INFO] --- maven-enforcer-plugin:3.0.0-M1:enforce (default-cli) @ discovery-service ---
[INFO]
[INFO] --- protobuf-maven-plugin:0.5.1:compile (default) @ discovery-service ---
[INFO] Compiling 56 proto file(s) to /Users/jrobb/Projects/vertabrae/trunk/scaling/discovery-service/target/generated-sources/protobuf/java
[ERROR] PROTOC FAILED: validate/validate.proto: File not found.
gogoproto/gogo.proto: File not found.
envoy/api/v2/core/address.proto: Import "validate/validate.proto" was not found or had errors.
envoy/api/v2/core/address.proto: Import "gogoproto/gogo.proto" was not found or had errors.
envoy/config/metrics/v2/stats.proto: Import "envoy/api/v2/core/address.proto" was not found or had errors.
envoy/config/metrics/v2/stats.proto: Import "validate/validate.proto" was not found or had errors.
envoy/config/metrics/v2/stats.proto:148:5: "envoy.api.v2.core.Address" is not defined.
envoy/config/metrics/v2/stats.proto:167:5: "envoy.api.v2.core.Address" is not defined.
输出继续,每个.proto 文件都有相同的错误集。
似乎问题的症结在于我没有 validate/validate.proto 或 gogoproto/gogo.proto,它们是在(几乎?)每个 .proto 文件的顶部导入的:
import "google/protobuf/wrappers.proto";
import "validate/validate.proto";
import "gogoproto/gogo.proto";
它似乎找到了wrappers.proto,我认为它来自我对protobuf-java 的编译时Maven 依赖。
我认为gogoproto/gogo.proto 可能正在寻找这个:https://github.com/gogo/protobuf/blob/master/gogoproto/gogo.proto
我完全不知道validate/validate.proto 应该来自哪里。我已经看到一些证据表明它是 Envoy 特有的,但我找不到它。
我在过去的几个小时里一直在搜索,但我对以前做过这件事的任何人都一无所知。我从哪里得到这些文件?如果我无法从 Maven Central 获取它们,我将自己构建它们并上传到 Artifactory。
查看为使用其他技术堆栈的人准备的文档时,我的眼睛发呆,我无法根据自己的需要进行翻译。任何和所有的帮助都将不胜感激,如果这是一个非常愚蠢的问题,我深表歉意。 :)
【问题讨论】:
-
好问题;我还试图弄清楚如何从我的 Java 应用程序中以合理的方式使用来自 Envoy 的
.proto文件......所以,Jake 那里的研究工作很棒! :) -
我有类似的问题,任何帮助将不胜感激:stackoverflow.com/questions/65560573/…
-
@TheGr8Adakron 我相信我的回答成立。无需直接使用 .proto 文件,只需导入 java-control-plane JAR。