The target
- Build the simplest microservices architecture
Introduction to the
GO Kit is a toolkit for building microservices that helps us solve common problems in distributed systems and application architectures, allowing us to focus more on the business logic itself.
Download address
Go-kit address: github.com/go-kit/kit Procedure code in this article: github.com/CalvinQiang… Submission point: Build the basic three-layer framework of Go-Kit
Go-kit three-tier architecture
- Transport
Responsible for the logic related to HTTP, GRPC, THRIFT, etc
- Endpoint
Define Request and Response formats, as well as various middleware
- Service
Business class, interface
Prepare the corresponding structure
1.1 Preparing the go.mod File
module com.calvin.service
require github.com/go-kit/kit v010.. 0
go 1.14
Copy the code
1.2 Complete the Server Layer-Service Logic
- Here we mainly define a UserService service and a GetName method (to get the user’s name based on the userId passed in).
package service
type IUserService interface {
GetName(userId int) string
}
type UserService struct{}func (s UserService) GetName(userId int) string {
if userId == 101 {
return "calvin"
}
return "guest"
}
Copy the code
1.3 Complete the Endpoint layer – encapsulate requests and responses
- Here,
endpoint.Endpoint
It actually returns a function type. - The Endpoint represents a single PRC method, which is the bridge between the server and the client.
- In this RPC method, we encapsulate the user’s request and the corresponding return structure
package endpoint
import (
"com.calvin.service/service"
"context"
"github.com/go-kit/kit/endpoint"
)
type UserRequest struct {
Uid int `json:"uid"`
}
type UserResponse struct {
Result string `json:"result"`
}
func GetUserEndPoint(userService service.IUserService) endpoint.Endpoint {
return func(ctx context.Context, request interface{}) (response interface{}, err error) {
r := request.(UserRequest)
result := userService.GetName(r.Uid)
return UserResponse{Result: result}, nil}}Copy the code
1.3 Complete the Transport layer – responsible for the logic related to HTTP, GRPC, THRIFT, etc
- Here we define how the data should be interacted, which can be in JSON/XML/binary format
- The DecodeUserRequest method is used to parse the data sent by the user
- The EncodeUserResponse method is used to encode the data sent to the customer
package transport
import (
"com.calvin.service/endpoint"
"context"
"encoding/json"
"errors"
"net/http"
"strconv"
)
func DecodeUserRequest(ctx context.Context, r *http.Request) (interface{}, error) {
// We get the user ID directly from params in the URL
// The request format is similar to: http://127.0.0.1? uid=101
if r.URL.Query().Get("uid") != "" {
uid, _ := strconv.Atoi(r.URL.Query().Get("uid"))
return endpoint.UserRequest{Uid: uid}, nil
}
return nil, errors.New("Parameter error")}func EncodeUserResponse(ctx context.Context, w http.ResponseWriter, response interface{}) error {
// Place the return body in json format
w.Header().Set("Content-type"."application/json")
return json.NewEncoder(w).Encode(response)
}
Copy the code
1.4 Write the main program
package main
import (
"com.calvin.service/endpoint"
"com.calvin.service/service"
"com.calvin.service/transport"
httpTransport "github.com/go-kit/kit/transport/http"
"net/http"
)
func main(a) {
user := service.UserService{}
endPoint := endpoint.GetUserEndPoint(user)
// Construct a service that implements http.handler and wraps the endpoint layer
serverHandler := httpTransport.NewServer(endPoint, transport.DecodeUserRequest, transport.EncodeUserResponse)
// Listen on the port, and use serverHandler to handle subsequent requests
_ = http.ListenAndServe(": 8080", serverHandler)
}
Copy the code
1.5 Running Programs
- Request http://127.0.0.1:8080, we will find that the request has an error in the parsing process, has not entered the server layer
- Request to http://127.0.0.1:8080? uid=1999
- Request to http://127.0.0.1:8080? uid=101
At the end
Welcome to pay attention to my official account [Heart of singularity]. I will regularly take notes on my study. Let’s work hard together