|
1 | 1 | # grpc_gateway_demo |
2 | 2 |
|
3 | | -```shell |
4 | | -$ curl localhost:8080/get -4 -X POST -H "Content-Type:application/json" -d '{"filename": "testdata/big.wav"}' -v > test_chunk_big.wav |
5 | | -Note: Unnecessary use of -X or --request, POST is already inferred. |
| 3 | +This repository is used to show the gRPC-JSON transcoder in grap-gateway(https://github.com/grpc-ecosystem/grpc-gateway) can be used together with stream google.api.HttpBody to support streaming media transfer. For example, in the scenario of text-to-speech, it's useful to synthesize speech with streaming mode to reduce latency. This demo also shows how we can automatically transcode streaming grpc to chunked http response. |
| 4 | + |
| 5 | +## Quick Start Guide |
| 6 | + |
| 7 | +Clone the repository and dependencies: |
| 8 | +```sh |
| 9 | +git clone https://github.com/npuichigo/grpc_gateway_demo.git |
| 10 | +cd grpc_gateway_demo |
| 11 | +# Pull the dependent third_party repositories (if exists) |
| 12 | +git submodule update --init |
| 13 | +``` |
| 14 | + |
| 15 | +From the repo directory, test your service with docker-compose: |
| 16 | +```sh |
| 17 | +docker-compose pull |
| 18 | +docker-compose up |
| 19 | +``` |
| 20 | + |
| 21 | +Or you can rebuild the images with: |
| 22 | + |
| 23 | +```sh |
| 24 | +$ docker-compose build |
| 25 | +``` |
| 26 | + |
| 27 | +Test restful api with curl: |
| 28 | + |
| 29 | +```sh |
| 30 | +$ curl localhost:8080/get?filename=testdata/music.mp3 -4 -v > test_chunk_music.mp3 |
6 | 31 | % Total % Received % Xferd Average Speed Time Time Time Current |
7 | 32 | Dload Upload Total Spent Left Speed |
8 | 33 | 0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0* Trying 127.0.0.1... |
9 | 34 | * Connected to localhost (127.0.0.1) port 8080 (#0) |
10 | | -> POST /get HTTP/1.1 |
| 35 | +> GET /get?filename=testdata/music.mp3 HTTP/1.1 |
11 | 36 | > Host: localhost:8080 |
12 | 37 | > User-Agent: curl/7.47.0 |
13 | 38 | > Accept: */* |
14 | | -> Content-Type:application/json |
15 | | -> Content-Length: 32 |
16 | 39 | > |
17 | | -} [32 bytes data] |
18 | | -* upload completely sent off: 32 out of 32 bytes |
19 | 40 | < HTTP/1.1 200 OK |
20 | | -< Content-Type: audio/wav |
| 41 | +< Content-Type: audio/mp3 |
21 | 42 | < Grpc-Metadata-Accept-Encoding: identity,gzip |
22 | 43 | < Grpc-Metadata-Content-Type: application/grpc |
23 | 44 | < Grpc-Metadata-Grpc-Accept-Encoding: identity,deflate,gzip |
24 | | -< Date: Mon, 25 Nov 2019 13:42:58 GMT |
| 45 | +< Date: Wed, 27 Nov 2019 03:28:40 GMT |
25 | 46 | < Transfer-Encoding: chunked |
26 | 47 | < |
27 | 48 | { [3836 bytes data] |
28 | | -100 1137k 0 1137k 100 32 64627 1 0:00:32 0:00:18 0:00:14 58610 |
29 | | -* Connection #0 to host localhost left intact |
30 | 49 | ``` |
| 50 | +
|
| 51 | +The grpc-server will send chunk of streaming data and the grpc-gateway will transcode that to chunked http response. |
| 52 | +```sh |
| 53 | +$ docker-compose up |
| 54 | +WARNING: Some services (grpc-server) use the 'deploy' key, which will be ignored. Compose does not support 'deploy' configuration - use `docker stack deploy` to deploy to a swarm. |
| 55 | +Starting grpc_gateway_demo_grpc-server_1 ... done |
| 56 | +Starting grpc_gateway_demo_grpc-gateway_1 ... done |
| 57 | +Starting grpc_gateway_demo_nginx_1 ... done |
| 58 | +Attaching to grpc_gateway_demo_grpc-server_1, grpc_gateway_demo_grpc-gateway_1, grpc_gateway_demo_nginx_1 |
| 59 | +grpc-server_1 | I1127 03:28:21.755975 1 server.cc:58] Running gRPC Server at 0.0.0.0:9090 ... |
| 60 | +grpc-server_1 | I1127 03:28:40.348382 9 demo_service_impl.cc:36] Get audio from grpc server: testdata/music.mp3 |
| 61 | +grpc-server_1 | I1127 03:28:40.349026 9 demo_service_impl.cc:59] Send 65536 bytes |
| 62 | +grpc-server_1 | I1127 03:28:40.449465 9 demo_service_impl.cc:59] Send 65536 bytes |
| 63 | +grpc-server_1 | I1127 03:28:40.550000 9 demo_service_impl.cc:59] Send 65536 bytes |
| 64 | +``` |
| 65 | +
|
| 66 | +You can now use your browser to play the streaming media. |
| 67 | +
|
| 68 | +## Test with grpc client |
| 69 | +
|
| 70 | +Just start with the docker image grpc-server: |
| 71 | +```sh |
| 72 | +docker run -it --name your_name npuichigo/grpc-server /bin/bash |
| 73 | +./build/bin/grpc_server |
| 74 | +``` |
| 75 | +
|
| 76 | +Open another terminal to use the grpc client: |
| 77 | +```sh |
| 78 | +docker exec -it your_name /bin/bash |
| 79 | +./build/bin/grpc_test_client --filename testdata/music.mp3 |
| 80 | +``` |
| 81 | +
|
| 82 | +
|
0 commit comments