背景介绍
tensorflow serving 在客户端和服务端之间的通信采用的是RPC/REST协议。在TFS提供的REST协议接口存在一定的局限性,REST和RPC对比如下:
- 1、REST在实际应用中不能支持运行过程动态模型发布。
- 2、REST预测过程中组装报文格式复杂。
- 3、RPC接口提供TFS的所有核心能力。
基于以上原因且考虑我们主要使用java进行编程,因此我们必须具备能够编译TFS JAVA API的能力,这篇文章主要目的就是提供编译TFS JAVA API的方法。
编译方法
step_1 安装protoc
protoc 3 已经有编译好的版本, 直接从protoc官网 下载编译好的安装包 protoc-3.6.1-osx-x86_64.zip
, 然后将命令复制到 /usr/local/bin
即可。
cd /tmp
mv protoc-3.5.1-osx-x86_64.zip .
unzip protoc-3.5.1-osx-x86_64.zip
cd bin
cp protoc /usr/local/bin/
lebron374$ protoc --version
libprotoc 3.6.1
step_2 maven构建工程
参考TFS API编译文章,核心操作的主要步骤都在文章里面写的明白。核心步骤主要是:
- 1、下载TF和TFS的代码,切换相同版本的分支保证TFS和TF的代码兼容,参考tensorflow-serving-api。
- 2、拷贝TF和TFS的proto文件到指定目录,参考tensorflow-serving-api initialize.sh文件
- 3、修改pom.xml依赖,参考tensorflow-serving-api,实际视具体依赖添加mvn依赖。
核心过程注解
拷贝proto文件
#!/bin/sh
SUBMODULE_PATH_TENSOR_FLOW="src/external/tensorflow"
SUBMODULE_PATH_TENSOR_SERV="src/external/tensorflow-serving"
DEST_SRC_PATH="src/main/proto"
function copy_proto_files()
{
dest_path=$1
src_path=$2
cd ${src_path}
proto_files=`find . | grep '\.proto$' | grep -v 'host'`
cd -
for file in ${proto_files}
do
echo ${file}
sub_file_path=`echo ${file} | awk 'BEGIN{FS=OFS="/";}{seg=$2; for(i=3; i<NF; ++i){seg=seg"/"$i;}}END{print seg;}'`
sub_file_name=`echo ${file} | awk 'BEGIN{FS=OFS="/";}{print $NF;}'`
src_file_name=${src_path}/${sub_file_path}/${sub_file_name}
dest_file_path=${dest_path}/${sub_file_path}
if [ ! -f ${dest_file_path} ]
then
mkdir -p ${dest_file_path}
fi
cp ${src_file_name} ${dest_file_path}
done
}
copy_proto_files ${DEST_SRC_PATH} ${SUBMODULE_PATH_TENSOR_FLOW}
copy_proto_files ${DEST_SRC_PATH} ${SUBMODULE_PATH_TENSOR_SERV}
说明:
- proto_files=
find . | grep '\.proto$' | grep -v 'host'
负责拷贝proto文件,实际过程中因为编译冲突动态排除几个冲突的proto文件。
pom文件
<?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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
<tensor.jar.version>1.8.0-SNAPSHOT</tensor.jar.version>
<tensor.version>1.8.0</tensor.version>
<grpc.version>1.12.0</grpc.version>
<protobuf.version>3.5.1</protobuf.version>
<protobuf.plugin.version>0.5.1</protobuf.plugin.version>
</properties>
<groupId>tensorflow.serving</groupId>
<artifactId>tensorflow-serving-api</artifactId>
<version>${tensor.jar.version}</version>
<packaging>jar</packaging>
<distributionManagement>
<repository>
<id>nexus</id>
<name>xxx nexus</name>
<url>http://maven.repos.xxx.com/nexus/content/repositories/snapshots/</url>
</repository>
</distributionManagement>
<dependencies>
<dependency>
<groupId>org.tensorflow</groupId>
<artifactId>proto</artifactId>
<version>${tensor.version}</version>
</dependency>
<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-netty</artifactId>
<version>${grpc.version}</version>
</dependency>
<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-protobuf</artifactId>
<version>${grpc.version}</version>
</dependency>
<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-stub</artifactId>
<version>${grpc.version}</version>
</dependency>
<dependency>
<groupId>com.google.protobuf</groupId>
<artifactId>protobuf-java</artifactId>
<version>${protobuf.version}</version>
</dependency>
</dependencies>
<build>
<extensions>
<extension>
<groupId>kr.motd.maven</groupId>
<artifactId>os-maven-plugin</artifactId>
<version>1.6.0</version>
</extension>
</extensions>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
</configuration>
</plugin>
<plugin>
<groupId>org.xolstice.maven.plugins</groupId>
<artifactId>protobuf-maven-plugin</artifactId>
<version>${protobuf.plugin.version}</version>
<configuration>
<protocArtifact>
com.google.protobuf:protoc:${protobuf.version}:exe:${os.detected.classifier}
</protocArtifact>
<pluginId>grpc-java</pluginId>
<pluginArtifact>
io.grpc:protoc-gen-grpc-java:${grpc.version}:exe:${os.detected.classifier}
</pluginArtifact>
</configuration>
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>compile-custom</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
网友评论