brpc 内建服务实现

brpc有很多的内建服务,便于生产环境使用,本次分析内建服务基本统一部分,各服务不同数据统计等实现这里暂时不讨论,后续会分析几个具体内部实现。

如果你想看看内建服务只需启动一个demo服务,执行简单的命令:
curl localhost:8003

[host build]$ curl localhost:8003
    __
   / /_  _________  _____
  / __ \/ ___/ __ \/ ___/
 / /_/ / /  / /_/ / /__
/_.___/_/  / .___/\___/
          /_/

github : https://github.com/brpc/brpc

/status : Status of services
/connections : List all connections
/flags : List all gflags
  /flags/port : List the gflag
  /flags/guard_page_size;help* : List multiple gflags with glob patterns (Use $ instead of ? to match single character)
  /flags/NAME?setvalue=VALUE : Change a gflag, validator will be called. User is responsible for thread-safety and consistency issues.
/vars : List all exposed bvars
  /vars/rpc_num_sockets : List the bvar
  /vars/rpc_server*_count;iobuf_blo$k_* : List multiple bvars with glob patterns (Use $ instead of ? to match single character)
/rpcz : Recent RPC calls(disabled)
  /rpcz/stats : Statistics of rpcz
  /rpcz?time=2021/01/22-19:50:38 : RPC calls before the time
  /rpcz?time=2021/01/22-19:50:38&max_scan=10 : N RPC calls at most before the time
  Other filters: min_latency, min_request_size, min_response_size, log_id, error_code
  /rpcz?trace=N : Recent RPC calls whose trace_id is N
  /rpcz?trace=N&span=M : Recent RPC calls whose trace_id is N and span_id is M
/hotspots/cpu : Profiling CPU (disabled)
/hotspots/heap : Profiling heap (disabled)
/hotspots/growth : Profiling growth of heap (disabled)
curl -H 'Content-Type: application/json' -d 'JSON' 172.17.226.15:8003/ServiceName/MethodName : Call method by http+json
/version : Version of this server, set by Server::set_version()
/health : Test healthy
/vlog : List all VLOG callsites
/sockets : Check status of a Socket
/bthreads : Check status of a bthread
/ids : Check status of a bthread_id
/protobufs : List all protobuf services and messages
/list : json signature of methods
/threads : Check pstack (disabled)
/dir : Browse directories and files (disabled)
[lfz@iz2zec6g7egdxs0gpgesoiz build]$ curl localhost:8003/hotspots/cpu
Error: cpu profiler is not enabled.
Read the docs: docs/cn/{cpu_profiler.md,heap_profiler.md}

brpc内建的服务统一使用了rpc请求框架完成服务的搭建工作。
基本统一的default_method来请求需要的功能。

如果你大概了解brpc使用方式,看一下下文内容就明白了。
以下是实现的proto文件一部分。

package brpc;
...
message IndexRequest {}
message IndexResponse {} 
message FlagsRequest {}
message FlagsResponse {} 
message VersionRequest {}
message VersionResponse {}
message HealthRequest {}
message HealthResponse {}
message StatusRequest {}
message StatusResponse {}
message ProtobufsRequest {}
message ProtobufsResponse {}
message ConnectionsRequest {}
message ConnectionsResponse {}
message ListRequest {}
message ListResponse {
    repeated google.protobuf.ServiceDescriptorProto service = 1;
}
message VarsRequest {}
message VarsResponse {}
message BthreadsRequest {}
message BthreadsResponse {}
message IdsRequest {}
message IdsResponse{}
message SocketsRequest {}
message SocketsResponse {}
message RpczRequest {}
message RpczResponse {}
message ThreadsRequest {}
message ThreadsResponse {}
message DirRequest {}
message DirResponse {}
message VLogRequest {}
message VLogResponse {}
message MetricsRequest {}
message MetricsResponse {}
message BadMethodRequest {
    required string service_name = 1;
}
message BadMethodResponse {}

service index {
    rpc default_method(IndexRequest) returns (IndexResponse);
}

service version {
    rpc default_method(VersionRequest) returns (VersionResponse);
}

service health {
    rpc default_method(HealthRequest) returns (HealthResponse);
}

service status {
    rpc default_method(StatusRequest) returns (StatusResponse);
}

service protobufs {
    rpc default_method(ProtobufsRequest) returns (ProtobufsResponse);
}

service connections {
    rpc default_method(ConnectionsRequest) returns (ConnectionsResponse);
}

service list {
    rpc default_method(ListRequest) returns (ListResponse);
}

service threads {
    rpc default_method(ThreadsRequest) returns (ThreadsResponse);
}

service vlog {
    rpc default_method(VLogRequest) returns (VLogResponse);
}

service bthreads {
    rpc default_method(BthreadsRequest) returns (BthreadsResponse);
}

service ids {
    rpc default_method(IdsRequest) returns (IdsResponse);
}

service sockets {
    rpc default_method(SocketsRequest) returns (SocketsResponse);
}

service brpc_metrics {
    rpc default_method(MetricsRequest) returns (MetricsResponse);
}

service badmethod {
    rpc no_method(BadMethodRequest) returns (BadMethodResponse);
}
...
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容