个人开发机:
申请完后进去:passwd设置密码
# 如果cmake低于3.15, gcc/g++ 低于 7.0 ,请根据文档进行安装。查看版本的方式
cmake -version # 检查cmake版本
gcc -v # 检查gcc/g++版本
g++ -v
sudo apt-get autoremove cmake
wget https://cmake.org/files/v3.23/cmake-3.23.0-linux-x86_64.tar.gz
tar zxf cmake-3.23.0-linux-x86_64.tar.gz
sudo mv cmake-3.23.0-linux-x86_64 /opt/cmake-3.23.0
sudo ln -sf /opt/cmake-3.23.0/bin/* /usr/bin/
cmake -version
cmake .. # 根据cmakeList.txt 生成makefile
cmake -DCMAKE_INSTALL_PREFIX=/usr/local .. // 指定安装路径 /usr/local
make clean
make
sudo apt-get install -y software-properties-common
sudo add-apt-repository ppa:ubuntu-toolchain-r/test
sudo apt update
sudo apt install g++-7 -y
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-7 80 --slave /usr/bin/g++ g++ /usr/bin/g++-7
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-9 60 --slave /usr/bin/g++ g++ /usr/bin/g++-9
# 1 安装依赖环境:
sudo apt-get install pkg-config
sudo apt-get install autoconf automake libtool make g++ unzip
sudo apt-get install libgflags-dev libgtest-dev
sudo apt-get install clang libc++-dev
# 2 下载grpc代码及其依赖包
git clone https://github.com/grpc/grpc.git
cd grpc
git submodule update --init //.gitmodules是依赖的第三方库
# 1.进入grpc源码根路径
$ cd grpc
# 2.创建编译目录
$ mkdir -p cmake/build
# 3.进入编译目录
$ pushd cmake/build
# 4.执行cmake设置cmake参数,并指定安装目录为/usr/local/grpc(该命令会转换成makefile供编译使用)
$ cmake -DgRPC_INSTALL=ON -DgRPC_BUILD_TESTS=OFF -DABSL_ENABLE_INSTALL=TRUE -DCMAKE_INSTALL_PREFIX=/usr/local/grpc ../..
-DBUILD_SHARED_LIBS=ON
# 5.编译
$ make -j 4
# 6.安装(将编译后的目标文件安装到/usr/local/grpc)
$ sudo make install
# 7.退出编译目录
$ popd
###################################################################
# 至此,grpc源码库已编译完成,生成的相关头文件、库文件在/usr/local/grpc目录下
# 8. 查看编译后的目标文件
$ ls /usr/local/grpc
bin include lib share
#安装后,要做其中依赖的absl库的全局环境变量配置,使得API项目能找得到(如果在编译项目时报其他依赖找不到,再加配置)
sudo vim /etc/profile
新建行添加:export CMAKE_PREFIX_PATH=/usr/local/grpc/lib/cmake/absl/:$CMAKE_PREFIX_PATH
source /etc/profile
# 4 下载protobuf及其依赖包
cd third_party/protobuf
git submodule update --init --recursive //依次下载依赖包
sudo apt-get install g++ git bazel //依赖的底层包
bazel build :protoc :protobuf //To build the C++ Protocol Buffer runtime and the Protocol Buffer compiler (protoc) execute the following:
cp bazel-bin/protoc /usr/local/bin
#cmake . -DCMAKE_CXX_STANDARD=14
mkdir -p cmake/build
pushd cmake/build
cmake -Dprotobuf_BUILD_TESTS=OFF -DCMAKE_INSTALL_PREFIX=/usr/local/protobuf .
#cmake --build /usr/local/protobuf --parallel 10
sudo make install
sudo vim /etc/profile
在文件末尾新建行添加:
export PATH=$PATH:/usr/local/protobuf/bin/
export PKG_CONFIG_PATH=/usr/local/protobuf/lib/cmake/
保存退出后:source /etc/profile
2) sudo vim /etc/ld.so.conf
在文件末尾新建行添加:
/usr/local/protobuf/lib
保存退出后:sudo ldconfig
## 在mac上装protobuf
brew install protobuf
## grpc包打包
cd build
cmake .
将下述可执行文件放入bin文件
lib中放入
注意数据转换【proto语法】:https://www.jianshu.com/p/da7ed5914088
protoc -I protos --grpc_out=. --plugin=protoc-gen-grpc=`which grpc_cpp_plugin` protos/parseNLU_service.proto
protoc -I protos --cpp_out=. protos/parseNLU_service.proto
./grpc1.29_pb3.11/bin/protoc -I=./protos/ --grpc_out=./ --plugin=protoc-gen-grpc=./grpc1.29_pb3.11/bin/grpc_cpp_plugin parseNLU_service.proto
./grpc1.29_pb3.11/bin/protoc -I=./protos/ --cpp_out=. protos/parseNLU_service.proto
遇到的问题:
error while loading shared libraries: libgrpc++.so.1
Perhaps you can try:
sudo cp YOUR_GRPC_DIRECTORY/libs/opt/libgrpc++.so.1 /usr/local/lib/
export LD_LIBRARY_PATH=/usr/local/lib
ptoto文件:
import引入,service.proto文件用来定义类型
import "ner.proto";
import "feature_word.proto";
service.proto.ner.NerResponse
编译多个.cc .h文件
如上述所说明的步骤
c++文件:使用service::proto来定义类型
service::proto::feature_word::FeatureWordResponse
https://blog.csdn.net/you_fathe/article/details/128192504
cmakelist.txt https://blog.51cto.com/u_12870633/5827470
#ifndef _CLIENT_H_
#define _CLIENT_H_
#include
#include
#include
#include
#include
//grpc头文件
#include
#include
#include
#include
#include "my_service.grpc.pb.h"
#include "my_service.pb.h"
#include "nlohmann/json.hpp"
#include "tool/MyLogger.h"
using namespace myspace;
namespace myclient {
using google::protobuf::MapPair;
using grpc::Channel;
using grpc::ClientContext;
using grpc::Status;
using grpc::Status;
using myService::myServiceServer;
using myService::Request;
using myService::Response;
class ParseClient{
public:
ParseClient(std::shared_ptr<Channel> channel)
:stub_(myServiceServer::NewStub(channel)){}
ParseClient(std::string address, int timeout, int nRetries):timeout(timeout), nRetries(nRetries){
grpc::ChannelArguments channelArgs;
channelArgs.SetInt(GRPC_ARG_KEEPALIVE_TIME_MS, 30000);
channelArgs.SetInt(GRPC_ARG_KEEPALIVE_TIMEOUT_MS, 10000);
channelArgs.SetInt(GRPC_ARG_KEEPALIVE_PERMIT_WITHOUT_CALLS, 1);
auto channel = grpc::CreateCustomChannel(address, grpc::InsecureChannelCredentials(), channelArgs);
stub_ = parseNLUServiceServer::NewStub(channel);
}
bool request(myService::Request& request, myService::Response& response) {
for (int i = 0; i < nRetries; ++i) {
ClientContext context;
context.set_deadline(std::chrono::system_clock::now() + std::chrono::milliseconds(timeout));
Status status = stub_->myServiceProcess(&context, request, &response);
if(status.ok()) {
std::cout << "status ok " << std::endl;
return true;
}
}
std::cout << "status no after tries" << std::endl;
return false;
}
private:
std::unique_ptr<myServiceServer::Stub> stub_;
int timeout = 500;
int nRetries = 2;
};
}
#endif
myService::Request request;
request.set_query(query);
request.mutable_nerresult()->CopyFrom(*nerResult);
//注意不要用set_allocated_nerresult();
int process_from_ip(std::string ip, myService::Request& request, myService::Response& response)
{
try {
myclient::ParseClient my_client(ip, 500, 2);
bool flag = my_client.request(request, response);
if (flag == false) {
return -1;
}
} catch (const exception &e) {
return -1;
}
return 0;
}
安装所需的库
pip install grpcio-tools
pip install protobuf
python -m grpc_tools.protoc -I ./proto --python_out=./python_client --grpc_python_out=./python_client protos/test.proto
生成test_pb2, test_pb2_grpc
from __future__ import print_function
import logging
import grpc
import test_pb2, test_pb2_grpc
def run():
print("Will try python grpc test")
input_data = input("input message: ")
with grpc.insecure_channel('serverip') as channel:
print(" " * 5)
stub = test_pb2_grpc.TestServiceServerStub(channel) //TestServiceServerStub可以到test_pb2_grpc文件里面找一下
response = stub.testServiceProcess(test_pb2_grpc.Request(query=input_data)) //testServiceProcess为proto文件里面定义的rpc方法
print("test client received: ")
for slot in response.slotInfosRes.slotRes:
print("---" * 5)
print("slot.SkillDomain :", slot.slotSkill, ", ", slot.slotDomain, ", ", slot.slotDictName, ", ", slot.slotDictId)
print("slot.RawValue :",slot.slotRawValue, ", ", slot.slotBegin, ", ", slot.slotEnd)
print("slot.IsBlackWhite :",slot.slotIsBlackWhite)
print("slot.slotScoreHotTypeAttr :",slot.slotScore, ", ", slot.slotHot, ", ", slot.slotType, ", ", slot.slotAttr)
if __name__ == '__main__':
logging.basicConfig()
run()