Java के साथ gRPC सेवा बनाना

1. खास जानकारी

gRPC, Google ने बनाया है. यह एक ऐसा रिमोट प्रोसीजर कॉल (आरपीसी) फ़्रेमवर्क और टूलसेट है जो किसी भी भाषा और प्लैटफ़ॉर्म पर काम करता है. इसकी मदद से, प्रोटोकॉल बफ़र का इस्तेमाल करके कोई सेवा तय की जा सकती है. प्रोटोकॉल बफ़र, बाइनरी सीरियलाइज़ेशन का एक बहुत ही शक्तिशाली टूलसेट और भाषा है. इसके बाद, यह आपको कई भाषाओं में, सेवा की परिभाषा से मुहावरेदार क्लाइंट और सर्वर स्टब जनरेट करने की सुविधा देता है.

इस कोडलैब में, आपको Java पर आधारित ऐसी सेवा बनाने का तरीका बताया जाएगा जो gRPC फ़्रेमवर्क का इस्तेमाल करके एपीआई को ऐक्सेस करने की सुविधा देती है. इसके बाद, जनरेट किए गए gRPC क्लाइंट-साइड स्टब का इस्तेमाल करने के लिए, क्लाइंट लिखने का तरीका बताया जाएगा.

आपको क्या सीखने को मिलेगा

  • प्रोटोकॉल बफ़र की भाषा
  • Java का इस्तेमाल करके gRPC सेवा को लागू करने का तरीका
  • Java का इस्तेमाल करके gRPC क्लाइंट को लागू करने का तरीका

इस ट्यूटोरियल का इस्तेमाल कैसे किया जाएगा?

सिर्फ़ इसे पढ़ें इसे पढ़ें और एक्सरसाइज़ पूरी करें

Node.js ऐप्लिकेशन बनाने के अपने अनुभव को आप क्या रेटिंग देंगे?

शुरुआती सामान्य एडवांस

Go ऐप्लिकेशन बनाने के अपने अनुभव को आप क्या रेटिंग देंगे?

शुरुआती सामान्य एडवांस

2. सेटअप और ज़रूरी शर्तें

अपने हिसाब से एनवायरमेंट सेट अप करना

  1. Cloud Console में साइन इन करें. इसके बाद, नया प्रोजेक्ट बनाएं या किसी मौजूदा प्रोजेक्ट का फिर से इस्तेमाल करें. अगर आपके पास पहले से कोई Gmail या Google Workspace खाता नहीं है, तो आपको एक खाता बनाना होगा.

96a9c957bc475304.png

b9a10ebdf5b5a448.png

a1e3c01a38fa61c2.png

प्रोजेक्ट आईडी याद रखें. यह सभी Google Cloud प्रोजेक्ट के लिए एक यूनीक नाम होता है. ऊपर दिया गया नाम पहले ही इस्तेमाल किया जा चुका है. इसलिए, यह आपके लिए काम नहीं करेगा. माफ़ करें! इस कोड लैब में इसे बाद में PROJECT_ID के तौर पर दिखाया जाएगा.

  1. इसके बाद, Google Cloud संसाधनों का इस्तेमाल करने के लिए, आपको Cloud Console में बिलिंग चालू करनी होगी.

इस कोडलैब को पूरा करने में ज़्यादा खर्च नहीं आएगा. "सफ़ाई करना" सेक्शन में दिए गए निर्देशों का पालन करना न भूलें. इसमें बताया गया है कि संसाधनों को कैसे बंद किया जाए, ताकि इस ट्यूटोरियल के बाद आपको बिलिंग न करनी पड़े. Google Cloud के नए उपयोगकर्ताओं को, मुफ़्त में आज़माने के लिए 300 डॉलर का क्रेडिट मिलता है.

Google Cloud Shell

इस कोडलैब को अपने कंप्यूटर से चलाया जा सकता है. हालांकि, इस कोडलैब में हम Google Cloud Shell का इस्तेमाल करेंगे. यह क्लाउड में चलने वाला कमांड लाइन एनवायरमेंट है.

