5. Local - built in filter factory

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 factoryeffectparameter
AddRequestHeaderAdd Header for original requestName and value of Header
AddRequestParameterAdd request parameters to the original requestParameter name and value
AddResponseHeaderAdd Header for original responseName and value of Header
DedupeResponseHeaderEliminate duplicate values in response headersHeader name and de duplication policy that need to be de duplicated
HystrixIntroduce circuit breaker protection of Hystrix for routingThe name of the HystrixCommand
FallbackHeadersAdd specific exception information to the request header of fallbackUriThe name of the Header
PrefixPathPrefix the original request pathprefix path
PreserveHostHeaderThe routing filter checks this property to determine whether to send the original Hostnothing
RequestRateLimiterIt is used to limit the current of requests. The current limiting algorithm is token bucketkeyResolver,rateLimiter,statusCode, denyEmptyKey,emptyKeyStatus
RedirectToRedirect the original request to the specified URLhttp status code and redirected url
RemoveHopByHopHeadersFilterDelete a series of headers specified by IETF organization for the original requestIt will be enabled by default. You can specify which headers to delete only through configuration
RemoveRequestHeaderDelete a Header for the original requestHeader name
RemoveResponseHeaderDelete a Header for the original responseHeader name
RewritePathRewrite the original request pathRegular expressions of original path and rewritten path
RewriteResponseHeaderOverride a Header in the original responseHeader name, regular expression of value, rewritten value
SaveSessionForce the WebSession::save operation before forwarding the requestnothing
secureHeadersAdd a series of security response headers to the original responseNone. It supports modifying the values of these security response headers
SetPathModify the original request pathModified path
SetResponseHeaderModify the value of a Header in the original responseModify the value of a Header in the original response, the Header name, and the modified value
SetStatusModify the status code of the original responseHTTP status code, which can be a number or a string
StripPrefixThe path used to truncate the original requestUse a number to indicate the number of paths to truncate
RetryRetry for different responsesretries,statuses,methods,series
RequestSizeSets 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 returnedThe size of the request packet, in bytes. The default value is 5M
ModifyRequestBodyModify the content of the original request body before forwarding the requestModified request body content
ModifyResponseBodyModify the content of the original response bodyModified response body content

1.1 add request header

In the gateway module

  port: 8070
    name: api-gateway
    #gateway configuration
      #Routing configuration [routing is to specify which micro service to go to when the request meets what conditions]
        - 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
            # 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
        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;

public class OrderController {

    private RestTemplate restTemplate;

    public String add(){
        System.out.println("checkout success ");
        String forObject = restTemplate.getForObject("http://stock-service/stock/reduck", String.class);
        return "Hello World " + forObject;

    public String header(@RequestHeader("X‐Request‐color") String color){
        return color;

Start test

Start the nacos, order, stock and gateway modules

visit: http://localhost:8070/order/header

1.2 add prefixes to matching routes

In the gateway module

In order sub module

  port: 8081
    context-path: /myorder
#Application name (nacos will take this name as the service name)
    name: order-service
        username: nacos
        password: nacos
        namespace: public

Start test

Start the nacos, order, stock and gateway services

visit: http://localhost:8070/order/add

Keywords: Java Front-end Spring Cloud

Added by pauper_i on Fri, 18 Feb 2022 17:21:55 +0200