MSE supports Go language traffic protection


With the widespread adoption of the Go language and cloud native, the Go language is increasingly used in microservice scenarios, and the demand for governance, flow restriction, and degradation of the Go language microservices is also increasing.

In the Go language, although the community has provided flow limiting libraries such as, one is that there is insufficient support for multiple languages, and only Go is supported. Secondly, in terms of functionality, flow limiting degradation will be subdivided into flow control, isolation, fuse, hotspot, and other functions, and dynamic configuration is not supported, which is not perfect in terms of functional support.

Alibaba Cloud's microservice engine (MSE), combined with Sentinel, brings microservice governance capabilities to Go language and Java language applications. This article will introduce how to use current limiting and degrading capabilities in Go language microservice applications.

What is current limiting degradation

In microservice applications, current limiting degradation is mainly divided into three steps:

• Target: For what kind of traffic

• Strategy: Current limiting degradation strategy

• FallbackAction: The behavior after triggering

For example, for the order creation interface (Target), we limit the request to 1000 QPS (Strategy). After triggering the flow restriction, the request returns an exception (FallbackAction).

Alibaba Cloud MSE supports defining resources through the open source Sentinel method, obtaining and applying current limiting and degrading rules from MSE. The overall access is as follows:

How to use MSE current limiting degradation

Application access

1. Download the MSE Go SDK [1]

A. You can refer to the examples in the example directory of the SDK to access.

2. In the go.mod file, add the following dependency declaration:

Application Deployment

In the cloud native deployment method, the usual deployment specification is a 12 factor [2]. Regarding configuration, it is recommended to store the application's configuration in environment variables. In this way, when an application is deployed, it only needs to switch different environment variables to access different environments.

Similarly, MSE-Go-SDK also recommends that you access MSE through environment variables, which are as follows:

After accessing according to the above steps, you can see the applications we have accessed on the application list page:

Resource Definition

At the beginning of the article, we mentioned that current limiting downgrade=Target+Strategy+FallbackAction. So the first step is to define a Target.

• Sentinel defines resources

MSE supports resources defined by users through Sentinel, just by wrapping the business logic with the following code blocks:

Of course, in daily business development, developers often provide services through microservice frameworks, such as dubbo go, Gin, and gRPC. Naturally, we hope to be able to register these services as resources:

• Dubbo-go method for defining resources

Simply introduce the dubbo go adapter to automatically register resources to MSE:

• The Dubbo application introduces the Dubbo adapter in the form of an import package in the code, where the init() function will automatically inject relevant filters. The Dubbo Go version requires ≥ 1.3.0. Sentinel Dubbo adapter automatically counts all provider and consumer calls.

• gRPC application access

• The gRPC application introduces the interceptor provided by Sentinel into the initialization code of gRPC. Sentinel provides both unary and streaming interceptors for both the server and client. The above code takes the server side as an example. The default flow limiting processing logic is to return a BlockError from Sentinel. You can also provide custom fallback processing logic when creating an interceptor.

• Gin Web application access

• Gin Web applications introduce SentinelMiddleware into Gin's initialization code. Sentinel will perform statistics on each API route, and the resource name is similar to GET:/foo/: id. The default flow limiting processing logic is to return a 429 (Too Many Requests) error code.

• Micro application access

• Introduce the wrapper provided by Sentinel into the initialization code of Go-Micro. Sentinel provides wrappers for both Go-Micro Server and Client. The above code takes the server side as an example. The buried point will extract the service method as the resource name by default, and the default flow control processing logic is to return a BlockError from Sentinel. You can also provide custom fallback processing logic when creating a wrapper.

How to configure flow restriction degradation rules

After accessing through the above method, you can configure rules such as flow control rules, isolation rules, fuse rules, and hotspot rules:

• View apps

After accessing, you can see the applications we have accessed on the application list page:

On the application details page, you can also see the overview data of the application:

You can view the resources registered in the code:

• Configure flow control rules

You can set flow control rules for each resource:

After setting the flow control rules, you can see that the rejected QPS increases and the QPS decreases:

By selecting flow control protection, you can preset the QPS traffic that the service can withstand. When the traffic reaches the set threshold, it immediately intercepts requests that exceed the limit, preventing applications from being overwhelmed by instantaneous traffic peaks.

• Configure isolation rules

Selecting isolation protection can ensure the stability of the system by controlling the number of concurrent threads on which an interface or dependency exists. It is generally applicable to scenarios where internal or downstream dependencies of applications become unstable, such as slow SQL, and downstream applications have longer response times.

• Configure fusing rules

By selecting fuse protection, you can monitor the response time or abnormal proportion of internal or downstream dependencies within the application, and immediately reduce the priority of downstream dependencies when the specified threshold is reached. Within the specified time, the system will not invoke this unstable resource to avoid application impact, thereby ensuring high application availability. After the specified time has elapsed, the call to the resource will be resumed.

• Configure hotspot rules

Select hotspot parameter protection, and AHAS will analyze statistical parameters, i.e. parameters with higher invocation times during resource invocation, and limit the flow of resource invocations containing hotspot parameters according to configured hotspot rules to protect system stability. Hotspots are frequently accessed data. For example, in the following scenario, it is necessary to count the Top data with the highest access frequency among a certain hotspot data and restrict its access Limit the product IDs that are purchased most frequently over a period of time to prevent a large number of requests to the database due to cache breakdown.

• Restrict user IDs that are frequently accessed over a period of time to prevent malicious order swiping.

MSE Micro Service Governance Plan

The OpenSergo open source project, in conjunction with the Sentinel project, is developing and improving current limiting and degrading standards to facilitate unified microservice governance for different language applications through a unified control surface.

Alibaba Cloud's microservice engine (MSE) will also gradually support the OpenSergo standard, enabling microservice developers to use OpenSergo to uniformly govern microservice applications in different languages.

At the same time, Alibaba Cloud MSE will also explore microservice governance, exploring the implementation of traffic governance, flow restriction degradation, database governance, message governance, and other functions, bringing a full lifecycle microservice governance to microservice developers.

Related Articles

Explore More Special Offers

  1. Short Message Service(SMS) & Mail Service

    50,000 email package starts as low as USD 1.99, 120 short messages start at only USD 1.00

phone Contact Us