There are many built-in filter factories in Gateway. Through some filter factories, we can carry out some business logic processors, such as adding and removing response headers, adding and removing parameters, etc
Filter factory official website: https://docs.spring.io/spring-cloud-gateway/docs/current/reference/html/#gatewayfilter-factories
Filter factory | effect | parameter |
---|---|---|
AddRequestHeader | Add Header for original request | Name and value of Header |
AddRequestParameter | Add request parameters to the original request | Parameter name and value |
AddResponseHeader | Add Header for original response | Name and value of Header |
DedupeResponseHeader | Eliminate duplicate values in response headers | Header name and de duplication policy that need to be de duplicated |
Hystrix | Introduce circuit breaker protection of Hystrix for routing | The name of the HystrixCommand |
FallbackHeaders | Add specific exception information to the request header of fallbackUri | The name of the Header |
PrefixPath | Prefix the original request path | prefix path |
PreserveHostHeader | The routing filter checks this property to determine whether to send the original Host | nothing |
RequestRateLimiter | It is used to limit the current of requests. The current limiting algorithm is token bucket | keyResolver,rateLimiter,statusCode, denyEmptyKey,emptyKeyStatus |
RedirectTo | Redirect the original request to the specified URL | http status code and redirected url |
RemoveHopByHopHeadersFilter | Delete a series of headers specified by IETF organization for the original request | It will be enabled by default. You can specify which headers to delete only through configuration |
RemoveRequestHeader | Delete a Header for the original request | Header name |
RemoveResponseHeader | Delete a Header for the original response | Header name |
RewritePath | Rewrite the original request path | Regular expressions of original path and rewritten path |
RewriteResponseHeader | Override a Header in the original response | Header name, regular expression of value, rewritten value |
SaveSession | Force the WebSession::save operation before forwarding the request | nothing |
secureHeaders | Add a series of security response headers to the original response | None. It supports modifying the values of these security response headers |
SetPath | Modify the original request path | Modified path |
SetResponseHeader | Modify the value of a Header in the original response | Modify the value of a Header in the original response, the Header name, and the modified value |
SetStatus | Modify the status code of the original response | HTTP status code, which can be a number or a string |
StripPrefix | The path used to truncate the original request | Use a number to indicate the number of paths to truncate |
Retry | Retry for different responses | retries,statuses,methods,series |
RequestSize | Sets the size of the maximum request packet allowed to be received. If the requested packet size exceeds the set value, 413 Payload Too Large is returned | The size of the request packet, in bytes. The default value is 5M |
ModifyRequestBody | Modify the content of the original request body before forwarding the request | Modified request body content |
ModifyResponseBody | Modify the content of the original response body | Modified response body content |
1.1 add request header
In the gateway module
server: port: 8070 spring: application: name: api-gateway cloud: #gateway configuration gateway: #Routing configuration [routing is to specify which micro service to go to when the request meets what conditions] routes: - id: order_route #The unique identifier of the route to order uri: lb://Order service # refers to the address to be forwarded. lb refers to the microservices obtained from nacos by name and following the load balancing policy #Assertion rules are the conditions to be met by routing and forwarding predicates: # When client access http://localhost:8070/order/add Route to ↓ # http://localhost:8081/order/add - Path=/order/** #When the request Path meets the rules specified by Path, route forwarding is carried out filters: #Filter. The request can be modified through the filter during transmission - AddRequestHeader=X-Request-color, blue #Add request header #Configure nacos nacos: discovery: server-addr: 127.0.0.1:8848 username: nacos password: nacos
In order sub module
package com.example.order.controller; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestHeader; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.client.RestTemplate; @RestController @RequestMapping("/order") public class OrderController { @Autowired private RestTemplate restTemplate; @RequestMapping("/add") public String add(){ System.out.println("checkout success "); String forObject = restTemplate.getForObject("http://stock-service/stock/reduck", String.class); return "Hello World " + forObject; } @RequestMapping("/header") public String header(@RequestHeader("X‐Request‐color") String color){ return color; } }
Start test
Start the nacos, order, stock and gateway modulesvisit: http://localhost:8070/order/header
1.2 add prefixes to matching routes
In the gateway module
In order sub module
server: port: 8081 servlet: context-path: /myorder #Application name (nacos will take this name as the service name) spring: application: name: order-service cloud: nacos: server-addr: 127.0.0.1:8848 discovery: username: nacos password: nacos namespace: public
Start test
Start the nacos, order, stock and gateway services
visit: http://localhost:8070/order/add