Gokit is microservice tookit and use Service/Endpoint/Transport to strict separation of concerns design. This talk to use go-kit develop microservice application integrate with istio, jaeger prometheus, etc service and deploy on Kubernetes.
14. Problem solved
1. Team are blocked on other teams, can’t make progress
2. Product velocity stalled
15. Problem cause
1. Testing becomes really hard
2. Build pipelines
3. Requires dev/ops culture: devs deploy and operate their
work
4. Monitoring and instrumentation - tailing logs?
5. Distributed tracing
6. Security
16. Go-kit goals
1. Make Go a first-class citizen for business logic
2. Microservice architecture
3. RPC as the messaging pattern
4. Provide best practices, idioms, examples and patterns
Golang UK Conference 2015 - Peter Bourgon - Go Kit A Toolkit for Microservices - https://www.youtube.com/watch?v=aL6sd4d4hxk
17. Go-kit non-goals
1. Be a turnkey solution
2. Require any specific infrastructure
3. Have opinions about orchestration, configuration, etc.
Golang UK Conference 2015 - Peter Bourgon - Go Kit A Toolkit for Microservices - https://www.youtube.com/watch?v=aL6sd4d4hxk
19. Add service: service and implement
type Addsvc interface {
sum(a, b int64) (int64, error)
}
type SumRequest struct {
A int64 `json:"a"`
B int64 `json:"b"`
}
func (s *addService) sum(a, b int64) (int64, error) {
return a + b, nil
}
type addService struct{}
20. Transports: Implement HTTP handler
func (s *addService) ServeHTTP(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case http.MethodPost:
var req SumRequest
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
if res, err := s.sum(req.A, req.B); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
} else {
fmt.Fprint(w, res)
}
}
}
21. main.go
func main() {
http.ListenAndServe(":8888", &addService{})
}
$ go run main.go
$ curl -X "POST" "http://localhost:8888" -d '{ "a": 3, "b": 34}'
37
Addservice an HTTP handler, we can pass it to ListenAndServe
23. Logging
func main() {
log.Printf("listening on :8888")
log.Fatal(http.ListenAndServe(":8888", &addService{}))
}
$ go run main.go
2019/11/29 15:24:29 listening on :8888
2019/11/29 15:24:32 [::1]:52200: POST / 200
$ curl -X "POST" "http://localhost:8888" -d '{ "a": 3, "b": 34}'
37
24. Endpoint
Endpoint is the fundamental building block of servers and clients.It represents
a single RPC method.
type Endpoint func(request) (response)
type Endpoint func(ctx context.Context, request interface{}) (response interface{}, err error)
25. Add service endpoint
Create endpoint by add service constructor and keep add service pure
func MakePostSumEndpoint(s addService) endpoint.Endpoint {
return func(_ context.Context, request interface{}) (response interface{}, err error) {
p := request.(SumRequest)
return s.sum(p.A, p.B)
}
}
27. Logging middleware
Create endpoint by add service constructor and keep add service pure
func loggingMiddleware(next endpoint.Endpoint) endpoint.Endpoint {
return func(ctx context.Context, request interface{}) (response interface{}, err error) {
begin := time.Now()
defer func() {
log.Printf("request took %s", time.Since(begin))
}()
return next(ctx, request)
}
}
s := addService{}
var e endpoint.Endpoint
e = MakePostSumEndpoint(s)
e = loggingMiddleware(e)
28. Http transport
Go kit comes with handy HTTP transport. NewServer constructs a new server,
which implements http.Handler and wraps the provided endpoint.
func NewServer(
e endpoint.Endpoint,
dec DecodeRequestFunc,
enc EncodeResponseFunc,
options ...ServerOption,
) *Server { ... }
postSum := httptransport.NewServer(e, decodeHTTPSumRequest, httptransport.EncodeJSONResponse)
r := mux.NewRouter()
r.Methods(http.MethodPost).Handler(postSum)
log.Printf("listening on :8888")
log.Fatal(http.ListenAndServe(":8888", r))
29. Design — How is a Go kit microservice modeled?
Transport
Endpoint
Service
https://gokit.io/faq/#design-mdash-how-is-a-go-kit-microservice-modeled
30. Go + microservices = Go kit - Speaker Deck - https://speakerdeck.com/peterbourgon/go-plus-microservices-equals-go-kit?slide=78
31. Golang UK Conference 2015 - Peter Bourgon - Go Kit A Toolkit for Microservices - https://youtu.be/aL6sd4d4hxk?t=1022
32. ● Authentication: Basic, casbin, JWT.
● Circuit Breaker: Hystrix, GoBreaker, and HandyBreaker.
● Logging: Provide an interface for structured logging. Recognizes
that logs are data. They need context and semantics to be useful
for analysis. Supported formats are logfmt and JSON.
● Metrics: Provides uniform interfaces for service instrumentation.
Comes with counters, gauges, and histograms. Has adapters for
CloudWatch, Prometheus, Graphite, DogStatsD, StatsD, expvar,
and more.
● Rate Limit: Uses Go's token bucket implementation.
● Service Discovery: Consul, DNS SRV, etcd, Eureka, ZooKeeper,
and more.
● Tracing: OpenCensus, OpenTracing, and Zipkin.
● Transport: AMQP, AWS Lambda, gRPC, HTTP, NATS, Thrift.
.
├── auth
├── circuitbreaker
├── cmd
├── endpoint
├── examples
├── log
├── metrics
├── ratelimit
├── sd
├── tracing
├── transport
├── util
├── .build.yml
├── .gitignore
├── .travis.yml
36. What Is a Service Mesh? By NGINX
1. Container orchestration framework
2. Services and instances (Kubernetes pods).
3. Sidecar proxy
4. Service discovery
5. Load balancing
6. Encryption
7. Authentication and authorization
8. Support for the circuit breaker pattern
What Is a Service Mesh? - NGINX - https://www.nginx.com/blog/what-is-a-service-mesh/
37. The control plane in a service mesh distributes configuration
across the sidecar proxies in the data plane
What Is a Service Mesh? - NGINX - https://www.nginx.com/blog/what-is-a-service-mesh/
42. Reference
1. Go + Microservices = Go Kit [I] - Peter Bourgon, Go Kit
2. Go kit
3. https://gokit.io/faq/
4. cage1016/gokitistio8s
5. cage1016/gk
6. What Is a Service Mesh?