感觉不把flag立起来就一直不能静下心来动手写代码,先立起来,看看啥时候能搞定:
- 先写一个brpc的服务,学一学brpc怎么玩的,最简单的实现一个http服务(get、post)
- 再写一个braft应用,在多个节点上实现一个分布式的id查询和自增服务
2022-11-09
下载brpc代码库,v1.3版本分支,Ubuntu 22.04上完成编译,完成echo_c++ example代码编译和运行。下一步适当修改,改造成一个http的服务。
2022-11-11
简单改了下Echo示例代码,加了个rpc参数,表示消息长度,改成了http协议:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
~/incubator-brpc/example/echo_c++$ git diff diff --git a/example/echo_c++/client.cpp b/example/echo_c++/client.cpp index 337aa6e9..0df24399 100644 --- a/example/echo_c++/client.cpp +++ b/example/echo_c++/client.cpp @@ -23,8 +23,7 @@ #include <brpc/channel.h> #include "echo.pb.h" -DEFINE_string(attachment, "", "Carry this along with requests"); -DEFINE_string(protocol, "baidu_std", "Protocol type. Defined in src/brpc/options.proto"); +DEFINE_string(protocol, "http", "Protocol type. Defined in src/brpc/options.proto"); DEFINE_string(connection_type, "", "Connection type. Available values: single, pooled, short"); DEFINE_string(server, "0.0.0.0:8000", "IP Address of server"); DEFINE_string(load_balancer, "", "The algorithm for load balancing"); @@ -64,12 +63,10 @@ int main(int argc, char* argv[]) { example::EchoResponse response; brpc::Controller cntl; - request.set_message("hello world"); + request.set_message("hello server123"); + request.set_length(strlen("hello server123")); cntl.set_log_id(log_id ++); // set by user - // Set attachment which is wired to network directly instead of - // being serialized into protobuf messages. - cntl.request_attachment().append(FLAGS_attachment); // Because `done'(last parameter) is NULL, this function waits until // the response comes back or error occurs(including timedout). @@ -77,8 +74,8 @@ int main(int argc, char* argv[]) { if (!cntl.Failed()) { LOG(INFO) << "Received response from " << cntl.remote_side() << " to " << cntl.local_side() - << ": " << response.message() << " (attached=" - << cntl.response_attachment() << ")" + << ": " << response.message() << " (length=" + << response.length() << ")" << " latency=" << cntl.latency_us() << "us"; } else { LOG(WARNING) << cntl.ErrorText(); diff --git a/example/echo_c++/echo.proto b/example/echo_c++/echo.proto index 2b39627f..8851b2e2 100644 --- a/example/echo_c++/echo.proto +++ b/example/echo_c++/echo.proto @@ -22,10 +22,12 @@ option cc_generic_services = true; message EchoRequest { required string message = 1; + required int32 length = 2; }; message EchoResponse { required string message = 1; + required int32 length = 2; }; service EchoService { diff --git a/example/echo_c++/server.cpp b/example/echo_c++/server.cpp index 41ffd0b1..549737f8 100644 --- a/example/echo_c++/server.cpp +++ b/example/echo_c++/server.cpp @@ -22,7 +22,6 @@ #include <brpc/server.h> #include "echo.pb.h" -DEFINE_bool(echo_attachment, true, "Echo attachment as well"); DEFINE_int32(port, 8000, "TCP Port of this server"); DEFINE_string(listen_addr, "", "Server listen address, may be IPV4/IPV6/UDS." " If this is set, the flag port will be ignored"); @@ -57,20 +56,11 @@ public: << "] from " << cntl->remote_side() << " to " << cntl->local_side() << ": " << request->message() - << " (attached=" << cntl->request_attachment() << ")"; + << " (length=" << request->length() << ")"; // Fill response. - response->set_message(request->message()); - - // You can compress the response by setting Controller, but be aware - // that compression may be costly, evaluate before turning on. - // cntl->set_response_compress_type(brpc::COMPRESS_TYPE_GZIP); - - if (FLAGS_echo_attachment) { - // Set attachment which is wired to network directly instead of - // being serialized into protobuf messages. - cntl->response_attachment().append(cntl->request_attachment()); - } + response->set_message("hello client"); + response->set_length(strlen("hello client")); } }; } // namespace example |
运行结果:
1 2 3 4 5 6 7 8 9 |
~/incubator-brpc/example/echo_c++$ ./echo_client -server="127.0.0.1:8080" I1111 09:37:14.304421 2015994 client.cpp:75] Received response from 127.0.0.1:8080 to 127.0.0.1:36476: hello client (length=12) latency=460us I1111 09:37:15.305069 2015994 client.cpp:75] Received response from 127.0.0.1:8080 to 127.0.0.1:36476: hello client (length=12) latency=521us ~/incubator-brpc/example/echo_c++$ ./echo_server --port="8080" I1111 09:37:56.076617 2016206 src/brpc/server.cpp:1070] Server[example::EchoServiceImpl] is serving on port=8080. I1111 09:37:56.077233 2016206 src/brpc/server.cpp:1073] Check out http://hih-l-9493:8080 in web browser. I1111 09:37:59.443775 2016217 server.cpp:55] Received request[log_id=7] from 127.0.0.1:60946 to 127.0.0.1:8080: hello server123 (length=15) I1111 09:38:00.444778 2016216 server.cpp:55] Received request[log_id=8] from 127.0.0.1:60946 to 127.0.0.1:8080: hello server123 (length=15) |
curl请求:
1 2 3 4 5 6 7 8 9 10 11 |
/incubator-brpc/example/echo_c++$ curl -X GET -H 'Content-Type: application/json' -d '{"message":"yyy", "length": 3}' localhost:8080/EchoService/Echo {"message":"hello client","length":12} # 或者POST请求也可以 ~/incubator-brpc/example/echo_c++$ curl -X POST -H 'Content-Type: application/json' -d '{"message":"yyy", "length": 31}' localhost:8080/EchoService/Echo {"message":"hello client","length":12} ~/incubator-brpc/example/echo_c++$ ./echo_server --port="8080" I1111 09:38:30.836687 2019460 src/brpc/server.cpp:1070] Server[example::EchoServiceImpl] is serving on port=8080. I1111 09:38:30.837011 2019460 src/brpc/server.cpp:1073] Check out http://hih-l-9493:8080 in web browser. I1111 09:38:52.031097 2019465 server.cpp:55] Received request[log_id=0] from 127.0.0.1:41962 to 127.0.0.1:8080: yyy (length=3) |
HTTP service,读文件内容:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
~/incubator-brpc/example/http_c++$ git diff http_server.cpp diff --git a/example/http_c++/http_server.cpp b/example/http_c++/http_server.cpp index d20ee71a..560496e8 100644 --- a/example/http_c++/http_server.cpp +++ b/example/http_c++/http_server.cpp @@ -22,6 +22,8 @@ #include <brpc/server.h> #include <brpc/restful.h> #include "http.pb.h" +#include <fstream> +#include <string> DEFINE_int32(port, 8010, "TCP Port of this server"); DEFINE_int32(idle_timeout_s, -1, "Connection will be closed if there is no " @@ -105,8 +107,17 @@ public: bthread_t th; bthread_start_background(&th, NULL, SendLargeFile, args.release()); } else { - cntl->response_attachment().append("Getting file: "); - cntl->response_attachment().append(filename); + std::string line; + std::ifstream myfile(filename); + if (myfile.is_open()) { + cntl->response_attachment().append("Getting file: " + filename + "\n"); + while (getline(myfile, line)) { + cntl->response_attachment().append(line + "\n"); + } + myfile.close(); + } else { + cntl->response_attachment().append("No such file"); + } } } }; @@ -175,7 +186,8 @@ int main(int argc, char* argv[]) { return -1; } if (server.AddService(&file_svc, - brpc::SERVER_DOESNT_OWN_SERVICE) != 0) { + brpc::SERVER_DOESNT_OWN_SERVICE, + "/v1/getfile/* => default_method") != 0) { LOG(ERROR) << "Fail to add file_svc"; return -1; } |
运行结果:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
~/incubator-brpc/example/http_c++$ ./http_client "localhost:8010/v1/getfile/http.proto" Getting file: http.proto // Licensed to the Apache Software Foundation (ASF) under one // or more contributor license agreements. See the NOTICE file // distributed with this work for additional information // regarding copyright ownership. The ASF licenses this file // to you under the Apache License, Version 2.0 (the // "License"); you may not use this file except in compliance // with the License. You may obtain a copy of the License at // ...... ~/incubator-brpc/example/http_c++$ curl "localhost:8010/v1/getfile/http.proto" Getting file: http.proto // Licensed to the Apache Software Foundation (ASF) under one // or more contributor license agreements. See the NOTICE file // distributed with this work for additional information // regarding copyright ownership. The ASF licenses this file // to you under the Apache License, Version 2.0 (the // "License"); you may not use this file except in compliance // with the License. You may obtain a copy of the License at // ...... |