這是一個建立於 的文章,其中的資訊可能已經有所發展或是發生改變。
gRPC概述
3/26/2016 9:16:08 AM
一直在尋找多平台多語言的通訊架構,微軟的WCF架構很強大和靈活,雖然也能通過自訂綁定和其他技術的用戶端通訊,但是始終沒有實現多平台的技術架構的統一。google的gRPC是一個不錯的選擇,相比於類似架構Thrift等,google的解決方案更成熟和通用。不足的是由於剛剛開源,使用範圍有限,國內資料更少。例如僅僅編譯C++的gRPC,花了我兩天的時間。在這個系列中,我會逐一介紹各個語言使用gRPC的細節。
gRPC是google2015年初開源的通訊架構。
使用gRPC可以在用戶端調用不同機器上的服務端的方法,而用戶端和服務端的開發語言和運行環境可以有很多種,基本涵蓋了主流語言和平台。雙方互動的協議可以在proto檔案中定義,用戶端和服務端可以很方便的通過工具產生協議和代理代碼。而訊息的編碼是採用google protocol buffer,資料量小、速度快。
是一個簡單的gRPC使用的情境,服務端採用C++開發,用戶端可以是anroid,和ruby。
gRPC具有以下特點:
- 基於 HTTP/2, 繼而提供了串連多工、Body 和 Header 壓縮等機制。可以節省頻寬、降低TCP連結次數、節省CPU使用和延長電池壽命等。
- 支援主流開發語言(C, C++, Python, PHP, Ruby, NodeJS, C#, Objective-C、Golang、Java)
- IDL (Interface Definition Language) 層使用了 Protocol Buffers, 非常適合團隊的介面設計
下面是C#、C++、java的用戶端和服務端的例子,這些用戶端都向服務端傳遞一個姓名XXX,服務端返回 hello XXX字串。
作為通訊協定的proto檔案
無論使用何種語言建立用戶端和服務端,都依照相同的proto檔案。proto檔案定義了協議介面和資料格式。不同的語言都依照proto檔案產生服務端的介面和服務類、用戶端的服務代理,只是產生工具的命令不同。
proto檔案如下:
syntax = "proto3";option java_multiple_files = true;option java_package = "io.grpc.examples.helloworld";option java_outer_classname = "HelloWorldProto";option objc_class_prefix = "HLW";package helloworld;// The greeting service definition.service Greeter { // Sends a greeting rpc SayHello (HelloRequest) returns (HelloReply) {}}// The request message containing the user's name.message HelloRequest { string name = 1;}// The response message containing the greetingsmessage HelloReply { string message = 1;}
C#服務端和用戶端代碼
C#的用戶端代理是由工具產生的,所以很簡單。
用戶端
public static void Main(string[] args){ Channel channel = new Channel("127.0.0.1:50051", ChannelCredentials.Insecure); var client = Greeter.NewClient(channel); String user = "paul"; var reply = client.SayHello(new HelloRequest { Name = user }); Console.WriteLine("Greeting: " + reply.Message); channel.ShutdownAsync().Wait(); Console.WriteLine("Press any key to exit..."); Console.ReadKey();}
服務端
class GreeterImpl : Greeter.IGreeter{ // Server side handler of the SayHello RPC public Task<HelloReply> SayHello(HelloRequest request, ServerCallContext context) { return Task.FromResult(new HelloReply { Message = "Hello " + request.Name }); }}class Program{ const int Port = 50051; public static void Main(string[] args) { Server server = new Server { Services = { Greeter.BindService(new GreeterImpl()) }, Ports = { new ServerPort("localhost", Port, ServerCredentials.Insecure) } }; server.Start(); Console.WriteLine("Greeter server listening on port " + Port); Console.WriteLine("Press any key to stop the server..."); Console.ReadKey(); server.ShutdownAsync().Wait(); }}
C++的服務端和用戶端代碼
C++的用戶端代理無法通過工具產生,需要手動編寫。
//C++用戶端class GreeterClient { public: GreeterClient(std::shared_ptr<Channel> channel) : stub_(Greeter::NewStub(channel)) {} // Assambles the client's payload, sends it and presents the response back // from the server. std::string SayHello(const std::string& user) { // Data we are sending to the server. HelloRequest request; request.set_name(user); // Container for the data we expect from the server. HelloReply reply; // Context for the client. It could be used to convey extra information to // the server and/or tweak certain RPC behaviors. ClientContext context; // The actual RPC. Status status = stub_->SayHello(&context, request, &reply); // Act upon its status. if (status.ok()) { return reply.message(); } else { return "RPC failed"; } } private: std::unique_ptr<Greeter::Stub> stub_;};int main(int argc, char** argv) { // Instantiate the client. It requires a channel, out of which the actual RPCs // are created. This channel models a connection to an endpoint (in this case, // localhost at port 50051). We indicate that the channel isn't authenticated // (use of InsecureChannelCredentials()). GreeterClient greeter(grpc::CreateChannel( "localhost:50051", grpc::InsecureChannelCredentials())); std::string user("world"); std::string reply = greeter.SayHello(user); std::cout << "Greeter received: " << reply << std::endl; return 0;}//C++服務端// Logic and data behind the server's behavior.class GreeterServiceImpl final : public Greeter::Service { Status SayHello(ServerContext* context, const HelloRequest* request, HelloReply* reply) override { std::string prefix("Hello "); reply->set_message(prefix + request->name()); return Status::OK; }};void RunServer() { std::string server_address("0.0.0.0:50051"); GreeterServiceImpl service; ServerBuilder builder; // Listen on the given address without any authentication mechanism. builder.AddListeningPort(server_address, grpc::InsecureServerCredentials()); // Register "service" as the instance through which we'll communicate with // clients. In this case it corresponds to an *synchronous* service. builder.RegisterService(&service); // Finally assemble the server. std::unique_ptr<Server> server(builder.BuildAndStart()); std::cout << "Server listening on " << server_address << std::endl; // Wait for the server to shutdown. Note that some other thread must be // responsible for shutting down the server for this call to ever return. server->Wait();}int main(int argc, char** argv) { RunServer(); return 0;}
java的服務端和用戶端代碼
gRPC本身不支援java語言,但是使用java實現了gRPC,架構是gRPC-java。使用gRPC-java一樣可以與gRPC的用戶端和服務端進行互動。
java用戶端代碼:
和C++一樣,java的用戶端仍然不能直接自動產生服務代理類,需要手動建立。
package io.grpc.examples.helloworld;import io.grpc.ManagedChannel;import io.grpc.ManagedChannelBuilder;import io.grpc.StatusRuntimeException;import java.util.concurrent.TimeUnit;import java.util.logging.Level;import java.util.logging.Logger;/** * A simple client that requests a greeting from the {@link HelloWorldServer}. */public class HelloWorldClient { private static final Logger logger = Logger.getLogger(HelloWorldClient.class.getName()); private final ManagedChannel channel; private final GreeterGrpc.GreeterBlockingStub blockingStub; /** Construct client connecting to HelloWorld server at {@code host:port}. */ public HelloWorldClient(String host, int port) { channel = ManagedChannelBuilder.forAddress(host, port) .usePlaintext(true) .build(); blockingStub = GreeterGrpc.newBlockingStub(channel); } public void shutdown() throws InterruptedException { channel.shutdown().awaitTermination(5, TimeUnit.SECONDS); } /** Say hello to server. */ public void greet(String name) { logger.info("Will try to greet " + name + " ..."); HelloRequest request = HelloRequest.newBuilder().setName(name).build(); HelloReply response; try { response = blockingStub.sayHello(request); } catch (StatusRuntimeException e) { logger.log(Level.WARNING, "RPC failed: {0}", e.getStatus()); return; } logger.info("Greeting: " + response.getMessage()); } /** * Greet server. If provided, the first element of {@code args} is the name to use in the * greeting. */ public static void main(String[] args) throws Exception { HelloWorldClient client = new HelloWorldClient("localhost", 50051); try { /* Access a service running on the local machine on port 50051 */ String user = "world"; if (args.length > 0) { user = args[0]; /* Use the arg as the name to greet if provided */ } client.greet(user); } finally { client.shutdown(); } }}
java服務端代碼:
package io.grpc.examples.helloworld;import io.grpc.Server;import io.grpc.ServerBuilder;import io.grpc.stub.StreamObserver;import java.io.IOException;import java.util.logging.Logger;/** * Server that manages startup/shutdown of a {@code Greeter} server. */public class HelloWorldServer { private static final Logger logger = Logger.getLogger(HelloWorldServer.class.getName()); /* The port on which the server should run */ private int port = 50051; private Server server; private void start() throws IOException { server = ServerBuilder.forPort(port) .addService(GreeterGrpc.bindService(new GreeterImpl())) .build() .start(); logger.info("Server started, listening on " + port); Runtime.getRuntime().addShutdownHook(new Thread() { @Override public void run() { // Use stderr here since the logger may have been reset by its JVM shutdown hook. System.err.println("*** shutting down gRPC server since JVM is shutting down"); HelloWorldServer.this.stop(); System.err.println("*** server shut down"); } }); } private void stop() { if (server != null) { server.shutdown(); } } /** * Await termination on the main thread since the grpc library uses daemon threads. */ private void blockUntilShutdown() throws InterruptedException { if (server != null) { server.awaitTermination(); } } /** * Main launches the server from the command line. */ public static void main(String[] args) throws IOException, InterruptedException { final HelloWorldServer server = new HelloWorldServer(); server.start(); server.blockUntilShutdown(); } private class GreeterImpl implements GreeterGrpc.Greeter { @Override public void sayHello(HelloRequest req, StreamObserver<HelloReply> responseObserver) { HelloReply reply = HelloReply.newBuilder().setMessage("Hello " + req.getName()).build(); responseObserver.onNext(reply); responseObserver.onCompleted(); } }}