https://github.com/lddl/traefik-grpc-reverse-proxy
Reverse proxy gRPC via Traefik
https://github.com/lddl/traefik-grpc-reverse-proxy
golang grpc http2 load-balancer microservices proxy reverse-proxy rpc traefik traefik-v2
Last synced: 3 months ago
JSON representation
Reverse proxy gRPC via Traefik
- Host: GitHub
- URL: https://github.com/lddl/traefik-grpc-reverse-proxy
- Owner: LdDl
- License: other
- Created: 2022-03-28T18:36:09.000Z (about 3 years ago)
- Default Branch: master
- Last Pushed: 2022-04-06T15:07:08.000Z (about 3 years ago)
- Last Synced: 2025-01-21T18:49:14.040Z (5 months ago)
- Topics: golang, grpc, http2, load-balancer, microservices, proxy, reverse-proxy, rpc, traefik, traefik-v2
- Language: Go
- Homepage:
- Size: 23.4 KB
- Stars: 3
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
## Just an example how to make reverse proxy for gRPC
### Backstory
Some time ago I had a task to do reverse proxy for multiple gRPC services.I've already known how to do common HTTP/HTTPS/WS/WSS stuff with both [Traefik](https://traefik.io/) and [Ngnix](https://nginx.org), but I had not have familiar with HTTP2 (which is gRPC based on) reverse proxy.
I've start digging into and now I provide some workaround below.### Requirements:
* Installed Traefik - https://doc.traefik.io/traefik/getting-started/install-traefik/
In my case I've used binary distribution for linux_amd64.tar.gz with [v2.6.2 tag](https://github.com/traefik/traefik/releases/tag/v2.6.2). Your mileage may vary.
Why no Nginx? There is no objective reason... I've just tried to do it with Traefik and it worked
* Prepared configuration file.I prefer to have *.toml format. I'll describe some of its contents later.
* Clone this repo and navigate to main directory
```shell
git clone https://github.com/LdDl/traefik-grpc-reverse-proxy.git
cd traefik-grpc-reverse-proxy
```### Run
* Run first service
```shell
go run service_one/cmd/main.go
```
If you want to rebuild *.pb.go from *.proto file(-s) click to expand [Optional]* Be sure that you have protoc with Go compatibility
* Run protoc
```shell
protoc -I service_one/rpc/proto service_one/rpc/proto/*.proto --go_out=plugins=grpc:service_one/rpc/proto/
```
* Run second service
```shell
go run service_one/cmd/main.go
```
If you want to rebuild *.pb.go from *.proto file(-s) click to expand [Optional]* Be sure that you have protoc with Go compatibility
* Run protoc
```shell
protoc -I service_two/rpc/proto service_two/rpc/proto/*.proto --go_out=plugins=grpc:service_two/rpc/proto/
```
* Start Traefik
```shell
./traefik --configFile=./grpc_proxy_example.toml
```Main challenge to prepare reverse proxy is to prepare correct rules for forwaring.
```bash
...
rule = "PathPrefix(`/service_one.Service`)"
# Where 'service_one' is package name of first service protobuf and 'Service' is service name
...
rule = "PathPrefix(`/service_two.Service`)"
# Where 'service_two' is package name of second service protobuf and 'Service' is service name
...
```
Don't forget that load balancer URL has to start with "h2c://" since it's HTTP2* Start test client
Note: make sure that *.proto files in client folder match same files in services folders (and rebuild it if necessary)
If you want to rebuild client protobuffs [Optional]```shell
protoc -I client/proto_one client/proto_one/*.proto --go_out=plugins=grpc:client/proto_one/
protoc -I client/proto_two client/proto_two/*.proto --go_out=plugins=grpc:client/proto_two/
```
Run:
```shell
go run client/cmd/main.go
```
If everything goes fine then you'll see:
```shell
Response from service #1 is: code:200 text:"Message from service #1"
Response from service #2 is: code:200 text:"Message from service #2"
Response from service #1 (reverse proxy) is: code:200 text:"Message from service #1"
Response from service #2 (reverse proxy) is: code:200 text:"Message from service #2
```* That's all. Now you know how to do gRPC reverse proxy via Traefik