Microservices are so popular today that if you can’t learn a microservice framework technology. How can you get a promotion and raise and boost your resume? Spring Cloud and Dubbo need to be studied separately. No time? No energy? Two frames to learn? Spring Cloud Alibaba, on the other hand, only needs you to learn one to have two microservices governance framework technologies. Why not? Come on! In SAO ape
Sentinel fuse current limiting
The access mode used by our Zuul gateway service was Sentinel access mode. In fact, there is a very useful Sentinel Starter dependency in the Spring Cloud Alibaba system. You only need to rely on one JAR package. Then configure the Sentinel server address.
Sentinel service setup and startup
Rapid build of Sentinel
The previous chapter talked about how to build. Let’s paste it again this time. Download the jar package from the official website
Release the address https://github.com/alibaba/Sentinel/releases
The source code to compile git clone https://github.com/alibaba/Sentinel.git
Then go to the directory and execute the MVN Clean Package
Command to start the
java -Dserver.port=8080 -Dcsp.sentinel.dashboard.server=localhost:8080 -Dproject.name=sentinel-dashboard -jar sentinel-dashboard.jarCopy the code
If you need a docker you can write docker Dockerfile
# FROM Java :8 # copy file to container, ADD microservice-discovery-eureka-0.0.1 -snapshot.jar /app.jar ADD./*.jar app.jar RUN mkdir -p /var/logs/Sentinel RUN mkdir -p /var/logs/ JVM RUN mkdir -p /var/logs/dump RUN bash -c 'touch /app.jar' # EXPOSE 8080 # Configure the ENTRYPOINT command to be executed after the container is started ["java","-Djava.security.egd=file:/dev/./urandom","-Dsentinel.dashboard.auth.username=sentinel","-Dsentinel.dashboard.au th.password=123456","-Dserver.servlet.session.timeout=7200","-XX:-PrintGCDetails","-XX:-PrintGCTimeStamps","-XX:-HeapDum pOnOutOfMemoryError","-XX:HeapDumpPath=/var/logs/dump/oom_dump.dump","-Xloggc:/var/logs/jvm/app.log","-Dfile.encoding=UT F8","-Duser.timezone=GMT+08","-XX:CMSInitiatingOccupancyFraction=90","-XX:MaxGCPauseMillis=200","-XX:StringTableSize=200 00","-XX:+UseG1GC","-Xss256k","-Xmx1024m","-Xms512m","-jar","/app.jar"]Copy the code
Execute docker image creation
Docker build -- Tag Sentinel :1.0.Copy the code
–tag projectName :version
Docker Run then starts the image. Docker run -d p8890:8080-p8891:8080 304342C105e9
The console then enters http://localhost:9088/ username password sentinel/123456
Login parameters:
Starting with Sentinel 1.6.0, the Sentinel console introduced basic login functionality, with the default user name and password being Sentinel. For details about how to configure the user name and password, see the authentication module documentation.
- Dsentinel. Dashboard. The auth. Username = sentinel is used to specify the console login user called sentinel;
- Dsentinel. Dashboard. The auth. The password is used to specify the console login password = 123456 to 123456; If these two parameters are omitted, the default user and password are both sentinel.
- . Dserver. Servlet. The session timeout = 7200 is used to specify the Spring of the Boot server session expiration time, such as 7200 7200 seconds; 60m indicates 60 minutes. The default value is 30 minutes.
Enter a password to log in
Once the login is complete, we begin the integration of the Gateway.
Introduce poM in our Gateway service
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-sentinel-gateway</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>Copy the code
Create the RulesController expose interface
package com.xian.cloud.controller; import com.alibaba.csp.sentinel.adapter.gateway.common.api.ApiDefinition; import com.alibaba.csp.sentinel.adapter.gateway.common.api.GatewayApiDefinitionManager; import com.alibaba.csp.sentinel.adapter.gateway.common.rule.GatewayFlowRule; import com.alibaba.csp.sentinel.adapter.gateway.common.rule.GatewayRuleManager; import com.alibaba.csp.sentinel.annotation.SentinelResource; import com.alibaba.csp.sentinel.slots.block.flow.FlowRule; import com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; import java.util.List; import java.util.Set; /** * @author <a href="mailto:[email protected]">Jim</a> */ @RestController public class RulesController { @GetMapping("/api") @SentinelResource("api") public Set<ApiDefinition> apiRules() { return GatewayApiDefinitionManager.getApiDefinitions(); } @GetMapping("/gateway") @SentinelResource("gateway") public Set<GatewayFlowRule> apiGateway() { return GatewayRuleManager.getRules(); } @GetMapping("/flow") @SentinelResource("flow") public List<FlowRule> apiFlow() { return FlowRuleManager.getRules(); }}Copy the code
The bootstrap.yml file is added to specify the sentinel service address
Port :8890 # Service start directly establish heartbeat connection: trueCopy the code
Start the service
Request curl http://localhost:9000/api many times
Real-time monitoring data
Cluster point link
On the right, you can configure flow control, degrade, hotspot, and authorization operations
Flow control Settings
Corresponding parameter properties
- Resource: indicates the name of the resource, which is the object of the traffic limiting rule
- Count: traffic limiting threshold
- Grade: Type of flow limiting threshold (QPS or number of concurrent threads)
- LimitApp: call source of flow control. If it is default, call source is not differentiated
- Strategy: Invokes the relational traffic limiting policy
- ControlBehavior: Flow control effect (direct reject, Warm Up, uniform queuing)
conclusion
Above is the integration solution of Spring Cloud Gateway and Sentinel. Careful students may think that the traffic limiting rules we set will not exist if the service is restarted, which is definitely unacceptable to us. The next article will show you how to store Sentinel Settings.
Past materials, reference materials
Sentinel official document address
Excerpt from the spring Cloud official documentation
Spring Cloud Alibaba official website address
Sample code address
Nacos address http://47.99.209.72:8848/nacos server
Address of Spring Cloud Alibaba
Introduction to Spring Cloud Alibaba
Spring Cloud Alibaba (Establishment of NACOS Registry)
Spring Cloud Alibaba uses the NACOS registry
Spring Cloud Alibaba NacOS configuration center use
Spring Cloud Gateway services
Spring Cloud Zuul Gateway Service one
Spring Cloud Gateway service Zuul ii
Spring Cloud Gateway service Zuul three dynamic routing
Spring Cloud Alibaba Gateway Sentinel Zuul quad-current limited fuse
Spring Cloud Gateway Gateway service 1
Spring Cloud Gateway gateway service two assertions, filters
Spring Cloud Gateway Three custom filter GatewayFilter
Spring Cloud Gateway Four dynamic routes
How can you like to share this public number.
Copyright notice: This article is originally published BY the blogger. It follows the COPYRIGHT agreement CC 4.0 BY-SA. Please attach the link of the original source and this statement. Please attach the qr code of the public account