08.go-kit/

directory
v0.0.0-...-d0adc60 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 14, 2024 License: MIT

README

Go-kit Examples

stringsvc

stringsvc1
go run 01.stringsvc1/main.go

curl -XPOST -d'{"s":"hello, world"}' localhost:8080/uppercase
curl -XPOST -d'{"s":"hello, world"}' localhost:8080/count
stringsvc2
go run 02.stringsvc2/*.go

curl -XPOST -d'{"s":"hello, world"}' localhost:8080/uppercase
curl -XPOST -d'{"s":"hello, world"}' localhost:8080/count
stringsvc3
  • add extra package
go get github.com/streadway/handy/breaker
go get github.com/afex/hystrix-go/hystrix
go get golang.org/x/time/rate
  • run server and proxy
go run 03.stringsvc3/*.go -listen=:8001 &
go run 03.stringsvc3/*.go -listen=:8002 &
go run 03.stringsvc3/*.go -listen=:8003 &
go run 03.stringsvc3/*.go -listen=:8080 -proxy=localhost:8001,localhost:8002,localhost:8003
  • run client
for s in foo bar baz ; do curl -d"{\"s\":\"$s\"}" localhost:8080/uppercase ; done
for s in foo bar baz ; do curl -d"{\"s\":\"$s\"}" localhost:8080/count ; done
curl localhost:8080/metrics

addsvc

apt-get -y install flex bison
wget https://github.com/apache/thrift/archive/v0.13.0.tar.gz
tar -zxvf v0.13.0.tar.gz
cd thrift-0.13.0/
./bootstrap.sh
./configure  --without-qt4 --without-qt5 --without-c_glib --without-csharp --without-java --without-erlang --without-nodejs --without-lua --without-python --without-perl --without-php --without-php_extension --without-dart --without-ruby --without-haskell --without-rs --without-cl --without-haxe --without-dotnetcore --without-d
make; make install
  • build
make pb thrift
make
  • run server.bin for test
./server.bin 
  • run client.bin for test
./client.bin -http-addr=localhost:8081 -method=sum 1 2
./client.bin -http-addr=localhost:8081 -method=concat 1 2
./client.bin -grpc-addr=localhost:8082 -method=sum 1 2
./client.bin -grpc-addr=localhost:8082 -method=concat 1 2
./client.bin -thrift-addr=localhost:8083 -method=sum 1 2
./client.bin -thrift-addr=localhost:8083 -method=concat 1 2
./client.bin -jsonrpc-addr=localhost:8084 -method=sum 1 2
./client.bin -jsonrpc-addr=localhost:8084 -method=concat 1 2

profilesvc

  • run profilesvc
go run ./cmd/profilesvc/main.go
  • create a profile
curl -d '{"id":"1234","Name":"Go Kit"}' -H "Content-Type: application/json" -X POST http://localhost:8080/profiles/
curl localhost:8080/profiles/1234
curl -d '{"id": "1", "location": "location1"}' -H "Content-Type: application/json" -X POST http://localhost:8080/profiles/1234/addresses/
curl -X GET localhost:8080/profiles/1234/addresses/
curl -X DELETE localhost:8080/profiles/1234/addresses/1

shipping

  • reference : GoDDD

  • test booking

    • Check out the sample cargos
    curl -X GET http://localhost:8080/booking/v1/cargos
    
    • Book new cargo
    curl -X POST -d '{"origin": "SESTO", "destination": "FIHEL", "arrival_deadline": "2016-03-21T19:50:24Z"}' -H "Content-Type: application/json" http://localhost:8080/booking/v1/cargos 
    curl -X POST -d '{"origin": "SESTO", "destination": "CNHKG", "arrival_deadline": "2021-01-19T09:28:00Z"}' -H "Content-Type: application/json" http://localhost:8080/booking/v1/cargos
    
    • Get cargo ABC123
    curl -X GET http://localhost:8080/booking/v1/cargos/ABC123
    
    • Request possible routes for sample cargo ABC123
    curl -X GET http://localhost:8080/booking/v1/cargos/ABC123/request_routes
    
    • change destination
    curl -d '{}' -X POST http://localhost:8080/booking/v1/cargos/ABC123/change_destination
    
    • Get location code
    curl -X GET http://localhost:8080/booking/v1/locations
    
    • Register incident
    curl -d '{"completion_time":"2016-03-21T19:50:24Z", "tracking_id":"ABC123", "voyage":"V100", "location":"SESTO", "event_type":"Receive"}' -X POST http://localhost:8080/handling/v1/incidents
    
    • tracking
    curl -X GET http://localhost:8080/tracking/v1/cargos/ABC123
    

apigateway

  • prepare
go get github.com/hashicorp/consul/api
usage of Go-Kit Api Gateway
  1. run addsvc
cd ${GOPATH}/src/08.go-kit/04.addsvc;
make pb thrift;
cd ${GOPATH}/src/08.go-kit/04.addsvc/cmd/addsvc;
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
curl -XPOST -d'{"a":7,"b":4}' http://localhost:7081/sum
  1. run stringsvc
cd ${GOPATH}/src/08.go-kit/03.stringsvc3;
go run . -listen :8081
curl -XPOST -d'{"s":"mytest"}' http://localhost:8081/count
curl -XPOST -d'{"s":"mytest"}' http://localhost:8081/uppercase
  1. run consul
cd $HOME; wget https://releases.hashicorp.com/consul/1.6.2/consul_1.6.2_linux_amd64.zip;
unzip consul_1.6.2_linux_amd64.zip; cp consul /usr/local/bin/;

cd ${GOPATH}/src/08.go-kit/07.apigateway;
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
  1. starting api gateway
cd ${GOPATH}/src/08.go-kit/07.apigateway;
go run main.go -consul.addr :8500

curl -XPOST -d'{"a":"7","b":"4"}' http://localhost:8000/addsvc/concat
curl -XPOST -d'{"a":7,"b":4}' http://localhost:8000/addsvc/sum
curl -XPOST -d'{"s":"mytest"}' http://localhost:8000/stringsvc/count
curl -XPOST -d'{"s":"mytest"}' http://localhost:8000/stringsvc/uppercase

Example : napodate

  • run napodate service
cd ${GOPATH}/src/08.go-kit/08.napodate/cmd;
go run main.go
  • test napodate service
curl http://localhost:8080/get
curl http://localhost:8080/status
curl -XPOST -d '{"date":"32/12/2020"}' http://localhost:8080/validate
curl -XPOST -d '{"date":"12/12/2021"}' http://localhost:8080/validate

kitlog

cd ${GOPATH}/src/github.com/go-kit/kit/cmd/kitgen
go get .
go install
kitgen -h
  • autogenerate endpoints, http, service with kitgen
cd ${GOPATH}/src/08.go-kit/09.kitloc;
mkdir hello; cd hello
kitgen ../service.go
  • run zipkin server
docker run -d -p 9411:9411 openzipkin/zipkin
  • run hello
go get go.opencensus.io
go get contrib.go.opencensus.io/exporter/zipkin

cd ${GOPATH}/src/08.go-kit/09.kitloc;
go run cmd/hello/main.go
  • test hello
export PORT=40775
curl -X POST -d '{}' http://localhost:${PORT}/hello
curl -X POST -d '{"FirstName":"John"}' http://localhost:${PORT}/hello
curl -X POST -d '{"LastName":"Doe"}' http://localhost:${PORT}/hello
curl -X POST -d '{"FirstName":"John","LastName":"Doe"}' http://localhost:${PORT}/hello

Micro-services Using Go-kit: REST Endpoint

  • Endpoint URL Format : /lorem/{type}/{min}/{max}
  • prepare
go get github.com/go-kit/kit
go get github.com/drhodes/golorem
go get github.com/gorilla/mux
  • run
cd ${GOPATH}/src/08.go-kit/10.lorem
go run lorem.d/main.go 
  • test
curl -X POST http://localhost:8080/lorem/word/1/50
curl -X POST http://localhost:8080/lorem/sentence/1/50
curl -X POST http://localhost:8080/lorem/paragraph/1/50
curl -X POST http://localhost:8080/lorem/nomention/1/50

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL