Skip to content

Commit

Permalink
Add grpc and protobuf;
Browse files Browse the repository at this point in the history
  • Loading branch information
cnutshell committed Oct 18, 2018
1 parent 207c4f8 commit b5bfaec
Show file tree
Hide file tree
Showing 7 changed files with 105 additions and 5 deletions.
4 changes: 2 additions & 2 deletions docs/golang/gRPC.md → docs/golang/grpc/gRPC.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ GRPC_VERBOSITY=debug GRPC_TRACE=tcp,http,api ./helloworld_application_using_grpc

## Performance

Q: How to profile grpc's performance?
> - How to profile grpc's performance?
## Concepts

Expand All @@ -62,7 +62,7 @@ On the other hand, **networks are inherently asynchronous** and in many scenario

A gRPC call comprises of a bidirectional stream of messages, initiated by the client. In the client-to-server direction, this stream begins with a mandatory `Call Header`, followed by optional `Initial-Metadata`, followed by zero or more `Payload Messages`. The server-to-client direction contains an optional `Initial-Metadata`, followed by zero or more `Payload Messages` terminated with a mandatory `Status` and optional `Status-Metadata` (a.k.a.,`Trailing-Metadata`).

![gRPC protocol](./gRPC-protocol.png)
![gRPC protocol](./gRPC.png)

gRPC bidirectional streams are mapped to HTTP/2 streams.

Expand Down
File renamed without changes
75 changes: 75 additions & 0 deletions docs/golang/grpc/grpc-example.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
# How to use gRPC

[Reference Link](https://github.com/grpc/grpc-go/blob/master/examples/gotutorial.md)

## Steps

###1. Defining the service

Define a service within file *route_guide.proto*.

Syntax:

```protobuf
// Interface exported by the server.
service RouteGuide {
// you could place 'string' before a type
rpc GetFeature(Point) returns (Feature) {}
}
message Point {
int32 latitude = 1;
int32 longitude = 2;
}
message Feature {
string name = 1;
Point location = 2;
}
```

### 2. Generating client and server code

You could do this using the **protobuf compiler** `protoc` with a special gRPC **go plugin**.

```shell
[root@k8s-1 tmp]# protoc --go_out=plugins=grpc:. route_guide.proto
[root@k8s-1 tmp]# ls
route_guide.proto route_guide.pb.go
```

### 3. Handle the server

First, implement the generated interface.

Then, build and start the server:

```reStructuredText
1. Specify the port we want to use to listen for client requests;
2. Create a instance of the gRPC server;
3. Register our service implementation with the gRPC server;
4. Call Serve() on the server with our port details to do a blocking wait until the process is killed or Stop() is called.
```

### 4. Handle the client

First, create a stub.

```text
1. Create a gRPC channel to communicate with the server;
2. Once the gRPC channel is setup, we need a client stub to perform RPCs;
```

Note that, in gRPC-go, RPCs operate in a blocking/synchronous mode.

### 5. Try it out

```text
1. Start the server;
2. Start the client to try it our.
```





Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@
1. establish a *connection* to server
2. call Invoke on this *connection*

![](./grpc-example.png)
![](./grpc-outline.png)
File renamed without changes
21 changes: 21 additions & 0 deletions docs/golang/protobuf/protocol-buffer.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# protocol buffer

[Reference link](https://developers.google.com/protocol-buffers/docs/overview)

## Introduction

*protocol buffers* is a language-neutral, platform-neutral, extensible way of **serializing structured data**.

Develops could use *protocol buffers* in their applications.

*protocol buffers* is a **data format**, google's **data interchange format**.

## Qustions

- Why *protocol buffer* are faster than *xml*?

- How *protocol buffer* messages are encoded?
> [protocol buffer encoding](https://developers.google.com/protocol-buffers/docs/encoding)
- What's the major differences between *proto3* and *proto2*?

8 changes: 6 additions & 2 deletions mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,12 @@ nav:
- cpp: cpp/about.md
- rust: rust/about.md
- golang:
- gRPC: golang/gRPC.md
- Example: golang/grpc-example.md
- gRPC:
- gRPC: golang/grpc/gRPC.md
- Outline: golang/grpc/grpc-outline.md
- Example: golang/grpc/grpc-example.md
- protobuf:
- protobuf: golang/protobuf/protocol-buffer.md
- python:
- module: python/module.md
- shell:
Expand Down

0 comments on commit b5bfaec

Please sign in to comment.