Android native使用gRPC通信的一种实现方式

目标是在Android上使用gRPC进行通信,当然是在Android的Native层的需求,应用层要使用gRPC会方便的多,搜一下资源,build.gradle配置一下依赖就能用了。下面在Ubuntu系统中来编译在Android Native运行的gRPC的库,分为:
1 下载gRPC源码
2 使用Android源码自带的编译工具编译gRPC
3 使用gRPC

下载gRPC源码

在github下载gRPC项目,然后更新子模块:

git clone https://gitee.com/githubplus/grpc.git
cd grpc
git tag
git checkout v1.20.0

更新submodule:

git submodule update --init

直接更新速度特别慢,甚至n次的中断,不知道什么岁月才能更新完,因此请修改.gitmodules文件,替换其中的github源为gitee源

vim .gitmodules

// replace content in .gitmodules
[submodule "third_party/zlib"]
	path = third_party/zlib
	url = https://gitee.com/githubplus/zlib
	# When using CMake to build, the zlib submodule ends up with a
	# generated file that makes Git consider the submodule dirty. This
	# state can be ignored for day-to-day development on gRPC.
	ignore = dirty
[submodule "third_party/protobuf"]
	path = third_party/protobuf
	url = https://gitee.com/githubplus/protobuf.git
	branch = 3.0.x
[submodule "third_party/gflags"]
	path = third_party/gflags
	url = https://gitee.com/githubplus/gflags.git
[submodule "third_party/googletest"]
	path = third_party/googletest
	url = https://gitee.com/githubplus/googletest.git
[submodule "third_party/boringssl"]
	path = third_party/boringssl
	url = https://gitee.com/githubplus/boringssl.git
[submodule "third_party/benchmark"]
	path = third_party/benchmark
	url = https://gitee.com/githubplus/benchmark.git
[submodule "third_party/boringssl-with-bazel"]
	path = third_party/boringssl-with-bazel
	url = https://gitee.com/githubplus/boringssl.git
[submodule "third_party/cares/cares"]
	path = third_party/cares/cares
	url = https://gitee.com/githubplus/c-ares.git
	branch = cares-1_12_0
[submodule "third_party/bloaty"]
	path = third_party/bloaty
	url = https://gitee.com/githubplus/bloaty.git
[submodule "third_party/abseil-cpp"]
	path = third_party/abseil-cpp
	url = https://gitee.com/githubplus/abseil-cpp
[submodule "third_party/libcxxabi"]
	path = third_party/libcxxabi
	url = https://gitee.com/githubplus/libcxxabi.git
	branch = release_60
[submodule "third_party/libcxx"]
	path = third_party/libcxx
	url = https://gitee.com/githubplus/libcxx.git
	branch = release_60
[submodule "third_party/data-plane-api"]
	path = third_party/data-plane-api
	url = https://gitee.com/githubplus/data-plane-api.git
[submodule "third_party/googleapis"]
	path = third_party/googleapis
	url = https://gitee.com/githubplus/googleapis.git
[submodule "third_party/protoc-gen-validate"]
	path = third_party/protoc-gen-validate
	url = https://gitee.com/githubplus/protoc-gen-validate.git
[submodule "third_party/upb"]
	path = third_party/upb
	url = https://gitee.com/githubplus/upb.git

gRPC的submodule就更新完成了,然后进到protobuf,同样修改protobuf的.gitmodules,再更新其submodule:

cd third_party/protobuf
vim .gitmodules

// replace content in .gitmodules
[submodule "third_party/benchmark"]
	path = third_party/benchmark
	url = https://gitee.com/githubplus/benchmark.git
[submodule "third_party/googletest"]
	path = third_party/googletest
	url = https://gitee.com/githubplus/googletest.git
	ignore = dirty

All done for source code!

使用Android源码自带的编译工具编译gRPC

安装依赖:

sudo apt-get install pkg-config
sudo apt-get install autoconf automake libtool make g++ unzip
sudo apt-get install libgflags-dev libgtest-dev

先编译x86的gRPC,在Ubuntu系统中直接编译:

sudo make
sudo make install

安装protobuf

cd third_party/protobuf/
./autogen.sh
./configure
sudo make
sudo make install

测试gRPC,编译运行Hello world

cd examples/cpp/helloworld/
make

开启一个服务

./greeter_server 

在另一个terminal

./greeter_client 

然后编译arm64的gRPC,使用Android源码中的交叉编译工具:

