Java’s SpringCloud Alibaba [1] [Nacos One Article Mastery Series] | Jump |
---|---|
Java’s SpringCloud Alibaba [2] [Microservice calling component Feign] | Jump |
Java’s SpringCloud Alibaba [3 】[Microservice Nacos-config Configuration Center] | Jump |
Java’s SpringCloud Alibaba [Four] [Microservice Sentinel Service Meltdown] | Jump |
Java’s SpringCloud Alibaba [5] [Microservice Sentinel integrates openfeign for downgrade] | Jump |
Java’s SpringCloud Alibaba [6] [Alibaba microservice distributed transaction component-Seata] | Jump |
SpringCloud Alibaba in Java [Seven] [Spring Cloud Microservice Gateway Gateway Component] | Jump |
SpringCloud Alibaba in Java [Eight] [Spring Cloud Microservice Service Gateway integrates sentinel current limiting] | Jump |
Java’s SpringCloud Alibaba [9] [Spring Cloud Microservice Skywalking] | Jump |
Java’s SpringCloud Alibaba [8] [Spring Cloud microservice Gateway integrated sentinel current limiting]
-
- 1. Gateway integrates sentinel current limiting
-
- 1. Add dependencies
- 2. Add configuration
- 3. Improve the test interface
- 2. Implement current limiting through code
-
- 1. Write configuration class
- 3. Implementation through configuration files
- 4. High availability of network management
1. Gateway integrates sentinel current limiting
As a barrier outside the internal system, the gateway plays a certain protective role internally, and current limiting is one of them. The current limiting at the gateway layer can simply limit the current for different routes, or it can also target the business interface. Perform current limiting, or group current limiting based on the characteristics of the interface.
1. Add dependencies
<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>
2. Add configuration
server: port: 8088 spring: application: name: api-gateway # gateway configuration cloud: gateway: routes: - id: order_route #The unique identifier of the route, route to order uri: lb://order-service # The address that needs to be forwarded lb refers to obtaining the microservice by name from nacos and following the load balancing policy order-service service name #Assertion rules are used for matching routing rules predicates: - Path=/order/** # http://localhost:8088/order-serve/order/add routes to ↓ # http://localhost:8020/order-serve/order/add #- After=2020-10-19T09:07:00.660 + 08:00[Asia/Shanghai] #- Header=X-Request-Id, \d + #- Method=GET #- Query=name,xushu|zhuge #- CheckAuth = xushu #filters: #- AddRequestHeader=X-Request-color,red #- AddRequestParameter=color,blue #- PrefixPath=/mall-order #To add a prefix corresponding to the microservice, you need to configure context-path #- StripPrefix=1 # Before forwarding, remove the first path # http://localhost:8020/order/add #- RedirectTo=302, https://www.baidu.com #-SetStatus= 404 #- CheckAuth=xushu #- id: stock_route # Configure Nacos # Cross-domain configuration #globalcors: # cors-configurations: # '[/**]': # Allow cross-domain access resources # allowedOrigins: "*" #Cross-domain allowed origins For example: www.smsm.com # allowedMethods: #-GET # - POST #-PUT #Configure Nacos nacos: discovery: server-addr: 127.0.0.1:8848 username: nacos password: nacos # Configure sentinel sentinel: transport: dashboard: 127.0.0.1:8858
3. Improve the test interface
Download: sentinel-dashboard-1.8.0.jar
https://github.com/alibaba/Sentinel/releases
Run jar package
java -Dserver.port=8858 -Dsentinel.dashboard.auth.username=xushu -Dsentinel.dashboard.auth.password=123456 -jar C:\Users\ZHENG\Desktop\sentinel-dashboard-1.8 .0.jar
Visit: http://localhost:8858/
Account: xushu
Password: 123456
http://127.0.0.1:8088/order/add
Visit: http://127.0.0.1:8088/order/add
Keep clicking for continuous visits
2. Implement current limiting through code
1. Write configuration class
@Configuration public class GatewayConfig {<!-- --> @PostConstruct //When setting initialization public void init(){<!-- --> BlockRequestHandler blockRequestHandler = new BlockRequestHandler() {<!-- --> @Override public Mono<ServerResponse> handleRequest(ServerWebExchange serverWebExchange, Throwable t) {<!-- --> System.out.println(t); HashMap<String,String> map = new HashMap<String,String>(); map.put("code",HttpStatus.TOO_MANY_REQUESTS.toString()); map.put("message","current limit"); //Customized exception handling return ServerResponse.status(HttpStatus.OK) .contentType(MediaType.APPLICATION_JSON) .body(BodyInserters.fromValue(map)) ; } }; GatewayCallbackManager.setBlockHandler(blockRequestHandler); } }
Visit: http://127.0.0.1:8088/order/get
Visit: http://127.0.0.1:8088/order/get
continuous access
3. Implementation through configuration files
scg: fallback: mode: response response-body: "{code:'',messageL:''}"
4. Network management high availability
In order to ensure the high availability of the Gateway, multiple Gateway instances can be started at the same time for load. Nginx or F5 is used upstream of the Gateway for load forwarding to achieve high availability.
Java’s SpringCloud Alibaba [1] [Nacos One Article Mastery Series] | Jump |
---|---|
Java’s SpringCloud Alibaba [2] [Microservice calling component Feign] | Jump |
Java’s SpringCloud Alibaba [3 】[Microservice Nacos-config Configuration Center] | Jump |
Java’s SpringCloud Alibaba [Four] [Microservice Sentinel Service Meltdown] | Jump |
Java’s SpringCloud Alibaba [5] [Microservice Sentinel integrates openfeign for downgrade] | Jump |
Java’s SpringCloud Alibaba [6] [Alibaba microservice distributed transaction component-Seata] | Jump |
SpringCloud Alibaba in Java [Seven] [Spring Cloud Microservice Gateway Gateway Component] | Jump |
SpringCloud Alibaba in Java [Eight] [Spring Cloud Microservice Service Gateway integrates sentinel current limiting] | Jump |
Java’s SpringCloud Alibaba [9] [Spring Cloud Microservice Skywalking] | Jump |