使用 GRPC C++ API连接Java的RPC服务

编译c++ grpc  

失败经历教训: 不要手动下载依赖库, 因为依赖库巨大

失败经历如下:

grpc  C++  Windows 手动编译

下载grpc包 https://github.com/grpc/grpc  
下载absl abseil-cpp-20220623.0.zip
c-ares-cares-1_18_1.zip
zlib1211.tar.gz
boringssl-master.zip
protobuf-cpp-3.21.2.zip

下载nasm   https://www.nasm.us/pub/nasm/releasebuilds/2.15.05/win64/
安装后  将路径C:\Users\chijing\AppData\Local\bin\NASM 设置到path系统环境遍历


此方法失败还有一堆库要下载,  下载列表

使用官方方法成功:

> git clone -b v1.47.1 https://github.com/grpc/grpc
> cd grpc
> git submodule update --init


这个过程会下载以下库:
[submodule "third_party/zlib"]
    path = third_party/zlib
    url = https://github.com/madler/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://github.com/protocolbuffers/protobuf.git
[submodule "third_party/googletest"]
    path = third_party/googletest
    url = https://github.com/google/googletest.git
[submodule "third_party/benchmark"]
    path = third_party/benchmark
    url = https://github.com/google/benchmark
[submodule "third_party/boringssl-with-bazel"]
    path = third_party/boringssl-with-bazel
    url = https://github.com/google/boringssl.git
[submodule "third_party/re2"]
    path = third_party/re2
    url = https://github.com/google/re2.git
[submodule "third_party/cares/cares"]
    path = third_party/cares/cares
    url = https://github.com/c-ares/c-ares.git
    branch = cares-1_17_2
[submodule "third_party/bloaty"]
    path = third_party/bloaty
    url = https://github.com/google/bloaty.git
[submodule "third_party/abseil-cpp"]
    path = third_party/abseil-cpp
    url = https://github.com/abseil/abseil-cpp.git
    branch = lts_2020_02_25
[submodule "third_party/envoy-api"]
    path = third_party/envoy-api
    url = https://github.com/envoyproxy/data-plane-api.git
[submodule "third_party/googleapis"]
    path = third_party/googleapis
    url = https://github.com/googleapis/googleapis.git
[submodule "third_party/protoc-gen-validate"]
    path = third_party/protoc-gen-validate
    url = https://github.com/envoyproxy/protoc-gen-validate.git
[submodule "third_party/libuv"]
    path = third_party/libuv
    url = https://github.com/libuv/libuv.git
[submodule "third_party/opencensus-proto"]
    path = third_party/opencensus-proto
    url = https://github.com/census-instrumentation/opencensus-proto.git
[submodule "third_party/opentelemetry"]
    path = third_party/opentelemetry
    url = https://github.com/open-telemetry/opentelemetry-proto.git
[submodule "third_party/xds"]
    path = third_party/xds
    url = https://github.com/cncf/xds.git


windwos make(默认32位需要加64位参数):

> @rem Run from grpc directory after cloning the repo with --recursive or updating submodules.
> md .build
> cd .build
> cmake .. -G "Visual Studio 14 2015" -DgRPC_INSTALL=ON -DPLATFORM=x64           
> cmake --build . --config Release

用vs打开grpc.sln: 直接编译即可

默认生成到C盘路径:

编写代码连接rpc服务

proto文件生成

在安装grpc目录中找到protoc.exe, 通过以下命令生成4个文件
.\protoc.exe -I . --grpc_out=. --plugin=protoc-gen-grpc=grpc_cpp_plugin.exe geo_service.proto
.\protoc.exe -I . --cpp_out=. geo_service.proto

 

 geo_service.proto内容

syntax = "proto3";
option java_multiple_files = true;
option java_package = "com.geostar";

message RequestGeo {
  string param = 1;
}

message ResponseGeo {
  string message = 1;
}
service GeostarService {

  rpc sayHello (RequestGeo) returns (ResponseGeo) {
  }
}

 具体实现代码如下:

// grpcclient.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//

#include <iostream>
#include <iostream>
#include <memory>
#include <string>
#include <grpcpp/grpcpp.h>
#include "geo_service.grpc.pb.h"
#include "geo_service.pb.h"

using grpc::Channel;
using grpc::ClientContext;
using grpc::Status;



