Springcloud microservice (24) - gateway gateway three features: routing, assertion and filter

Huaguo shuilian 2022-01-27 05:47:08 阅读数:175

springcloud microservice gateway gateway features

One 、Gateway Create dynamic routing

We from gateway Configuration file for application.yml Can be seen in ,url Is constant , Load balancing cannot be achieved , There are still problems . So you need to configure dynamic routing . That is, the function of dynamically creating routes from the registry , Routing with microservice name .

server:
port: 9527
spring:
application:
name: cloud-gateway-server
cloud:
gateway:
discovery:
locator:
enabled: true # Enable the function of dynamically creating routes from the registry , Routing with microservice name
routes:
- id: payment_routh # The routing ID, There are no fixed rules, but it is required to be unique
uri: lb://CLOUD-PAYMENT-SERVICE # The routing address provided after matching
predicates:
- Path=/payment/get/** # Assertion , Route with matching path
- id: payment_routh2
uri: lb://CLOUD-PAYMENT-SERVICE # The routing address provided after matching
predicates:
- Path=/payment/lb/**
eureka:
client:
fetch-registry: true
register-with-eureka: true
service-url:
defaultZone: http://eureka7001.com:7001/eureka/

1.2 test

start-up Eureka,payment8001,payment8002,gateway9527

Request address :http://localhost:9527/payment/lb
effect :8001/8002 Switch between two ports

Two 、 Assertion Predicate

Spring Cloud Gateway Route matching as Spring WebFlux HandlerMapping Part of the infrastructure .

Spring Cloud Gateway Including many built-in Route Predicate factory . All of these Predicate Both with HTTP The different properties of the request match . Multiple Route Predicate Factories can combine Spring Cloud Gateway establish Route Object time , Use RoutePredicateFactory establish Predicate object , Predicate Object can be assigned to Route.Spring Cloud Gateway Contains many built-in Route Predicate Factories.

All of these predicates match HTTP Different properties of the request . Many predicate factories can be combined , And through logic and.

frequently-used Route Predicate

1. After Route Predicate

server:
port: 9527
spring:
application:
name: cloud-gateway-server
cloud:
gateway:
discovery:
locator:
enabled: true
routes:
- id: payment_routh # The routing ID, There are no fixed rules, but it is required to be unique
uri: lb://CLOUD-PAYMENT-SERVICE # The routing address provided after matching
predicates:
- Path=/payment/get/** # Assertion , Route with matching path
- After=2021-12-17T21:24:30.485+08:00[Asia/Shanghai]
- id: payment_routh2
uri: lb://CLOUD-PAYMENT-SERVICE # The routing address provided after matching
predicates:
- Path=/payment/lb/**
- After=2021-12-17T21:24:30.485+08:00[Asia/Shanghai]
eureka:
client:
fetch-registry: true
register-with-eureka: true
service-url:
defaultZone: http://eureka7001.com:7001/eureka/

Request only in after Then time will work , It will be reported in less time 404
2. Before Route Predicate
3. Between Route Predicate
4. Cookie Route Predicate
5. Header Route Predicate
6. Host Route Predicate
7. Method Route Predicate
8. Path Route Predicate
9. Query Route Predicate
 

3、 ... and 、 filter Filter

Routing filters can be used to modify incoming HTTP Requested and returned HTTP Respond to , Routing filters can only specify routes to use .
Spring Cloud Gateway Built in a variety of routing filters , They are all made up of GatewayFilter Our factory Class to generate

Life cycle :
1.pre

2.post

3. Custom filter

Custom global GlobalFilter
1. Implement two interfaces

@Component
@Slf4j
public class MyLogGlobalFilter implements GlobalFilter,OrderedFilter {
@Override
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
log.info("come in global filter: {}", new Date());
ServerHttpRequest request = exchange.getRequest();
String uname = request.getQueryParams().getFirst("uname");
if (uname == null) {
log.info(" The user is called null, Illegal users ");
exchange.getResponse().setStatusCode(HttpStatus.NOT_ACCEPTABLE);
return exchange.getResponse().setComplete();
}
// Pass it to the next filter
return chain.filter(exchange);
}
@Override
public int getOrder() {
return 0;
}
}

copyright:author[Huaguo shuilian],Please bring the original link to reprint, thank you. https://en.javamana.com/2022/01/202201270547067560.html