यह Debian पर आधारित वर्चुअल मशीन है. इसमें डेवलपमेंट के लिए ज़रूरी सभी टूल पहले से मौजूद हैं. यह 5 जीबी की होम डायरेक्ट्री उपलब्ध कराता है और Google Cloud में चलता है. इससे नेटवर्क की परफ़ॉर्मेंस और पुष्टि करने की प्रोसेस बेहतर होती है. इसका मतलब है कि इस कोडलैब के लिए, आपको सिर्फ़ एक ब्राउज़र की ज़रूरत होगी. हां, यह Chromebook पर भी काम करता है.

  1. Cloud Console से Cloud Shell चालू करने के लिए, बस Cloud Shell चालू करें a8460e837e9f5fda.png पर क्लिक करें. इसे चालू होने और एनवायरमेंट से कनेक्ट होने में कुछ ही समय लगेगा.

b532b2f19ab85dda.png

Screen Shot 2017-06-14 at 10.13.43 PM.png

Cloud Shell से कनेक्ट होने के बाद, आपको दिखेगा कि आपकी पुष्टि पहले ही हो चुकी है और प्रोजेक्ट पहले से ही आपके PROJECT_ID पर सेट है.

gcloud auth list

कमांड आउटपुट

Credentialed accounts:
 - <myaccount>@<mydomain>.com (active)
gcloud config list project

कमांड आउटपुट

[core]
project = <PROJECT_ID>

अगर किसी वजह से प्रोजेक्ट सेट नहीं है, तो यह कमांड दें:

gcloud config set project <PROJECT_ID>

क्या आपको PROJECT_ID की तलाश है? देखें कि आपने सेटअप के दौरान किस आईडी का इस्तेमाल किया था या Cloud Console के डैशबोर्ड में जाकर इसे देखें:

2485e00c1223af09.png

Cloud Shell, कुछ एनवायरमेंट वैरिएबल को डिफ़ॉल्ट रूप से भी सेट करता है. ये वैरिएबल, आने वाले समय में कमांड चलाने के दौरान आपके काम आ सकते हैं.

echo $GOOGLE_CLOUD_PROJECT

कमांड आउटपुट

<PROJECT_ID>
  1. आखिर में, डिफ़ॉल्ट ज़ोन और प्रोजेक्ट कॉन्फ़िगरेशन सेट करें.
gcloud config set compute/zone us-central1-f

आपके पास अलग-अलग ज़ोन चुनने का विकल्प होता है. ज़्यादा जानकारी के लिए, रीजन और ज़ोन देखें.

3. gRPC सेवा बनाना

Maven की मदद से नया Java प्रोजेक्ट बनाएं:

$ mvn archetype:generate -DgroupId=com.example.grpc \
 -DartifactId=grpc-hello-server \
 -DarchetypeArtifactId=maven-archetype-quickstart \
 -DinteractiveMode=false
$ cd grpc-hello-server

gRPC की परिभाषा वाली फ़ाइल जोड़ना

gRPC में, सेवा के पेलोड (अनुरोध और जवाब) और सेवा के कामों को आईडीएल (इंटरफ़ेस डेफ़िनिशन लैंग्वेज) में कैप्चर करना होता है. gRPC, मैसेज पेलोड और कार्रवाइयों को तय करने के लिए, Protobuffer 3 सिंटैक्स का इस्तेमाल करता है. आइए, Hello Request और Hello Response के साथ एक सामान्य Greeting Service के लिए प्रोटो फ़ाइल बनाते हैं.

सबसे पहले, नई प्रोटो फ़ाइल को सेव करने के लिए एक नई प्रोटो डायरेक्ट्री बनाएं:

$ mkdir -p src/main/proto

इसके बाद, एक नई प्रोटो फ़ाइल src/main/proto/GreetingService.proto बनाएं.

