Go-Kit Api Gateway

Getting the source code

Download the go-kit examples by go get’ing the repo somewhere on your local system:

$ go get github.com/go-kit/kit

Starting Addsvc

Cd into the addsvc example directory (this is assuming your $GOHOME is set to $HOME/go), then fetch the dependencies:

$ cd $HOME/go/src/github.com/go-kit/kit/examples/addsvc
$ go get
$ go run addsvc.go -debug.addr :7080 -http-addr :7081 -grpc-addr :7082 -thrift-addr :7083 -jsonrpc-addr :7084
$ curl -XPOST -d'{"a":"7","b":"4"}' http://localhost:7081/concat
{"v":"74"}
$ curl -XPOST -d'{"a":7,"b":4}' http://localhost:7081/sum
{"v":11}

Starting Stringsvc

We will now start a second example service. Open a new terminal, cd into the stringsvc3 example directory, fetch dependencies, then run the service:

$ cd $HOME/go/src/github.com/go-kit/kit/examples/stringsvc3
$ go get
$ go run *.go -listen :8081
$ curl -XPOST -d'{"s":"mytest"}' http://localhost:8081/count
{"v":6}
$ curl -XPOST -d'{"s":"mytest"}' http://localhost:8081/uppercase
{"v":"MYTEST"}

Starting Consul

First, you need to download Consul for your specific OS/architecture here.

$ mkdir ./consul.d$ echo '{"service": {"name": "addsvc", "tags": [], "port": 7082}}' > ./consul.d/addsvc.json$ echo '{"service": {"name": "stringsvc", "tags": [], "port": 8081}}' > ./consul.d/stringsvc.json
$ ./consul agent -dev -config-dir=./consul.d

Starting Api Gateway

Now that we have Consul running and all of our services registered with Consul, we can make use of the Consul api by running the apigateway go-kit example. The Api Gateway pattern is meant to be used to create a single entry point for requests that will be handled by a variety of services. You can read more about it here. This is what the apigateway example will do for us.

$ cd $HOME/go/src/github.com/go-kit/kit/examples/apigateway
$ go run main.go -consul.addr :8500
$ curl -XPOST -d'{"a":"7","b":"4"}' http://localhost:8000/addsvc/concat
{"v":"74"}
$ curl -XPOST -d'{"a":7,"b":4}' http://localhost:8000/addsvc/sum
{"v":11}
$ curl -XPOST -d'{"s":"mytest"}' http://localhost:8000/stringsvc/count
{"v":6}
$ curl -XPOST -d'{"s":"mytest"}' http://localhost:8000/stringsvc/uppercase
{"v":"MYTEST"}

--

--

Love podcasts or audiobooks? Learn on the go with our new app.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store