1. Add the gateway filter for the request header AddRequestHeader
The matched route will add the specified request header and value in the format of request header name, request header value
Server: port: 81 Spring: cloud: gateway: routes: # Configure a set of routes. - id: apptest1 http://localhost:80 # Predicates: -path =/say/** filters: -addrequestheader =gateway,8848Copy the code
Change the Controller code
package gateway.controller; import lombok.Data; import org.springframework.beans.factory.annotation.Value; import org.springframework.web.bind.annotation.GetMapping; 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.context.request.RequestContextHolder; import reactor.netty.http.server.HttpServerRequest; import java.util.Iterator; import java.util.Map; /** * @author sz * @DATE 2022/3/20 20:15 */ @Data @RestController public class HelloController { @Value("${server.port}") public String serverPort; @GetMapping("/say") public String say( @RequestHeader(name = "gateway",required = false)String value ) { return "HelloWord "+serverPort +" "+value; } @GetMapping("/say/one") public String sayOne() { return "HelloWord one"; }}Copy the code
Returns the result
We send the request without the request header, because matching the appTest1 route adds the specified request header to the request
2. Add the gateway filter AddRequestParameter for request parameters
Matched routes will add the specified request parameters and values in the format of request parameter name, request parameter value
Server: port: 81 Spring: cloud: gateway: routes: # Configure a set of routes. - id: apptest1 http://localhost:8081 # Predicates: -path =/say/** filters: -addrequestParameter =gateway,8848Copy the code
package gateway.controller; import lombok.Data; import org.springframework.beans.factory.annotation.Value; import org.springframework.web.bind.annotation.*; import org.springframework.web.context.request.RequestContextHolder; import reactor.netty.http.server.HttpServerRequest; import java.util.Iterator; import java.util.Map; /** * @author sz * @DATE 2022/3/20 20:15 */ @Data @RestController public class HelloController { @Value("${server.port}") public String serverPort; @GetMapping("/say") public String say( @RequestParam(name = "gateway",required = false)String value ) { return "HelloWord "+serverPort +" "+value; } @GetMapping("/say/one") public String sayOne() { return "HelloWord one"; }}Copy the code
3. Add the gateway filter AddResponseHeader for the response header
The matched route will add the specified response header parameter and value in the format of response header name, response header value
Server: port: 81 Spring: cloud: gateway: routes: # Configure a set of routes. - id: apptest1 http://localhost:8081 # Predicates: -path =/say/** filters: -addresponseheader =gateway, 8848Copy the code
When we send a request, we do not carry any headers, but when we respond, we can see that we have added the specified headers
4. Request header mapping gateway filters MapRequestHeader
MapRequestHeader= First request, second request
If the request header does not exist in the request object, no changes are made to the request object’s request header
If the request header exists in the request object and the request header does not exist, the request header two is added to the request object’s request header and the value of the request header one is assigned to the request header two
No changes will be made to the request object if the first two of the requests already exist
Server: port: 81 Spring: cloud: gateway: routes: # Configure a set of routes. - id: apptest1 http://localhost:8081 # Predicates: -path =/say/** filters: -maprequestheader =fuck, helloCopy the code
What does that mean?
If the request object has a FUCK request header in its request header and no Hello request header
The Hello request header is added and the fuck request header value is assigned to the Hello request header
If the request object’s request header already carries a Hello request header, or does not have a FUCK request header, the Hello request header will not change
See demo
@Data @RestController public class HelloController { @Value("${server.port}") public String serverPort; @GetMapping("/say") public String say( ) { ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes)RequestContextHolder.getRequestAttributes(); HttpServletRequest request = servletRequestAttributes.getRequest(); HttpServletResponse response = servletRequestAttributes.getResponse(); String fuck = request.getHeader("fuck"); System.out.println("fuck = " + fuck); String hello = request.getHeader("hello"); System.out.println("hello = " + hello); return "HelloWord "; } @GetMapping("/say/one") public String sayOne() { return "HelloWord one"; }}Copy the code
5. Add the path prefix gateway filter PrefixPath
If the route matches the current route, the specified prefix is added to the path
Server: port: 81 Spring: cloud: gateway: routes: # Configure a set of routes. - id: apptest1 http://localhost:8081 # Predicates: -path =/say/** filters: -prefixpath =/prefixCopy the code
The above configuration
The request mapping cannot be found by accessing /say because the mapping address of the request object has been prefixed with address /prefix
The access path is localhost:81/prefix/say
6. Redirection gateway filter RedirectTo
Server: port: 81 Spring: cloud: gateway: routes: # Configure a set of routes. - id: apptest1 http://localhost:8081 # after matching service routing address predicates: - Path = / say / * * filters: - RedirectTo = 302, http://localhost:8081/say/oneCopy the code
If the current request matches the route, it is redirected to the specified address
7. Delete the request header gateway filter RemoveRequestHeader
Server: port: 81 Spring: cloud: gateway: routes: # Configure a set of routes. - id: apptest1 http://localhost:8081 # Predicates: -path =/say/** filters: -removerequestheader =fuckCopy the code
If the current request matches this route, the request header parameter specified in the request header is removed
The request object has the request header FUCK and the value you
But by the time the Controller layer receives it, it’s gone
8. Delete the response header gateway filter RemoveResponseHeader
If the current request matches this route, the header information specified in the response header is removed
Server: port: 81 Spring: cloud: gateway: routes: # Configure a set of routes. - id: apptest1 http://localhost:8081 # Predicates: -path =/say/** filters: -removeresponseHeader =fuckCopy the code
Leave this gateway filter out and access /say
@GetMapping("/say")
public String say(
)
{
ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes)RequestContextHolder.getRequestAttributes();
HttpServletRequest request = servletRequestAttributes.getRequest();
HttpServletResponse response = servletRequestAttributes.getResponse();
response.setHeader("fuck","you");
return "HelloWord ";
}
Copy the code
The response header has fuck and has a value
If we add this filter, restart the gateway module to access again
Server: port: 81 Spring: cloud: gateway: routes: # Configure a set of routes. - id: apptest1 http://localhost:8081 # Predicates: -path =/say/** filters: -removeresponseHeader =fuckCopy the code
Fuck has been removed from the response header
9. Delete the request parameter gateway filter PrefixPath
Server: port: 81 Spring: cloud: gateway: routes: # Configure a set of routes. - id: apptest1 http://localhost:8081 # Predicates: -path =/say/** filters: -removerequestparameter =fuckCopy the code
If the current request matches this route, the fuck in the request parameters is removed, if any
10. Path rewrite gateway filter RewritePath
Server: port: 81 Spring: cloud: gateway: routes: # Configure a set of routes. - id: apptest1 http://localhost:8081 # Predicates: -path =/say/** filters: -rewritepath =/say(? <segment>/? .*), $\{segment}Copy the code
If the current request matches the path, the path is rewritten according to the specified rules
The above configuration where we access /say/fuck will be rewritten to access /fuck
@GetMapping("/say")
public String say(
)
{
ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes)RequestContextHolder.getRequestAttributes();
HttpServletRequest request = servletRequestAttributes.getRequest();
HttpServletResponse response = servletRequestAttributes.getResponse();
response.setHeader("fuck","you");
return "HelloWord ";
}
@GetMapping("/fuck")
public String fuck(){
return "RewritePath fuck";
}
Copy the code
11. The template resets the path gateway filter SetPath
If the request matches the current route, the template is used to reset the path
What does that mean if you look at this configuration
Spring: cloud: gateway: routes: # Configure a set of routes. - id: apptest1 http://localhost:80 # Predicates: -path =/fuck/{segment} filters: -setpath =/{segment}Copy the code
If we access /fuck/say the path will be reset to /say
For example, we accessed localhost:81/fuck/say when in fact we accessed localhost:81/say
12. Set the response status code gateway filter SetStatus
Modifies the response status code to the specified value. Note that only the response status code is modified
Spring: cloud: gateway: routes: # Configure a set of routes. - id: apptest1 http://localhost:80 # Predicates: -path =/say filters: -setStatus =404Copy the code
13. Retry the gateway filter
retries
: Number of retries that should be attempted.statuses
: the HTTP status code should be retried, using the representation.org.springframework.http.HttpStatus
methods
: the HTTP method should be retried by using the representation.org.springframework.http.HttpMethod
series
: A sequence of status codes to retry, using the representation.org.springframework.http.HttpStatus.Series
exceptions
: A list of raised exceptions that should be retried.backoff
: Retreats the index configured for retry. Retry is performed after the fallback interval is, where iteration is. If yes, the maximum rollback limit of the application is. If true, a computational fallback is used.firstBackoff * (factor ^ n)``n``maxBackoff``maxBackoff``basedOnPreviousValue``prevBackoff * factor
Spring: cloud: gateway: routes: # Configure a set of routes. - id: apptest1 http://localhost:80 # Predicates: -path =/say/** filters: - name: Retry args: retries: 30 statuses: NOT_FOUND methods: GET,POST backoff: firstBackoff: 1000ms maxBackoff: 5000ms factor: 2 basedOnPreviousValue: falseCopy the code
The effect of the preceding configuration is
The request path will be retried 30 times if it matches the current route (ID: Apptest1) and the response status code returned is NOT_FOUND (404)
After the first retry interval is 1 second, the maximum retry interval is 5 seconds
Global filters have been added to see the effects of the test
@Component public class GlobalFilterConf implements GlobalFilter { Long start = System.currentTimeMillis(); long end = 0L; @override public <Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {system.out.println (" "+exchange.getRequest().getPath().toString()); Long temp = end = System.currentTimeMillis(); System.out.println(" interval: "+ (end-start)/1000); start = temp; return chain.filter(exchange); }}Copy the code
Access a mapping address that does not exist