首先需要按照grpc官网上说的办法从github上下载源码,编译,然后跑一跑对应的测试代码。

在cpp的helloworld例子中,client端,第一个函数是创建channel。

SRE实战 互联网时代守护先锋,助力企业售后服务体系运筹帷幄!一键直达领取阿里云限量特价优惠。
GreeterClient greeter(grpc::CreateChannel("localhost:50051", grpc::InsecureChannelCredentials()));

我们从这里开始分析,CreateChannel这个函数的具体实现在src/cpp/client/create_channel.cc(这个文件里指定了namespace grpc),CreateChannel再调用CreateCustomChannel,这里会根据creds的类型来创建Channel。

std::shared_ptr<Channel> CreateCustomChannel(
    const grpc::string& target,
    const std::shared_ptr<ChannelCredentials>& creds,
    const ChannelArguments& args) {
  GrpcLibraryCodegen init_lib;  // We need to call init in case of a bad creds.
  return creds ? creds->CreateChannel(target, args)
               : CreateChannelInternal(
                     "",
                     grpc_lame_client_channel_create(
                         nullptr, GRPC_STATUS_INVALID_ARGUMENT,
                         "Invalid credentials."),
                     std::vector<std::unique_ptr<
                         experimental::ClientInterceptorFactoryInterface>>());
}
creds的类型为ChannelCredentials,那我们来具体看看ChannelCredentials这个类是如何实现的,ChannelCredentials这个类的定义在include/grpcpp/security/credentials.h文件中,这里定义了虚函数CreateChannel。
virtual std::shared_ptr<Channel> CreateChannel(
      const grpc::string& target, const ChannelArguments& args) = 0;

以ChannelCredentials类为父类的子类有,InsecureChannelCredentialsImpl,CronetChannelCredentialsImpl,SecureChannelCredentials,具体是哪个类,我们需要再看下CreateChannel的第二个参数,

grpc::InsecureChannelCredentials()
InsecureChannelCredentials这个函数的定义和实现在src/cpp/client/insecure_credentials.cc文件中,函数中创建了一个指向ChannelCredentials类的InsecureChannelCredentialsImpl对象,再看InsecureChannelCredentialsImpl类实现的CreateChannel。也就是根据c++的多态用法,在上面提到的CreateCustomChannel函数中,调用的CreateChannel,就是调用InsecureChannelCredentialsImpl这个类的CreateChannel。

未完待续。。。

 

扫码关注我们
微信号:SRE实战
拒绝背锅 运筹帷幄