Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to understand Spring Cloud alibaba Gateway

2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "how to understand Spring Cloud alibaba Gateway". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's ideas to study and learn "how to understand Spring Cloud alibaba Gateway".

Spring cloud alibaba integrates his internal open source Sentinel circuit breaker current limiting framework.

Sentinel introduction

With the popularity of microservices, the stability between services is becoming more and more important. Taking the flow as the starting point, Sentinel protects the stability of the service from many dimensions, such as flow control, circuit breaker degradation, system load protection and so on.

Sentinel has the following characteristics:

Rich application scenarios: Sentinel takes on the core scenarios of Alibaba's Singles Day traffic promotion in the past 10 years, such as second kill (that is, burst traffic control within the system capacity), message peak cutting and valley filling, cluster flow control, real-time fuse downstream unavailable applications, and so on. Complete real-time monitoring: Sentinel also provides real-time monitoring function. You can see the second-rate data of a single machine connected to the application in the console, and even the summary operation of less than 500 clusters.

Broad open source ecology: Sentinel provides out-of-the-box integration modules with other open source frameworks / libraries, such as Spring Cloud, Dubbo, and gRPC. You only need to introduce the appropriate dependencies and make a simple configuration to quickly access Sentinel.

Perfect SPI extension point: Sentinel provides easy-to-use and perfect SPI extension interface. You can quickly customize the logic by implementing an extension interface. Such as customized rule management, adapting to dynamic data sources, and so on.

The main features of Sentinel are:

Sentinel's open source ecology:

Tell me about how to build. This time it will not extend to a deeper level. Only teach you to use it quickly. When the spring cloud gateway update is completed, it will take you to dig deeper into the functions of Sentinel.

Rapid Construction of Sentinel

Download the jar package already typed on the official website directly.

Release address https://github.com/alibaba/Sentinel/releases

Source code compiling git clone https://github.com/alibaba/Sentinel.git

Then go to the directory to execute mvn clean package

Command start

Java-Dserver.port=8080-Dcsp.sentinel.dashboard.server=localhost:8080-Dproject.name=sentinel-dashboard-jar sentinel-dashboard.jar

If you need docker, you can write docker Dockerfile

# based on which image FROM java:8# copies files to the container It can also be written directly as ADD microservice-discovery-eureka-0.0.1-SNAPSHOT.jar / app.jarADD. / * .jar app.jarRUN mkdir-p / var/logs/SentinelRUN mkdir-p / var/logs/jvmRUN mkdir-p / var/logs/dumpRUN bash-c 'touch / app.jar'# Open Port 8080 EXPOSE 808 configuration Container ENTRYPOINT ["java", "- Djava.security.egd=file:/dev/./urandom", "- XX:-PrintGCDetails" "- XX:-PrintGCTimeStamps", "- XX:-HeapDumpOnOutOfMemoryError", "- XX:HeapDumpPath=/var/logs/dump/oom_dump.dump", "- Xloggc:/var/logs/jvm/app.log", "- Dfile.encoding=UTF8", "- Duser.timezone=GMT+08", "- XX:CMSInitiatingOccupancyFraction=90", "- XX:MaxGCPauseMillis=200", "- XX:StringTableSize=20000", "- XX:+UseG1GC", "- Xss256k", "- Xmx1024m", "- Xms512m", "- jar", "/ app.jar"]

Perform docker image production

Docker build-- tag sentinel:1.0.

-- tag projectname:version pay attention to how to write

Then docker run starts the mirror. Here the author uses the docker image method to start docker run-d-p9088purl 8080 304342c105e9

Then the console enters the http://localhost:9088/ username and password sentinel/sentinel

Login parameter configuration:

Since Sentinel 1.6.0, the Sentinel console has introduced basic login functions, with a default user name and password of sentinel. You can configure a user name and password by referring to the authentication module documentation.

Dsentinel.dashboard.auth.username=sentinel is used to specify that the login user name of the console is sentinel

