Auto-configures and runs the embedded gRPC server with @GRpcService-enabled beans as part of spring-boot application.
gRPC is a modern open source high performance RPC framework that can run in any environment. It can efficiently connect services in and across data centers with pluggable support for load balancing, tracing, health checking and authentication. It is also applicable in last mile of distributed computing to connect devices, mobile applications and browsers to backend services.
- Simple service definition:
Define your service using Protocol Buffers, a powerful binary serialization toolset and language - Works across languages and platforms:
Automatically generate idiomatic client and server stubs for your service in a variety of languages and platforms - Start quickly and scale:
Install runtime and dev environments with a single line and also scale to millions of RPCs per second with the framework - Bi-directional streaming and integrated auth:
Bi-directional streaming and fully integrated pluggable authentication with HTTP/2-based transport
By default, gRPC uses Protocol Buffers, Google’s mature open source mechanism for serializing structured data (although it can be used with other data formats such as JSON)
gRPC uses HTTP/2 as its transfer protocol, so it inherits some great features that HTTP/2 offers, such as binary framing, which is high performance and robust, lighter to transport and safer to decode compared to other text-based protocols. And because it’s binary, it’s a great combination with protocol buffer.
HTTP/2 also compresses the headers using HPACK, which will reduce the overhead cost and improve the performance.
Multiplexing is possible in HTTP/2, which means the client and server can send multiple requests and responses in parallel over a single TCP connection. This will help reduce the latency and improve network utilisation.
Finally, HTTP/2 allows server-push, where with 1 single request from client, server can send back multiple responses. This is extremely valuable to reduce the round-trip latency between client and server in many cases, when server knows exactly what resources client will need and send them before they’re even requested.
We can check HTTP/2 in http://http2demo.io for check performance with HTTP/1.1
So gRPC:
- is binary based. more efficient on the network , more efficient on cpu
- supports multiple languages c++,Objective-c,PHP,python,Ruby,Node.js,Go,C#,Java
- defines an interface , defines package and the payload. every field has a tag number.
- uses Stubby a RPC framework by google for very high traffic on requests O(10^10)=10,000,000,000 RPC calls per second
- proto files = human readable interface files to generate java stubs by maven plugin
- java stubs = java abstract classes that will be used by us to implement our services
- java service implementation = our implementation code for java stubs
- gRPC server = a server that let communicate client and server
- java stubs = interfaces that can be called
https://medium.com/@akhaku/protobuf-definition-best-practices-87f281576f31
https://developers.google.com/protocol-buffers/docs/style
https://developers.google.com/protocol-buffers/docs/proto3#scalar
https://github.com/protocolbuffers/protobuf/releases/tag/v3.12.3
https://grpc.io/docs/guides/error/
https://github.com/DKE-Data/agrirouter-api-protobuf-definitions/pull/16/files
https://itnext.io/protobuf-and-null-support-1908a15311b6
further references:
- https://grpc.io/docs/what-is-grpc/core-concepts/
- https://grpc.io/blog/grpc-on-http2/
- https://spring.io/blog/2015/03/22/using-google-protocol-buffers-with-spring-mvc-based-rest-services
- https://github.com/LogNet/grpc-spring-boot-starter
please see application.properties files in resources folder and select a active profile "dev" or "com" to run project. you can check test methods too.
tips:
- install "Protocol Buffer Editor" (https://plugins.jetbrains.com/plugin/14004-protocol-buffer-editor) for IntelijIDEA to detect .proto files (IntelijIDEA: File -> Settings -> Plugins -> search "Protocol Buffer Editor" and install and restart IDE.)
- in IntelijIDEA for build stub files from proto file: view menu>tool windows>maven>start
- BloomRPC: GUI Client for gRPC services. Inspired by Postman and GraphQL Playground BloomRPC aims to provide the simplest and most efficient developer experience for exploring and querying your gRPC services. Download at : https://github.com/uw-labs/bloomrpc/releases
Steps by Video:
- gRPC Project Setup in Java:
- How to create gRPC Proto files and Generate Java Stubs:
- Implementing gRPC Service in Java:
- How to Start gRPC Server in Java:
- How To Test gRPC Services Using GUI client:
- gRPC Client Implementation in Java:
- Source code:
- gRPC Java - Build Modern API & Microservices:
- IntelijIDEA: Help -> Edit Custom Vm Options -> add these two line:
- -Dfile.encoding=UTF-8
- -Dconsole.encoding=UTF-8
- IntelijIDEA: File -> Settings -> Editor -> File Encodings-> Project Encoding: form "System default" to UTF-8. May be it affected somehow.
- IntelijIDEA: File -> Settings -> Editor -> General -> Code Completion -> check "show the documentation popup in 500 ms"
- IntelijIDEA: File -> Settings -> Editor -> General -> Auto Import -> check "Optimize imports on the fly (for current project)"
- IntelijIDEA: File -> Settings -> Editor -> Color Scheme -> Color Scheme Font -> Scheme: Default -> uncheck "Show only monospaced fonts" and set font to "Tahoma"
- IntelijIDEA: Run -> Edit Configuration -> Spring Boot -> XXXApplication -> Configuration -> Environment -> VM Options: -Dspring.profiles.active=dev
- IntelijIDEA: Run -> Edit Configuration -> Spring Boot -> XXXApplication -> Code Coverage -> Fix the package in include box