फ़ाइल में बदलाव करने के लिए, vim,nano,या emacs का इस्तेमाल किया जा सकता है:

src/main/proto/GreetingService.proto

syntax = "proto3";
package com.example.grpc;

// Request payload
message HelloRequest {
  // Each message attribute is strongly typed.
  // You also must assign a "tag" number.
  // Each tag number is unique within the message.
  string name = 1;

  // This defines a strongly typed list of String
  repeated string hobbies = 2;

  // There are many more basics types, like Enum, Map
  // See https://developers.google.com/protocol-buffers/docs/proto3
  // for more information.
}

message HelloResponse {
  string greeting = 1;
}

// Defining a Service, a Service can have multiple RPC operations
service GreetingService {
  // Define a RPC operation
  rpc greeting(HelloRequest) returns (HelloResponse);
}

gRPC डिपेंडेंसी और प्लगिन जोड़ना

डेफ़िनिशन मिलने के बाद, हम इस फ़ाइल से सर्वर-साइड स्टब और क्लाइंट-साइड स्टब, दोनों जनरेट कर सकते हैं. आपको gRPC डिपेंडेंसी और प्लगिन जोड़ने होंगे.

सबसे पहले, pom.xml में gRPC डिपेंडेंसी जोड़ें:

pom.xml

<project>
  ...
  <dependencies>
    <dependency>
      <groupId>io.grpc</groupId>
      <artifactId>grpc-netty-shaded</artifactId>
      <version>1.24.0</version>
    </dependency>
    <dependency>
      <groupId>io.grpc</groupId>
      <artifactId>grpc-protobuf</artifactId>
      <version>1.24.0</version>
    </dependency>
    <dependency>
      <groupId>io.grpc</groupId>
      <artifactId>grpc-stub</artifactId>
      <version>1.24.0</version>
    </dependency>
    <dependency>
      <groupId>javax.annotation</groupId>
      <artifactId>javax.annotation-api</artifactId>
      <version>1.3.2</version>
    </dependency>
    ...
  </dependencies>
  ...
</project>

इसके बाद, प्लगिन जोड़ें:

pom.xml

<project>
  ...
  <dependencies>
    ...
  </dependencies>
  <build>
    <extensions>
      <extension>
        <groupId>kr.motd.maven</groupId>
        <artifactId>os-maven-plugin</artifactId>
        <version>1.6.2</version>
      </extension>
    </extensions>
    <plugins>
      <plugin>
        <groupId>org.xolstice.maven.plugins</groupId>
        <artifactId>protobuf-maven-plugin</artifactId>
        <version>0.6.1</version>
        <configuration>
          <protocArtifact>com.google.protobuf:protoc:3.9.0:exe:${os.detected.classifier}</protocArtifact>
          <pluginId>grpc-java</pluginId>
          <pluginArtifact>io.grpc:protoc-gen-grpc-java:1.24.0:exe:${os.detected.classifier}</pluginArtifact>
        </configuration>
        <executions>
          <execution>
            <goals>
              <goal>compile</goal>
              <goal>compile-custom</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>

</project>

स्टब जनरेट करना

ऐप्लिकेशन बनाने के दौरान, प्लगिन प्रोटोकॉल बफ़र की परिभाषाओं को Java कोड में बदल देगा.

$ mvn -DskipTests package

जनरेट की गई फ़ाइलें देखने के लिए:

$ find target/generated-sources

सेवा को लागू करना

सबसे पहले, एक नई GreetingServiceImpl क्लास बनाएं, जो greeting ऑपरेशन को लागू करेगी:

src/main/java/com/example/grpc/GreetingServiceImpl.java

package com.example.grpc;

import io.grpc.stub.StreamObserver;

