springcloud gateway Custom Assertion Rule Explanation routing at the end of suffix
- 2021-11-29 06:56:46
- OfStack
Directory springcloud gateway custom assertion rules, suffix end routing 1. New 1 route assertion factory ExtCheckRoutePredicateFactory2. Modify gateway configuration 3. Modify gateway source code, add custom assertion class to system predicates Gateway custom route assertion factory class application. yml file route assertion factory configuration class
springcloud gateway Custom Assertion Rule, Routing at End of Suffix
Because of the need of work, we need to use springcloud gateway and route ending with. html for websocket forwarding.
The 8 routing rules of gateway can not be satisfied, so it is necessary to customize the assertion rules.
1. Create a new routing assertion factory ExtCheckRoutePredicateFactory
@Component
public class ExtCheckRoutePredicateFactory extends AbstractRoutePredicateFactory<ExtCheckRoutePredicateFactory.Config> {
public ExtCheckRoutePredicateFactory() {
super(Config.class);
}
@Override
public Predicate<ServerWebExchange> apply(Config config) {
return new Predicate<ServerWebExchange>() {
@Override
public boolean test(ServerWebExchange serverWebExchange) {
String url=serverWebExchange.getRequest().getURI().toString();
if(url.endsWith(".html")){
return true;
}
return false;
}
};
}
public static class Config{
private String name;
public String getName(){
return name;
}
public void setName(String name){
this.name=name;
}
}
}
Matches this route if it ends with. html
2. Modify the gateway configuration
gateway:
routes:
- id: abc
uri: http://localhost:8080
predicates:
- name: ExtCheck
ExtCheck is the prefix name of our new assertion factory, which is automatically recognized.
At this time, the run found that the system could not find our custom assertion class at all.
Step 3 is required
3. Modify the source code of gateway and add the custom assertion class to the system predicates
@Bean
public RouteLocator routeDefinitionRouteLocator(GatewayProperties properties,
List<GatewayFilterFactory> gatewayFilters,
List<RoutePredicateFactory> predicates,
RouteDefinitionLocator routeDefinitionLocator,
ConfigurationService configurationService) {
predicates.add(new ExtCheckRoutePredicateFactory());
return new RouteDefinitionRouteLocator(routeDefinitionLocator, predicates,
gatewayFilters, properties, configurationService);
}
Run again, successfully forwarded according to. html suffix, done!
Gateway Custom Routing Assertion Factory Class
application. yml file
server:
port: 7000
spring:
zipkin:
base-url: http://127.0.0.1:9411/ #zipkin server Request address of
discoveryClientEnabled: false # Jean nacos Think of it as 1 A URL Instead of using it as a service name
sleuth:
sampler:
probability: 1.0 # Percentage of samples
application:
name: api-gateway
cloud:
nacos:
discovery:
server-addr: localhost:8848 # Will gateway Register to nacos
gateway:
discovery:
locator:
enabled: true # Jean gateway From nacos Get service information from
routes:
- id: product_route
uri: lb://service-product
order: 1
predicates:
- Path=/product-serv/**
filters:
- StripPrefix=1
- id: order_route
uri: lb://service-order
order: 1
predicates:
- Path=/order-serv/**
- Age=18,60
filters:
- StripPrefix=1
Routing Assertion Factory Configuration Class
import lombok.Data;
import lombok.NoArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.cloud.gateway.handler.predicate.AbstractRoutePredicateFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import java.util.Arrays;
import java.util.List;
import java.util.function.Predicate;
// This is 1 Custom routing assertion factory classes , There are two requirements
//1 The name must be Configure +RoutePredicateFactory
//2 Must inherit AbstractRoutePredicateFactory< Configuration class >
@Component
public class AgeRoutePredicateFactory extends AbstractRoutePredicateFactory<AgeRoutePredicateFactory.Config> {
// Constructor
public AgeRoutePredicateFactory() {
super(Config.class);
}
// Read the parameter values in the configuration file Assign a value to a property in the configuration class
public List<String> shortcutFieldOrder() {
// The order of this position must correspond to the order of the values in the configuration file
return Arrays.asList("minAge", "maxAge");
}
// Assertion logic
public Predicate<ServerWebExchange> apply(Config config) {
return new Predicate<ServerWebExchange>() {
@Override
public boolean test(ServerWebExchange serverWebExchange) {
//1 Receive the incoming from the foreground age Parameter
String ageStr = serverWebExchange.getRequest().getQueryParams().getFirst("age");
//2 First judge whether it is empty
if (StringUtils.isNotEmpty(ageStr)) {
//3 If not empty , Then route logic judgment is carried out
int age = Integer.parseInt(ageStr);
if (age < config.getMaxAge() && age > config.getMinAge()) {
return true;
} else {
return false;
}
}
return false;
}
};
}
// Configuration class , Used to receive the corresponding parameters in the configuration file
@Data
@NoArgsConstructor
public static class Config {
private int minAge;//18
private int maxAge;//60
}
}