In 2020, Sentinel launched sentinel-Golang, a Go native version, which continues to make breakthroughs in the field of cloud native. This paper will illustrate how to integrate Nacos in Sentinel-Golang, make it an external dynamic data source, store flow control rules in Nacos, and realize dynamic real-time updating of rules.

This paper is mainly divided into two parts:

  1. Sentinel flow control rules are defined in the code to achieve flow limiting effect.
  2. Sentinel flow control rules were defined in nacOS configuration center to realize current limiting effect and dynamically update rules in NACOS to realize dynamic flow control.

The following is a detailed introduction to relevant background knowledge.

1. Sentinel

With the popularity of microservices, stability between services becomes increasingly important. Sentinel takes flow as the entry point to protect the stability of service from multiple dimensions such as flow control, fusing downgrading and system load protection.

Sentinel has the following characteristics:

  • Rich application scenarios: Sentinel has undertaken the core scenarios of Alibaba’s double Eleven traffic drive in the past 10 years, such as SEC killing (i.e. burst traffic control within the range of system capacity), message peaking and valley filling, cluster flow control, real-time fusing of unavailable downstream applications, etc.
  • Complete real-time monitoring: Sentinel also provides real-time monitoring capabilities. From the console, you can see a summary of the performance of a single machine-by-second data, or even a cluster of less than 500 machines, for accessing the application.
  • Extensive Open source ecosystem: Sentinel provides out-of-the-box integration modules with other open source frameworks/libraries, such as Spring Cloud, Dubbo, and gRPC. You can quickly access Sentinel by introducing the appropriate dependencies and simple configuration.
  • Sophisticated SPI extension points: Sentinel provides an easy-to-use, sophisticated SPI extension interface. You can quickly customize the logic by implementing an extension interface. For example, customize rule management and adapt dynamic data sources.

1.1 History of Sentinel

  • Sentinel was born in 2012 and its main function is inlet flow control.
  • From 2013 to 2017, Sentinel developed rapidly within Alibaba Group and became a basic technology module, covering all core scenarios. Sentinel has thus accumulated a large number of traffic aggregation scenarios and production practices.
  • Sentinel became open source in 2018 and continues to evolve.
  • In 2019, Sentinel has been exploring multi-language extensions with C++ native versions and Envoy cluster traffic control support.
  • In 2020, Sentinel will launch the Go native version, looking forward to further breakthroughs in the cloud native space.https://github.com/alibaba/sentinel-golang

2. Nacos

Nacos is a platform for dynamic service discovery, configuration management and service management that makes it easier to build cloud native applications. Nacos is derived from Alibaba’s internal ConfigServer and Diamond, and is their open source implementation. The alibaba soft load team has experienced ten years of experience in this field after experiencing the peak traffic on Singles Day and the super large capacity of Alibaba’s economy, which ensures its stability and functionality.

(Sentinel-Go integration with Nacos Dynamic data source architecture)

At present, the strategies of stream limiting and fusing in Sentinel are implemented based on rules. The purpose of dynamic data source extension is to realize dynamic update of the loading and updating of rule data through some configuration central middleware (such as NACOS, ETCD, Conful,etc.).

3. Sentinel-go Current limiting Demo

When NACOS is not integrated, rules are defined within the code and no external data sources are used.

3.1 installation

go get github.com/alibaba/sentinel-golang

3.2 the Demo sample

The use of Sentinel is divided into the following steps:

  1. Configure and initialize Sentinel
  2. Buried points (Defining resources)
  3. Configuration rules
package main

import (
	"fmt"
	"log"
	"math/rand"
	"time"

	sentinel "github.com/alibaba/sentinel-golang/api"
	"github.com/alibaba/sentinel-golang/core/base"
	"github.com/alibaba/sentinel-golang/core/flow"
	"github.com/alibaba/sentinel-golang/util"
)

func main(a) {
	// We should initialize Sentinel first.
	err := sentinel.InitDefault()
	iferr ! =nil {
		log.Fatalf("Unexpected error: %+v", err)
	}

	_, err = flow.LoadRules([]*flow.FlowRule{
		{
			Resource:        "some-test",
			MetricType:      flow.QPS,
			Count:           10,
			ControlBehavior: flow.Reject,
		},
	})
	iferr ! =nil {
		log.Fatalf("Unexpected error: %+v", err)
		return
	}

	ch := make(chan struct{})

	for i := 0; i < 10; i++ {
		go func(a) {
			for {
				e, b := sentinel.Entry("some-test", sentinel.WithTrafficType(base.Inbound))
				ifb ! =nil {
					// Blocked. We could get the block reason from the BlockError.
					time.Sleep(time.Duration(rand.Uint64()%10) * time.Millisecond)
				} else {
					// Passed, wrap the logic here.
					fmt.Println(util.CurrentTimeMillis(), "passed")
					time.Sleep(time.Duration(rand.Uint64()%10) * time.Millisecond)

					// Be sure the entry is exited finally.
					e.Exit()
				}

			}
		}()
	}
	<-ch
}
Copy the code

