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 consul, zipkin, prometheus, etc service and deploy on Kubernetes.
22. Problem solved
1. Team are blocked on other teams, can’t make progress
2. Product velocity stalled
23. 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
24. 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
25. 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
27. type Addsvc interface {
sum(a, b int64) (int64, error)
}
type SumRequest struct {
A int64 `json:"a"`
B int64 `json:"b"`
}
Add service: service and implement
func (s *addService) sum(a, b int64) (int64, error) {
return a + b, nil
}
type addService struct{}
28. 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)
}
}
}
29. main
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
31. 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
32. Endpoint
type Endpoint func(request) (response)
Endpoint is the fundamental building block of servers and clients.It represents a
single RPC method.
type Endpoint func(ctx context.Context, request interface{}) (response interface{}, err error)
33. Add service endpoint
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)
}
}
Create endpoint by add service constructor and keep add service pure
35. Logging middleware
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)
Create endpoint by add service constructor and keep add service pure
36. Http transport
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))
Go kit comes with handy HTTP transport. NewServer constructs a new server,
which implements http.Handler and wraps the provided endpoint.
37. Design — How is a Go kit microservice modeled?
https://gokit.io/faq/#design-mdash-how-is-a-go-kit-microservice-modeled
Transport
Endpoint
Service
38. Go + microservices = Go kit - Speaker Deck - https://speakerdeck.com/peterbourgon/go-plus-microservices-equals-go-kit?slide=78
39. Golang UK Conference 2015 - Peter Bourgon - Go Kit A Toolkit for Microservices - https://youtu.be/aL6sd4d4hxk?t=1022
40. ● 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
44. 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/
45. 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/
50. Reference
1. Go + Microservices = Go Kit [I] - Peter Bourgon, Go Kit
2. Go kit
3. https://gokit.io/faq/
4. cage1016/gokitconsulk8s
5. cage1016/gk
6. What Is a Service Mesh?