• 使用 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内容

    1. syntax = "proto3";
    2. option java_multiple_files = true;
    3. option java_package = "com.geostar";
    4. message RequestGeo {
    5. string param = 1;
    6. }
    7. message ResponseGeo {
    8. string message = 1;
    9. }
    10. service GeostarService {
    11. rpc sayHello (RequestGeo) returns (ResponseGeo) {
    12. }
    13. }

     具体实现代码如下:

    1. // grpcclient.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
    2. //
    3. #include
    4. #include
    5. #include
    6. #include
    7. #include
    8. #include "geo_service.grpc.pb.h"
    9. #include "geo_service.pb.h"
    10. using grpc::Channel;
    11. using grpc::ClientContext;
    12. using grpc::Status;
    13. #pragma region
    14. //#pragma comment(lib, "absl_bad_any_cast_impl.lib")
    15. #pragma comment(lib, "absl_bad_optional_access.lib")
    16. #pragma comment(lib, "absl_bad_variant_access.lib")
    17. #pragma comment(lib, "absl_base.lib")
    18. #pragma comment(lib, "absl_city.lib")
    19. #pragma comment(lib, "absl_civil_time.lib")
    20. #pragma comment(lib, "absl_cord.lib")
    21. #pragma comment(lib, "absl_cordz_functions.lib")
    22. #pragma comment(lib, "absl_cordz_handle.lib")
    23. #pragma comment(lib, "absl_cordz_info.lib")
    24. #pragma comment(lib, "absl_cordz_sample_token.lib")
    25. #pragma comment(lib, "absl_cord_internal.lib")
    26. #pragma comment(lib, "absl_debugging_internal.lib")
    27. #pragma comment(lib, "absl_demangle_internal.lib")
    28. #pragma comment(lib, "absl_examine_stack.lib")
    29. #pragma comment(lib, "absl_exponential_biased.lib")
    30. #pragma comment(lib, "absl_failure_signal_handler.lib")
    31. #pragma comment(lib, "absl_flags.lib")
    32. #pragma comment(lib, "absl_flags_commandlineflag.lib")
    33. #pragma comment(lib, "absl_flags_commandlineflag_internal.lib")
    34. #pragma comment(lib, "absl_flags_config.lib")
    35. #pragma comment(lib, "absl_flags_internal.lib")
    36. #pragma comment(lib, "absl_flags_marshalling.lib")
    37. #pragma comment(lib, "absl_flags_parse.lib")
    38. #pragma comment(lib, "absl_flags_private_handle_accessor.lib")
    39. #pragma comment(lib, "absl_flags_program_name.lib")
    40. #pragma comment(lib, "absl_flags_reflection.lib")
    41. #pragma comment(lib, "absl_flags_usage.lib")
    42. #pragma comment(lib, "absl_flags_usage_internal.lib")
    43. #pragma comment(lib, "absl_graphcycles_internal.lib")
    44. #pragma comment(lib, "absl_hash.lib")
    45. #pragma comment(lib, "absl_hashtablez_sampler.lib")
    46. #pragma comment(lib, "absl_int128.lib")
    47. #pragma comment(lib, "absl_leak_check.lib")
    48. #pragma comment(lib, "absl_leak_check_disable.lib")
    49. #pragma comment(lib, "absl_log_severity.lib")
    50. #pragma comment(lib, "absl_low_level_hash.lib")
    51. #pragma comment(lib, "absl_malloc_internal.lib")
    52. #pragma comment(lib, "absl_periodic_sampler.lib")
    53. #pragma comment(lib, "absl_random_distributions.lib")
    54. #pragma comment(lib, "absl_random_internal_distribution_test_util.lib")
    55. #pragma comment(lib, "absl_random_internal_platform.lib")
    56. #pragma comment(lib, "absl_random_internal_pool_urbg.lib")
    57. #pragma comment(lib, "absl_random_internal_randen.lib")
    58. #pragma comment(lib, "absl_random_internal_randen_hwaes.lib")
    59. #pragma comment(lib, "absl_random_internal_randen_hwaes_impl.lib")
    60. #pragma comment(lib, "absl_random_internal_randen_slow.lib")
    61. #pragma comment(lib, "absl_random_internal_seed_material.lib")
    62. #pragma comment(lib, "absl_random_seed_gen_exception.lib")
    63. #pragma comment(lib, "absl_random_seed_sequences.lib")
    64. #pragma comment(lib, "absl_raw_hash_set.lib")
    65. #pragma comment(lib, "absl_raw_logging_internal.lib")
    66. #pragma comment(lib, "absl_scoped_set_env.lib")
    67. #pragma comment(lib, "absl_spinlock_wait.lib")
    68. #pragma comment(lib, "absl_stacktrace.lib")
    69. #pragma comment(lib, "absl_status.lib")
    70. #pragma comment(lib, "absl_statusor.lib")
    71. #pragma comment(lib, "absl_strerror.lib")
    72. #pragma comment(lib, "absl_strings.lib")
    73. #pragma comment(lib, "absl_strings_internal.lib")
    74. #pragma comment(lib, "absl_str_format_internal.lib")
    75. #pragma comment(lib, "absl_symbolize.lib")
    76. #pragma comment(lib, "absl_synchronization.lib")
    77. #pragma comment(lib, "absl_throw_delegate.lib")
    78. #pragma comment(lib, "absl_time.lib")
    79. #pragma comment(lib, "absl_time_zone.lib")
    80. #pragma comment(lib, "address_sorting.lib")
    81. #pragma comment(lib, "cares.lib")
    82. #pragma comment(lib, "crypto.lib")
    83. #pragma comment(lib, "gpr.lib")
    84. #pragma comment(lib, "grpc++.lib")
    85. #pragma comment(lib, "grpc++_alts.lib")
    86. #pragma comment(lib, "grpc++_error_details.lib")
    87. #pragma comment(lib, "grpc++_reflection.lib")
    88. #pragma comment(lib, "grpc++_unsecure.lib")
    89. #pragma comment(lib, "grpc.lib")
    90. #pragma comment(lib, "grpcpp_channelz.lib")
    91. #pragma comment(lib, "grpc_plugin_support.lib")
    92. #pragma comment(lib, "grpc_unsecure.lib")
    93. #ifdef _DEBUG
    94. //#pragma comment(lib, "libprotobuf-lited.lib")
    95. #pragma comment(lib, "libprotobufd.lib")
    96. //#pragma comment(lib, "libprotocd.lib")
    97. #pragma comment(lib, "zlibstaticd.lib")
    98. #else
    99. //#pragma comment(lib, "libprotobuf-lite.lib")
    100. #pragma comment(lib, "libprotobuf.lib")
    101. //#pragma comment(lib, "libprotoc.lib")
    102. #pragma comment(lib, "zlibstatic.lib")
    103. #endif // _DEBUG
    104. #pragma comment(lib, "re2.lib")
    105. #pragma comment(lib, "ssl.lib")
    106. #pragma comment(lib, "upb.lib")
    107. class GsGRPCClient {
    108. public:
    109. GsGRPCClient(std::shared_ptr channel)
    110. : stub_(GeostarService::NewStub(channel)) {}
    111. ResponseGeo GetMsg(const std::string& param) {
    112. RequestGeo request;
    113. request.set_param(param);
    114. ResponseGeo reply;
    115. ClientContext context;
    116. Status status = stub_->sayHello(&context, request, &reply);
    117. if (status.ok()) {
    118. return reply;
    119. }
    120. else {
    121. std::cout << status.error_code() << ": " << status.error_message()
    122. << std::endl;
    123. return reply;
    124. }
    125. }
    126. private:
    127. std::unique_ptr stub_;
    128. };
    129. int main(int argc, char** argv) {
    130. GsGRPCClient z_msg(grpc::CreateChannel(
    131. "192.168.36.67:8090", grpc::InsecureChannelCredentials()));
    132. std::string query("OID>=1 and OID<=102");
    133. ResponseGeo reply = z_msg.GetMsg(query);
    134. std::cout << reply.message() << std::endl;
    135. return 0;
    136. }

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

  • 相关阅读:
    数据库中的存储过程、游标、触发器与常用的内置函数
    Golang 程序启动原理详解
    使用python为表格填充颜色
    【面试】class文件里面是什么?
    opencv-python图像处理:阈值,滤波,腐蚀,膨胀,梯度
    再见了青春,联想Y450最后一次升级,真的神一般存在。
    【论文笔记】LLM-Augmenter
    卷麻了,00后测试用例写的比我还好,简直无地自容。。。
    《痞子衡嵌入式半月刊》 第 60 期
    33.Tornado_wtform数据验证
  • 原文地址:https://blog.csdn.net/chijingjing/article/details/126031461