The official expmale:github.com/alibaba/sen…

4. Sentinel-go integration with Nacos

Sentinel-go integrates with Nacos for external dynamic data source functionality.

4.1 deployment Nacos

4.1.1 Version selection

You can find descriptions of the features supported by each release in Nacos’s Release Notes and blog, and the current recommended stable release is 1.3.1.

4.1.2 Environment Preparations

Nacos relies on the Java environment to run. If you build and run Nacos from code and need to configure the Maven environment for this, make sure you install it in one of the following versions:

  1. The 64-bit OS supports Linux, Unix, Mac, and Windows. Linux, Unix, and Mac are recommended.
  2. 64-bit JDK 1.8+; Download & Configure.
  3. Maven 3.2 x +; Download & Configure.

4.1.3 Download the source code or install the package

You can get Nacos both from source code and distribution packages.

Download the source code from Github

git clone https://github.com/alibaba/nacos.git
cd nacos/
mvn -Prelease-nacos -Dmaven.test.skip=true clean install -U  
ls -al distribution/target/
// change the $version to your actual path
cd distribution/target/nacos-server-$version/nacos/bin
Copy the code

Download the compressed package after compilation

You can download the nacos-server-$version.zip package from the latest stable version.

Unzip nacos-server-$version.zip or tar -xvf nacos-server-$version.tar.gz CD nacos/binCopy the code

4.1.4 Starting the Server