#pragma  region
//#pragma comment(lib, "absl_bad_any_cast_impl.lib")
#pragma comment(lib, "absl_bad_optional_access.lib")
#pragma comment(lib, "absl_bad_variant_access.lib")
#pragma comment(lib, "absl_base.lib")
#pragma comment(lib, "absl_city.lib")
#pragma comment(lib, "absl_civil_time.lib")
#pragma comment(lib, "absl_cord.lib")
#pragma comment(lib, "absl_cordz_functions.lib")
#pragma comment(lib, "absl_cordz_handle.lib")
#pragma comment(lib, "absl_cordz_info.lib")
#pragma comment(lib, "absl_cordz_sample_token.lib")
#pragma comment(lib, "absl_cord_internal.lib")
#pragma comment(lib, "absl_debugging_internal.lib")
#pragma comment(lib, "absl_demangle_internal.lib")
#pragma comment(lib, "absl_examine_stack.lib")
#pragma comment(lib, "absl_exponential_biased.lib")
#pragma comment(lib, "absl_failure_signal_handler.lib")
#pragma comment(lib, "absl_flags.lib")
#pragma comment(lib, "absl_flags_commandlineflag.lib")
#pragma comment(lib, "absl_flags_commandlineflag_internal.lib")
#pragma comment(lib, "absl_flags_config.lib")
#pragma comment(lib, "absl_flags_internal.lib")
#pragma comment(lib, "absl_flags_marshalling.lib")
#pragma comment(lib, "absl_flags_parse.lib")
#pragma comment(lib, "absl_flags_private_handle_accessor.lib")
#pragma comment(lib, "absl_flags_program_name.lib")
#pragma comment(lib, "absl_flags_reflection.lib")
#pragma comment(lib, "absl_flags_usage.lib")
#pragma comment(lib, "absl_flags_usage_internal.lib")
#pragma comment(lib, "absl_graphcycles_internal.lib")
#pragma comment(lib, "absl_hash.lib")
#pragma comment(lib, "absl_hashtablez_sampler.lib")
#pragma comment(lib, "absl_int128.lib")
#pragma comment(lib, "absl_leak_check.lib")
#pragma comment(lib, "absl_leak_check_disable.lib")
#pragma comment(lib, "absl_log_severity.lib")
#pragma comment(lib, "absl_low_level_hash.lib")
#pragma comment(lib, "absl_malloc_internal.lib")
#pragma comment(lib, "absl_periodic_sampler.lib")
#pragma comment(lib, "absl_random_distributions.lib")
#pragma comment(lib, "absl_random_internal_distribution_test_util.lib")
#pragma comment(lib, "absl_random_internal_platform.lib")
#pragma comment(lib, "absl_random_internal_pool_urbg.lib")
#pragma comment(lib, "absl_random_internal_randen.lib")
#pragma comment(lib, "absl_random_internal_randen_hwaes.lib")
#pragma comment(lib, "absl_random_internal_randen_hwaes_impl.lib")
#pragma comment(lib, "absl_random_internal_randen_slow.lib")
#pragma comment(lib, "absl_random_internal_seed_material.lib")
#pragma comment(lib, "absl_random_seed_gen_exception.lib")
#pragma comment(lib, "absl_random_seed_sequences.lib")
#pragma comment(lib, "absl_raw_hash_set.lib")
#pragma comment(lib, "absl_raw_logging_internal.lib")
#pragma comment(lib, "absl_scoped_set_env.lib")
#pragma comment(lib, "absl_spinlock_wait.lib")
#pragma comment(lib, "absl_stacktrace.lib")
#pragma comment(lib, "absl_status.lib")
#pragma comment(lib, "absl_statusor.lib")
#pragma comment(lib, "absl_strerror.lib")
#pragma comment(lib, "absl_strings.lib")
#pragma comment(lib, "absl_strings_internal.lib")
#pragma comment(lib, "absl_str_format_internal.lib")
#pragma comment(lib, "absl_symbolize.lib")
#pragma comment(lib, "absl_synchronization.lib")
#pragma comment(lib, "absl_throw_delegate.lib")
#pragma comment(lib, "absl_time.lib")
#pragma comment(lib, "absl_time_zone.lib")
#pragma comment(lib, "address_sorting.lib")
#pragma comment(lib, "cares.lib")
#pragma comment(lib, "crypto.lib")
#pragma comment(lib, "gpr.lib")
#pragma comment(lib, "grpc++.lib")
#pragma comment(lib, "grpc++_alts.lib")
#pragma comment(lib, "grpc++_error_details.lib")
#pragma comment(lib, "grpc++_reflection.lib")
#pragma comment(lib, "grpc++_unsecure.lib")
#pragma comment(lib, "grpc.lib")
#pragma comment(lib, "grpcpp_channelz.lib")
#pragma comment(lib, "grpc_plugin_support.lib")
#pragma comment(lib, "grpc_unsecure.lib")
#ifdef _DEBUG
//#pragma comment(lib, "libprotobuf-lited.lib")
#pragma comment(lib, "libprotobufd.lib")
//#pragma comment(lib, "libprotocd.lib")
#pragma comment(lib, "zlibstaticd.lib")
#else
//#pragma comment(lib, "libprotobuf-lite.lib")
#pragma comment(lib, "libprotobuf.lib")
//#pragma comment(lib, "libprotoc.lib")
#pragma comment(lib, "zlibstatic.lib")
#endif // _DEBUG

#pragma comment(lib, "re2.lib")
#pragma comment(lib, "ssl.lib")
#pragma comment(lib, "upb.lib")

class GsGRPCClient {
public:
	GsGRPCClient(std::shared_ptr<Channel> channel)
		: stub_(GeostarService::NewStub(channel)) {}

	ResponseGeo GetMsg(const std::string& param) {
		RequestGeo request;
		request.set_param(param);


		ResponseGeo reply;

		ClientContext context;


		Status status = stub_->sayHello(&context, request, &reply);


		if (status.ok()) {
			return reply;  
		}
		else {
			std::cout << status.error_code() << ": " << status.error_message()
				<< std::endl;
			return reply;
		}
	}

private:
	std::unique_ptr<GeostarService::Stub> stub_;
};

int main(int argc, char** argv) {
	GsGRPCClient z_msg(grpc::CreateChannel(
		"192.168.36.67:8090", grpc::InsecureChannelCredentials()));
	std::string query("OID>=1 and OID<=102");
	ResponseGeo reply = z_msg.GetMsg(query);
	std::cout << reply.message() << std::endl;
	return 0;
}

rpc组件较多暂时不上传到资源

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值