You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@brpc.apache.org by GitBox <gi...@apache.org> on 2022/12/26 02:29:30 UTC

[GitHub] [incubator-brpc] 17336 commented on issue #2059: 请问bvar变量的expose接口有什么用呢?

17336 commented on issue #2059:
URL: https://github.com/apache/incubator-brpc/issues/2059#issuecomment-1364823171

   > 使用的多维度bvar?能否给下使用代码例子?
   
   不是,单维度的bvar
   
   ```
   namespace other_metric {
       bvar::Adder<int> time;
       bvar::Window<bvar::Adder<int> > time_minitue("metric", "time", &time, 60);
   }
   void increase_bvar() {
       while(true){
           other_metric::time<<1;
           std::this_thread::sleep_for(std::chrono::seconds(1));
       }
   }
   
   int main(int argc, char* argv[]) {
       // Parse gflags. We recommend you to use gflags as well.
       GFLAGS_NS::ParseCommandLineFlags(&argc, &argv, true);
   
       // Generally you only need one Server.
       brpc::Server server;
   
       // Instance of your service.
       example::EchoServiceImpl echo_service_impl;
   
       // Add the service into server. Notice the second parameter, because the
       // service is put on stack, we don't want server to delete it, otherwise
       // use brpc::SERVER_OWNS_SERVICE.
       if (server.AddService(&echo_service_impl, 
                             brpc::SERVER_DOESNT_OWN_SERVICE) != 0) {
           LOG(ERROR) << "Fail to add service";
           return -1;
       }
   
       butil::EndPoint point;
       if (!FLAGS_listen_addr.empty()) {
           if (butil::str2endpoint(FLAGS_listen_addr.c_str(), &point) < 0) {
               LOG(ERROR) << "Invalid listen address:" << FLAGS_listen_addr;
               return -1;
           }
       } else {
           point = butil::EndPoint(butil::IP_ANY, FLAGS_port);
       }
       // Start the server.
       brpc::ServerOptions options;
       options.idle_timeout_sec = FLAGS_idle_timeout_s;
       if (server.Start(point, &options) != 0) {
           LOG(ERROR) << "Fail to start EchoServer";
           return -1;
       }
       std::thread t(increase_bvar);
       // Wait until Ctrl-C is pressed, then Stop() and Join() the server.
       server.RunUntilAskedToQuit();
       t.join();
       return 0;
   }
   ```


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org
For additional commands, e-mail: dev-help@brpc.apache.org