Linux/Unix/Mac startup command (standalone stands for standalone mode, not cluster mode): Sh -m standalone If you are using an Ubuntu system or are running the script and an error message is displayed indicating that the [[symbol cannot be found, try the following operation: bash startup.sh -m standalone

Run the CMD startup. CMD command or double-click the startup.

Deployment of successful visit http://127.0.0.1:8848/nacos * * * * user name/password: nacos/nacos

4.2 Sentinel flow limiting configuration to Nacos

  1. Log in to Nacos Web
  2. In configuration Management, create a configuration
  3. DataId =flow,group=sentinel-go
  4. Paste the sample data source into the configuration content.

4.2.1 Example of external data Source of Nacos

This sample is a Demo configuration of flow control. When the number of concurrent traffic is greater than 100, it is rejected.

For details about the configuration, see github.com/alibaba/sen…

[{"resource": "some-test"."metricType": 1."count": 100.0."controlBehavior":0}]Copy the code

After the configuration is complete, the corresponding traffic limiting configuration is displayed in the NACOS configuration list.

4.3 Nacos data source integration

4.3.1 Creating a Project

  1. version
    1. The Sentinel-Golang version uses 0.6.0 and NACOS-SDK-GO uses 1.0.0
  2. go.mod
module sentinel-go-nacos-example

go 1.13

require (
	github.com/alibaba/sentinel-golang v06.. 0
	github.com/nacos-group/nacos-sdk-go v1. 0. 0
)

Copy the code
  1. main.go
package main

import (
	"fmt"
	"math/rand"
	"sync/atomic"
	"time"

	sentinel "github.com/alibaba/sentinel-golang/api"
	"github.com/alibaba/sentinel-golang/core/base"
	"github.com/alibaba/sentinel-golang/ext/datasource/nacos"
	"github.com/alibaba/sentinel-golang/util"
	"github.com/nacos-group/nacos-sdk-go/clients"

	"github.com/alibaba/sentinel-golang/ext/datasource"
	"github.com/nacos-group/nacos-sdk-go/common/constant"
)

type Counter struct {
	pass  *int64
	block *int64
	total *int64
}

func main(a) {
	// Flow counter, for flow control to print logs more intuitive, independent of integrated NACOS data source.
	counter := Counter{pass: new(int64), block: new(int64), total: new(int64)}

	/ / nacos server address
	sc := []constant.ServerConfig{
		{
			ContextPath: "/nacos",
			Port:        8848,
			IpAddr:      "127.0.0.1",}}// Set parameters related to nacos Client. For details, see https://github.com/nacos-group/nacos-sdk-go
	cc := constant.ClientConfig{
		TimeoutMs: 5000,}// Generate the nacOS config client.
	client, err := clients.CreateConfigClient(map[string]interface{} {"serverConfigs": sc,
		"clientConfig":  cc,
	})
	iferr ! =nil {
		fmt.Printf("Fail to create client, err: %+v", err)
		return
	}
	// Register the flow control rule Handler
	h := datasource.NewFlowRulesHandler(datasource.FlowRuleJsonArrayParser)
	// Create the NacosDataSource data source
	// Sentinel-go corresponds to the group that creates the configuration file in NACOS
	// Flow corresponds to the dataId that creates the configuration file in NACOS
	nds, err := nacos.NewNacosDataSource(client, "sentinel-go"."flow", h)
	iferr ! =nil {
		fmt.Printf("Fail to create nacos data source client, err: %+v", err)
		return
	}
	// nacOS data source initialization
	err = nds.Initialize()
	iferr ! =nil {
		fmt.Printf("Fail to initialize nacos data source client, err: %+v", err)
		return
	}
	// Start statistics
	go timerTask(&counter)

	// Simulate traffic
	ch := make(chan struct{})
	for i := 0; i < 10; i++ {
		go func(a) {
			for {
				atomic.AddInt64(counter.total, 1)
				//some-test corresponds to resource in the nacOS flow control configuration file
				e, b := sentinel.Entry("some-test", sentinel.WithTrafficType(base.Inbound))
				ifb ! =nil {
					atomic.AddInt64(counter.block, 1)
					// Blocked. We could get the block reason from the BlockError.
					time.Sleep(time.Duration(rand.Uint64()%10) * time.Millisecond)
				} else {
					atomic.AddInt64(counter.pass, 1)
					time.Sleep(time.Duration(rand.Uint64()%10) * time.Millisecond)

					// Be sure the entry is exited finally.
					e.Exit()
				}

			}
		}()
	}
	<-ch
}

//statistic print
func timerTask(counter *Counter) {
	fmt.Println("begin to statistic!!!")
	var (
		oldTotal, oldPass, oldBlock int64
	)
	for {
		time.Sleep(1 * time.Second)
		globalTotal := atomic.LoadInt64(counter.total)
		oneSecondTotal := globalTotal - oldTotal
		oldTotal = globalTotal

		globalPass := atomic.LoadInt64(counter.pass)
		oneSecondPass := globalPass - oldPass
		oldPass = globalPass

		globalBlock := atomic.LoadInt64(counter.block)
		oneSecondBlock := globalBlock - oldBlock
		oldBlock = globalBlock
		fmt.Println(util.CurrentTimeMillis()/1000."total:", oneSecondTotal, " pass:", oneSecondPass, " block:", oneSecondBlock)
	}
}

Copy the code

4.3.2 Running results

4.3.3 Dynamically Updating traffic Limiting Configurations

During project startup, modify flow control configuration parameters in NACOS. Will count from 100 – > 400

You can see that the reloadrule log is printed and the flow control is dynamically 100->400

conclusion

Using NACOS as an external dynamic data source in Sentinel-Go, you only need to change the part that declares and loads the Rule to read from the NACOS data source.

In this article, only the integration of flow control is described. The integration of fuse,warmup, and hotspot parameters is also described. You only need to modify the configuration content as required

Reference address: github.com/alibaba/sen…

Key code:

	h := datasource.NewFlowRulesHandler(datasource.FlowRulesJsonConverter)
	nds, err := nacos.NewNacosDataSource(client, "sentinel-go"."flow", h)
	iferr ! =nil {
		fmt.Printf("Fail to create nacos data source client, err: %+v", err)
		return
	}
	err = nds.Initialize()
	iferr ! =nil {
		fmt.Printf("Fail to initialize nacos data source client, err: %+v", err)
		return
	}
Copy the code

A link to the

  • Demo address: github.com/alibaba/sen…
  • Sentinel-golang:github.com/alibaba/sen…
  • Sentinel golang community exchange group: 30150716,23339422
  • Nacos: Nacos. IO/useful – cn/index…
  • Nacos Nail Community Exchange Group: 30438813, 23191211(Nacos Golang Ecological Exchange Group)
  • Nacos-sdk-go Project address: github.com/nacos-group…

Author’s brief introduction

Binbin Zhang Github account: Sanxun0325, Nacos Commiter,Sentinel-Golang Contributor, now works in OpenJaw micro service team. Currently, I am mainly responsible for the development of Nacos and Sentinel-Golang community-related projects, as well as the promotion and integration of Nacos in the Golang micro-service ecosystem.

“Alibaba Cloud originator focuses on micro-service, Serverless, container, Service Mesh and other technical fields, focuses on the trend of cloud native popular technology, large-scale implementation of cloud native practice, and becomes the public account that most understands cloud native developers.”