public class GreetingServiceImpl extends GreetingServiceGrpc.GreetingServiceImplBase {
  @Override
  public void greeting(GreetingServiceOuterClass.HelloRequest request,
        StreamObserver<GreetingServiceOuterClass.HelloResponse> responseObserver) {
  // HelloRequest has toString auto-generated.
    System.out.println(request);

    // You must use a builder to construct a new Protobuffer object
    GreetingServiceOuterClass.HelloResponse response = GreetingServiceOuterClass.HelloResponse.newBuilder()
      .setGreeting("Hello there, " + request.getName())
      .build();

    // Use responseObserver to send a single response back
    responseObserver.onNext(response);

    // When you are done, you must call onCompleted.
    responseObserver.onCompleted();
  }
}

सर्वर को लागू करना

आखिर में, आपको किसी पोर्ट पर सुनने के लिए सर्वर शुरू करना होगा और इस सेवा को लागू करने के लिए रजिस्टर करना होगा. App क्लास और उसके मुख्य तरीके में बदलाव करें:

src/main/java/com/example/grpc/App.java

package com.example.grpc;

import io.grpc.*;

public class App
{
    public static void main( String[] args ) throws Exception
    {
      // Create a new server to listen on port 8080
      Server server = ServerBuilder.forPort(8080)
        .addService(new GreetingServiceImpl())
        .build();

      // Start the server
      server.start();

      // Server threads are running in the background.
      System.out.println("Server started");
      // Don't exit the main thread. Wait until server is terminated.
      server.awaitTermination();
    }
}

आखिर में, सर्वर चलाएं:

$ mvn -DskipTests package exec:java -Dexec.mainClass=com.example.grpc.App
...
Server Started

4. सेवा का इस्तेमाल करना

जनरेटर ने पहले ही सभी क्लाइंट-साइड स्टब जनरेट कर दिए हैं. लैब को आसान बनाने के लिए, हम एक ही Maven प्रोजेक्ट का इस्तेमाल करेंगे. हालांकि, हम एक नया मुख्य तरीका इस्तेमाल करके, एक नई Client क्लास जोड़ेंगे.

सबसे पहले, + पर क्लिक करके, नया Cloud Shell सेशन खोलें, ताकि आपको सर्वर बंद न करना पड़े:

1ff0fda960b9adfc.png

नए सेशन में, grpc-hello-server डायरेक्ट्री पर स्विच करें:

$ cd grpc-hello-server

इसके बाद, नई Client क्लास जोड़ें:

src/main/java/com/example/grpc/Client.java

package com.example.grpc;

import io.grpc.*;

public class Client
{
    public static void main( String[] args ) throws Exception
    {
      // Channel is the abstraction to connect to a service endpoint
      // Let's use plaintext communication because we don't have certs
      final ManagedChannel channel = ManagedChannelBuilder.forTarget("localhost:8080")
        .usePlaintext(true)
        .build();

      // It is up to the client to determine whether to block the call
      // Here we create a blocking stub, but an async stub,
      // or an async stub with Future are always possible.
      GreetingServiceGrpc.GreetingServiceBlockingStub stub = GreetingServiceGrpc.newBlockingStub(channel);
      GreetingServiceOuterClass.HelloRequest request =
        GreetingServiceOuterClass.HelloRequest.newBuilder()
          .setName("Ray")
          .build();

      // Finally, make the call using the stub
      GreetingServiceOuterClass.HelloResponse response = 
        stub.greeting(request);

      System.out.println(response);

      // A Channel should be shutdown before stopping the process.
      channel.shutdownNow();
    }
}

आखिर में, क्लाइंट को चलाएं:

$ mvn -DskipTests package exec:java -Dexec.mainClass=com.example.grpc.Client
...
greeting: "Hello there, Ray"

हो गया! यह बहुत आसान है, है न?

5. स्ट्रीमिंग सेवा

आपके पास और भी कई विकल्प हैं. उदाहरण के लिए, अनुरोध या जवाब पैरामीटर में stream कीवर्ड को प्रोटो फ़ाइल में जोड़कर, आसानी से स्ट्रीमिंग सेवा बनाई जा सकती है. जैसे,

