酷播亮新聞
最棒的知識補給站

ngrinder 壓測grpc協議方案

Before you begin

Prerequisites

Download the example

You』ll need a local copy of the example code to work through this quickstart. Download the example code from our Github repository (the following command clones the entire repository, but you just need the examples for this quickstart and other tutorials):

  1. $ # Clone the repository at the latest release to get the example code:
  2. $ git clone -b v1.13.1 https://github.com/grpc/grpc-java
  3. $ # Navigate to the Java examples:
  4. $ cd grpc-java/examples

Run a gRPC application

From the examples directory:

  1. Compile the client and server
  2. $ ./gradlew installDist
  3. Run the server
  4. $ ./build/install/examples/bin/hello-world-server
  5. In another terminal, run the client
  6. $ ./build/install/examples/bin/hello-world-client

Congratulations! You』ve just run a client-server application with gRPC.

Update a gRPC service

Now let』s look at how to update the application with an extra method on the server for the client to call. Our gRPC service is defined using protocol buffers; you can find out lots more about how to define a service in a .proto file in gRPC Basics: Java. For now all you need to know is that both the server and the client 「stub」 have a SayHello RPC method that takes a HelloRequest parameter from the client and returns a HelloReply from the server, and that this method is defined like this:

  1. // The greeting service definition.
  2. service Greeter {
  3. // Sends a greeting
  4. rpc SayHello (HelloRequest) returns (HelloReply) {}
  5. }
  6. // The request message containing the user”s name.
  7. message HelloRequest {
  8. string name = 1;
  9. }
  10. // The response message containing the greetings
  11. message HelloReply {
  12. string message = 1;
  13. }

Let』s update this so that the Greeter service has two methods. Editsrc/main/proto/helloworld.proto and update it with a new SayHelloAgain method, with the same request and response types:

  1. // The greeting service definition.
  2. service Greeter {
  3. // Sends a greeting
  4. rpc SayHello (HelloRequest) returns (HelloReply) {}
  5. // Sends another greeting
  6. rpc SayHelloAgain (HelloRequest) returns (HelloReply) {}
  7. }
  8. // The request message containing the user”s name.
  9. message HelloRequest {
  10. string name = 1;
  11. }
  12. // The response message containing the greetings
  13. message HelloReply {
  14. string message = 1;
  15. }

(Don』t forget to save the file!)

Update and run the application

When we recompile the example, normal compilation will regenerate GreeterGrpc.java, which contains our generated gRPC client and server classes. This also regenerates classes for populating, serializing, and retrieving our request and response types.

However, we still need to implement and call the new method in the human-written parts of our example application.

Update the server

In the same directory, opensrc/main/java/io/grpc/examples/helloworld/HelloWorldServer.java. Implement the new method like this:

  1. private class GreeterImpl extends GreeterGrpc.GreeterImplBase {
  2. @Override
  3. public void sayHello(HelloRequest req, StreamObserver responseObserver) {
  4. HelloReply reply = HelloReply.newBuilder().setMessage(“Hello ” + req.getName()).build();
  5. responseObserver.onNext(reply);
  6. responseObserver.onCompleted();
  7. }
  8. @Override
  9. public void sayHelloAgain(HelloRequest req, StreamObserver responseObserver) {
  10. HelloReply reply = HelloReply.newBuilder().setMessage(“Hello again ” + req.getName()).build();
  11. responseObserver.onNext(reply);
  12. responseObserver.onCompleted();
  13. }
  14. }

Update the client

In the same directory, opensrc/main/java/io/grpc/examples/helloworld/HelloWorldClient.java. Call the new method like this:

  1. public void greet(String name) {
  2. logger.info(“Will try to greet ” + name + ” …”);
  3. HelloRequest request = HelloRequest.newBuilder().setName(name).build();
  4. HelloReply response;
  5. try {
  6. response = blockingStub.sayHello(request);
  7. } catch (StatusRuntimeException e) {
  8. logger.log(Level.WARNING, “RPC failed: {0}”, e.getStatus());
  9. return;
  10. }
  11. logger.info(“Greeting: ” + response.getMessage());
  12. try {
  13. response = blockingStub.sayHelloAgain(request);
  14. } catch (StatusRuntimeException e) {
  15. logger.log(Level.WARNING, “RPC failed: {0}”, e.getStatus());
  16. return;
  17. }
  18. logger.info(“Greeting: ” + response.getMessage());
  19. }

Run!

Just like we did before, from the examples directory:

  1. Compile the client and server
  2. $ ./gradlew installDist
  3. Run the server
  4. $ ./build/install/examples/bin/hello-world-server
  5. In another terminal, run the client
  6. $ ./build/install/examples/bin/hello-world-client

2.編寫grpc-client的代碼

根據定義的grpc的proto的文件,自動回生成代碼,主要是grpc的基本包。裡面包括grpc,stub,service服務等;

ngrinder 壓測grpc協議方案

調試的例子為:代碼出現這個說明grpc調通成功;

ngrinder 壓測grpc協議方案

5.集成到ngrinder的grpc的代碼;

將jar上傳到lib目錄中。groovy引入,直接在@test裡面調用就可以了;

喜歡這篇文章嗎?立刻分享出去讓更多人知道吧!

本站內容充實豐富,博大精深,小編精選每日熱門資訊,隨時更新,點擊「搶先收到最新資訊」瀏覽吧!


請您繼續閱讀更多來自 程序員小新人學習 的精彩文章:
如有侵權請來信告知:酷播亮新聞 » ngrinder 壓測grpc協議方案