sudo make GRPC_CROSS_COMPILE=true GRPC_CROSS_AROPTS="cr --target=elf64-little" HAS_PKG_CONFIG=false HAS_EMBEDDED_PROTOBUF=true CC=aosp/prebuilts/gcc/linux-x86/arm/toolchain/bin/arm-linux-androideabi-gcc CXX=aosp/prebuilts/gcc/linux-x86/arm/toolchain/bin/arm-linux-androideabi-g++ RANLIB=aosp/prebuilts/gcc/linux-x86/arm/toolchain/bin/aarch64-linux-gnu-gcc-ranlib LD=aosp/prebuilts/gcc/linux-x86/arm/toolchain/bin/arm-linux-androideabi-gcc LDXX=aosp/prebuilts/gcc/linux-x86/arm/toolchain/bin/arm-linux-androideabi-g++ AR=aosp/prebuilts/gcc/linux-x86/arm/toolchain/bin/arm-linux-androideabi-ar STRIP=aosp/prebuilts/gcc/linux-x86/arm/toolchain/bin/arm-linux-androideabi-strip PROTOBUF_CONFIG_OPTS="--host=arm-linux-androideabi --with-protoc=/usr/local/protobuf/bin/protoc"

在libs/opt目录下会有生成的静态库和动态库,可使用libgrpc.a&libgrpc++.a&libgrpc++_reflection.a&protobuf/libprotobuf.a这4个静态库。

使用gRPC

直接修改examples/cpp/helloworld/的MakeFile测试一下:

CXX = /aosp/prebuilts/gcc/linux-x86/arm/toolchain/bin/arm-linux-androideabi-g++
CXXFLAGS += -std=c++11 -I/usr/local/protobuf/include -I/usr/local/include -fPIC
LDFLAGS += -L. -lprotobuf -lgrpc++ -lgrpc -lglog\
           -Wl,--no-as-needed -lgrpc++_reflection -Wl,--as-needed\
           -ldl -lpthread -static

make一下,push到Android系统的/system/bin运行起来。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用Go语言编写一个gRPC通信时,需要以下几个步骤: 1. 定义gRPC服务接口和消息类型:首先,需要在`.proto`文件中定义gRPC服务接口和消息类型。该文件描述了服务和消息的结构以及方法的签名。例如,可以定义一个`HelloService`服务,其中包含一个`SayHello`方法,用于接收一个名字并返回问候语。 2. 使用Protocol Buffers生成Go代码:将`.proto`文件编译为Go代码,使用以下命令生成所需的代码: ``` protoc --go_out=. hello.proto ``` 这将在当前目录下生成用于gRPC通信的Go代码。 3. 实现gRPC服务接口:在生成的Go代码中,实现gRPC服务接口。具体来说,实现`HelloServiceServer`接口,并提供`SayHello`方法的具体实现逻辑。 4. 启动gRPC服务器:在Go代码中启动gRPC服务器,监听指定的端口,并注册实现了服务接口的对象。这样,服务器就可以接收来自客户端的请求,并调用相应的方法进行处理。 5. 编写客户端代码:编写一个独立的Go程序作为gRPC客户端,连接到服务器,并调用相应的方法发送请求和处理响应。 下面是一个简单的示例: ```go // hello.proto syntax = "proto3"; package helloworld; service HelloService { rpc SayHello (HelloRequest) returns (HelloResponse); } message HelloRequest { string name = 1; } message HelloResponse { string message = 1; } ``` ```go // server.go package main import ( "context" "fmt" "log" "net" "google.golang.org/grpc" pb "path/to/helloworld" // 导入生成的Go代码 ) type helloServer struct{} func (s *helloServer) SayHello(ctx context.Context, req *pb.HelloRequest) (*pb.HelloResponse, error) { name := req.GetName() message := fmt.Sprintf("Hello, %s!", name) return &pb.HelloResponse{Message: message}, nil } func main() { lis, err := net.Listen("tcp", ":50051") if err != nil { log.Fatalf("failed to listen: %v", err) } grpcServer := grpc.NewServer() pb.RegisterHelloServiceServer(grpcServer, &helloServer{}) log.Println("Starting gRPC server on port 50051...") if err := grpcServer.Serve(lis); err != nil { log.Fatalf("failed to serve: %v", err) } } ``` ```go // client.go package main import ( "context" "log" "google.golang.org/grpc" pb "path/to/helloworld" // 导入生成的Go代码 ) func main() { conn, err := grpc.Dial(":50051", grpc.WithInsecure()) if err != nil { log.Fatalf("failed to connect: %v", err) } defer conn.Close() client := pb.NewHelloServiceClient(conn) req := &pb.HelloRequest{Name: "Alice"} res, err := client.SayHello(context.Background(), req) if err != nil { log.Fatalf("failed to call SayHello: %v", err) } log.Println(res.GetMessage()) } ``` 上述示例演示了一个简单的gRPC通信,包括定义了一个`HelloService`服务,实现了`SayHello`方法,并在服务器和客户端进行了相应的实现。你可以根据实际需求,修改和扩展这个示例。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值