6 Ağustos 2020 Perşembe

gRPC - gRPC Remote Procedure Calls

Giriş
Microservis mimarisinde REST veya gRPC sıkça kullanılıyor. gRPC kullanabilmek için ProtoBuf bilmek gerekiyor. ProtoBuf yazısına bakabilirsiniz.

g Harfi Ne Anlama Gelir
Açıklaması şöyle. Liste burada
'g' stands for something different every gRPC release:
gRPC ve REST Karşılaştırması
gRPC'nin REST'e tercih edilmesi için bazı sebepler şöyle
Following are some reasons you should choose gRPC over Rest:
1) gRPC transfer binary data as payload rather then text which makes communication much faster and compact.
2) gRPC use HTTP/2 with long-lived connections so overhead of making connection per request can be avoided.
3) gRPC is type-safe so if you are expecting an integer then nobody can send it as a string.
4) gRPC is few millisecond faster then REST. So if millisecond matter for you then gRPC can be good choice.
5) gRPC supports streaming so if you are looking for distributed streaming support gRPC can be pretty useful in this case.
Bu sebepler, şu avantajları getiriyor.
gRPC is very good for the streaming requirements and if you want to avoid making connections per request. In the case of a microservice architecture it has the added advantage of a reduction of payload, saving the connection cost and type-safe contract.

gRPC client also supports reactive way of handling the request, so if you are developing the reactive architecture then gRPC can support that too.
gRPC Protobuf İle Çalışır
Açıklaması şöyle
gRPC uses protocol buffers by default.
gRPC ve Diğer Transportlar
Açıklaması şöyle
gRPC works with many transports, by default it uses HTTP/2 but it can also be implemented on QUIC , Cronet, etc.
gRPC ve HTTP/2 Transport
HTTP/2 ile çalışır. Açıklaması şöyle.
...gRPC requires more intentional design by providing an interface specification (protobuf) and requires HTTP/2 as its protocol, which currently doesn’t have wide browser support. However, gRPC offers significant advantages including a compact binary message format, bi-directional streaming and improved performance at scale.
Şeklen şöyle

Açıklaması şöyle. Burada binary gRPC paketinin nasıl HTTP/2 POST isteği ile gönderildiği anlatılıyor
In a typical gRPC call, the following steps take place.
1. It all starts with a client workflow initiating an RPC call.
2. Once RPC starts, the client stub encodes the message in binary format. And, then it creates an HTTP POST request with the encoded message.
3. Afterward, the encoded message is sent over the channel. A gRPC channel provides a connection to a gRPC server on a specified host and port.
4. On the server-side, the server hands over the encoded message to the auto-generated server stub.
5. After receiving the message, the server stub deserializes the message into a language-specific data structure.
6. And finally, the server stub makes a call to the overridden service method and passes the parsed message.

Maven Dependency
gRPC Maven Dependency yazısına taşıdım

Service Definitions in gRPC
4 çeşit servis tanımı var
1. Unary . Request, response ile aynıdır.
2. Server Streaming
3. Client Streaming
4. Bi-Directional Streaming

1. Unary 
Klasik request-response çağrısı. Senkron veya asenkron çalışabilir.

2. Client Streaming
Açıklaması şöyle
Client keeps on sending a request to the server by using a single TCP connection. The server might accept all the messages and sends a single response back. Use case: File upload functionality
3. Server Streaming
Açıklaması şöyle. Dashboard vs gibi için kullanılabilir.
Server sends multiple messages to the client via single TCP connection. Use case: Pagination or Server pushes periodic updates to the client asynchronously.
4. Bi-Directional Streaming
Açıklaması şöyle
Client and Server can keep on sharing messages via single TCP connection. Use case: Chat application or GPS or Client & server have to work together in an interactive way to complete a task
protobuf
gRPC protobuf yazısına taşıdım

gRPC Gradle Plugin
gRPC Gradle Plugin yazısına taşıdım

gRPC Maven Plugin
Kod üretmek içindir. Sanırım en çok protobuf-maven-plugin kullanılıyor

Örnek - protoc-jar-maven-plugin
Şöyle yaparız
<plugin>
  <groupId>com.github.os72</groupId>
  <artifactId>protoc-jar-maven-plugin</artifactId>
  <version>3.7.1</version>
  <executions>
    <execution>
     <phase>generate-sources</phase>
      <goals>
        <goal>run</goal>
       </goals>
      <configuration>
        <includeMavenTypes>direct</includeMavenTypes>
        <inputDirectories>
          <include>${project.basedir}/src/main/Protobuf</include>
        </inputDirectories>
        <outputTargets>
          <outputTarget>
            <type>java</type>
            <outputDirectory>
              ${project.basedir}/target/generated-sources/protobuf
            </outputDirectory>
        </outputTarget>
        <outputTarget>
          <type>grpc-java</type>
          <pluginArtifact>io.grpc:protoc-gen-grpc-java:1.30.0</pluginArtifact>
          <outputDirectory>
            ${project.basedir}/target/generated-sources/protobuf
          </outputDirectory>
        </outputTarget>
      </outputTargets>
    </configuration>
  </execution>
  </executions>
</plugin
Örnek - protobuf-maven-plugin
protobuf plugin yazısına taşıdım

gRPC Server
gRPC Server yazısına taşıdım

gRPC Service
gRP Service yazısına taşıdım

gRPC Client
gRPC Client yazısına taşıdım

Hiç yorum yok:

Yorum Gönder