Home >Backend Development >Golang >Let's talk about how to use Golang to implement Sidecar service
Sidecar is a solution in a distributed system that lacks proper support for inter-service calls due to the service topology or some reasons. In inter-service calls, Sidecar acts as a proxy, distributing all incoming and outgoing requests to other services, load balancing, traffic shaping, security processing, etc. The topic this time is the implementation of Sidecar in Golang. Next, this article will introduce in detail the importance of Sidecar construction, how Sidecar works, and how to use Golang to implement Sidecar services.
First of all, we need to clarify the role of Sidecar. In a service-oriented architecture, many services need to call each other. In this case, some problems will arise. For example, if service A wants to access service B, then it needs to develop a calling client. In this client, we need to determine the calling method, address, port, protocol, etc. The more services there are, the more complicated the calling relationship will be. With Sidecar, we only need to deploy a Sidecar in each service instance, which can intercept all requests and process the requests accordingly, thus eliminating the complexity of access relationships and providing a more simplified service. Calling method.
The main workflow of Sidecar is as follows:
Step 1: Sidecar is responsible for intercepting all client requests.
Step 2: Sidecar converts the intercepted request into a normal protocol, and then sends the request to the service.
Step 3: When the service response arrives, Sidecar extracts the response from the service, performs reverse conversion, and returns the response to the client.
An architecture diagram with Sidecar function is as follows:
First, you need to determine the client that many services need to call The name and address as well as the port in order to make the call, we call this service discovery. The specific steps are as follows:
Step 1: Service discovery: Sidecar needs to learn the service discovery information of the current application from the configuration center or registration center. This service discovery information records all the services of the application and their address and port information. This information can be used to allow Sidecar to complete the service call.
Step 2: Proxy settings: Sidecar needs to intercept requests and perform proxy settings. If the client request has a corresponding identifier, the request is converted into a normal protocol format and sent to the service. If not, the Sidecar rejects it.
Through the above steps, you can implement the Sidecar service in Golang. For code implementation, you can refer to the following process:
//Define the service discovery address
const discoverUrl = "https://srv. discover.local/path"
type ServiceDiscoveryClient interface{
FindService(serviceName string) ([]string, error)
}
func NewServiceDiscoveryClient() ServiceDiscoveryClient {
return &serviceDiscoveryClient{discoverUrl}
}
type serviceDiscoveryClient struct{
url string
}
func (c* serviceDiscoveryClient) FindService(serviceName string) ([]string, error){
//Request the address and port of the service from the service discovery center
}
//Define the proxy Port and service name
const proxyPort int = 1000
const serviceName string = "my-service"
type Proxy interface {
Start() error
Stop() error
}
func NewProxy(serviceDiscoveryClient ServiceDiscoveryClient) Proxy {
//Initialize Sidecar proxy
}
type proxy struct {
serviceDiscoveryClient ServiceDiscoveryClient
}
func (p* proxy) Start() error {
//Start listening to the port and intercept requests
}
func (p* proxy) Stop() error {
//Stop listening port
}
$ go run main.go
The above is the detailed content of Let's talk about how to use Golang to implement Sidecar service. For more information, please follow other related articles on the PHP Chinese website!