src/main/proto/GreetingService.proto

syntax = "proto3";
package com.example.grpc;

...

// Defining a Service, a Service can have multiple RPC operations
service GreetingService {
  // MODIFY HERE: Update the return to streaming return.
  rpc greeting(HelloRequest) returns (stream HelloResponse);
}

अपने सर्वर को अपडेट करें, ताकि वह एक के बजाय कई जवाब भेज सके. इसके लिए, एक से ज़्यादा responseObserver.onNext(...) कॉल करें:

src/main/java/com/example/grpc/GreetingServiceImpl.java

package com.example.grpc;

import io.grpc.stub.StreamObserver;

public class GreetingServiceImpl extends GreetingServiceGrpc.GreetingServiceImplBase {
  @Override
  public void greeting(GreetingServiceOuterClass.HelloRequest request,
        StreamObserver<GreetingServiceOuterClass.HelloResponse> responseObserver) {
 
    ...

    // Feel free to construct different responses if you'd like.
    responseObserver.onNext(response);
    responseObserver.onNext(response);
    responseObserver.onNext(response);

    // When you are done, you must call onCompleted.
    responseObserver.onCompleted();
  }
}

क्लाइंट को ब्लॉकिंग स्टब के बजाय, असिंक्रोनस स्टब का इस्तेमाल करना चाहिए. क्लाइंट कोड को अपडेट करें. साथ ही, यह पक्का करें कि stub टाइप को GreetingServiceStub पर अपडेट किया गया हो:

src/main/java/com/example/grpc/Client.java

package com.example.grpc;

import io.grpc.*;

// New import
import io.grpc.stub.*;

public class Client
{
    public static void main( String[] args ) throws Exception
    {
      final ManagedChannel channel = ManagedChannelBuilder.forTarget("localhost:8080")
        .usePlaintext(true)
        .build();

      // Replace the previous synchronous code with asynchronous code.
      // This time use an async stub:
       GreetingServiceGrpc.GreetingServiceStub stub = GreetingServiceGrpc.newStub(channel);

      // Construct a request
      GreetingServiceOuterClass.HelloRequest request =
        GreetingServiceOuterClass.HelloRequest.newBuilder()
          .setName("Ray")
          .build();

      // Make an Asynchronous call. Listen to responses w/ StreamObserver
      stub.greeting(request, new StreamObserver<GreetingServiceOuterClass.HelloResponse>() {
        public void onNext(GreetingServiceOuterClass.HelloResponse response) {
          System.out.println(response);
        }
        public void onError(Throwable t) {
        }
        public void onCompleted() {
          // Typically you'll shutdown the channel somewhere else.
          // But for the purpose of the lab, we are only making a single
          // request. We'll shutdown as soon as this request is done.
          channel.shutdownNow();
        }
      });
    }
}

ऐप्लिकेशन को फिर से बनाएं:

$ mvn -DskipTests package

सर्वर और क्लाइंट, दोनों को उनके Cloud Shell सेशन में रीस्टार्ट करें.

सर्वर शुरू करने के लिए:

$ mvn exec:java -Dexec.mainClass=com.example.grpc.App
...
Server Started

क्लाइंट शुरू करने के लिए:

$ mvn exec:java -Dexec.mainClass=com.example.grpc.Client
...
greeting: "Hello there, Ray"
greeting: "Hello there, Ray"
greeting: "Hello there, Ray"

6. बधाई हो!

हमने इन विषयों पर बात की:

  • प्रोटोकॉल बफ़र की भाषा
  • Java का इस्तेमाल करके gRPC सर्वर को लागू करने का तरीका
  • Java का इस्तेमाल करके gRPC क्लाइंट को लागू करने का तरीका

अगले चरण:

हमें सुझाव/राय दें या शिकायत करें

  • कृपया थोड़ा समय निकालकर, हमारे छोटे से सर्वे में हिस्सा लें