Dsentinel.dashboard.auth.password=123456 is used to specify that the login password for the console is 123456; if these two parameters are omitted, the default user and password are sentinel

Dserver.servlet.session.timeout=7200 is used to specify the expiration time of the Spring Boot server session. For example, 7200 means 7200 seconds; 60m represents 60 minutes. The default is 30 minutes.

Enter the password to log in

The service has started our integration of zuul services in the integration gateway to sentinel.

Pom add

Com.alibaba.cloud spring-cloud-starter-alibaba-sentinel

Operation to add two files under resources

Api.json

[{"apiName": "cloud-discovery-server", "predicateItems": [{"pattern": "/ server/**"}, {"pattern": "/ server/server/**", "matchStrategy": 1} {"items": [{"pattern": "/ server/**"}]}, {"apiName": "cloud-discovery-client", "predicateItems": [{"pattern": "/ client/**"}]}]

Start the service and make a previous access

Gateway.json

[{"resource": "cloud-discovery-server", "count": 1}, {"resource": "cloud-discovery-client", "count": 10, "intervalSec": 2, "controlBehavior": 1, "maxQueueingTimeoutMs": 200, "paramItem": {"parseStrategy": 3, "fieldName": "test"}}]

Multiple requests for curl http://localhost:9083/server/server/hello?name=tom

And then on the console.

Api.json corresponds to console api management

A user-defined API definition group can be thought of as a combination of some URL matches. For example, we can define an API called my_api, and request that the path patterns of / foo/** and / baz/** fall under the API group of my_api. When you limit the flow, you can limit the current according to this custom API grouping dimension.

Gateway corresponds to console flow control management.

Resource: resource name, which can be the route name in the gateway or the user-defined API packet name.

ResourceMode: the rule is for the route of API Gateway (RESOURCE_MODE_ROUTE_ID) or the API packet defined by the user in Sentinel (RESOURCE_MODE_CUSTOM_API_NAME). The default is route.

Grade: the dimension of current limit indicator and the grade field of the same current limit rule.

Count: current limiting threshold

IntervalSec: statistical time window (in seconds). Default is 1 second (current limit is only available for parameters).

ControlBehavior: the control effect of traffic shaping. The controlBehavior field of the same current limit rule supports two modes: fast failure and uniform queuing. The default is fast failure.

Burst: the number of additional requests allowed when dealing with sudden requests (currently, only the parameter limit is valid).

MaxQueueingTimeoutMs: the maximum queue time (in milliseconds) in uniform queuing mode, which takes effect only in uniform queuing mode.

ParamItem: parameter current limit configuration. If it is not provided, it means that the current is not limited for the parameters, and the gateway rule will be converted into a normal traffic control rule; otherwise, it will be converted into a hot spot rule. The fields are:

ParseStrategy: a policy for extracting parameters from a request. Currently, you can extract source IP (PARAM_PARSE_STRATEGY_CLIENT_IP),

There are four modes: Host (PARAM_PARSE_STRATEGY_HOST), arbitrary Header (PARAM_PARSE_STRATEGY_HEADER) and arbitrary URL parameter (PARAM_PARSE_STRATEGY_URL_PARAM).

FieldName: if the extraction policy selects Header mode or URL parameter mode, you need to specify the corresponding header name or URL parameter name.

Pattern and matchStrategy: reserved for subsequent parameter matching features, which are not currently implemented.

In fact, you can see the request link and traffic rules on the console. The location of the editor can all match this is a variety of combined configurations. Because Sentinel is Alibaba's open source flow control framework. Very simple and practical. The documentation is also very comprehensive, and it is highly recommended that you go to the official website to learn these materials. I'm just doing a simple integration here.

Taking the flow as the starting point, Sentinel protects the stability of the service from many dimensions, such as flow control, circuit breaker degradation, system load protection and so on.

Thank you for your reading. the above is the content of "how to understand Spring Cloud alibaba Gateway". After the study of this article, I believe you have a deeper understanding of how to understand Spring Cloud alibaba Gateway, and the specific usage needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report