GateWay routing forwarding and filtering

This is done in a Gateway project with eureka and other components

server: port: 9006 spring: application: name: zhao-service-gateway cloud: gateway: routes: - id: Service -autodeliver router #uri: http://127.0.0.1:8091 uri: lb:// zhao-autodeliver predicates: -path = /autodeliver/** -id: service-resume-router #uri: http://127.0.0.1:8081 uri: lb://zhao-service-resume predicates: - Path=/resume/** filters: - StripPrefix=1Copy the code

With the first hao-service-AutoDeliver configuration, both fixed IP and service name can be used to access the service through the gateway project, but the fixed IP is not flexible, while LB :// Zhao-service-AutoDeliver can achieve random load balancing. And you don’t have to fill in a fixed IP address to avoid the troubleFilters: -stripprefix =1 in the second service configuration. This configuration will filter out the first path configuration, so we will add the original configuration in addition to the first filter configuration in the last access. The format of the visit is as follows

GateWay assertion

The above configuration for paths is the configuration for predicates, and the Gateway also has several predicates built inBasically, the above assertions are filtered based on the information carried by the request, which can be combined to achieve the desired operation

GateWay defines a global filter

@Component @Slf4j public class BlackListFilter implements GlobalFilter, Ordered{ private static final List<String> blackList=new ArrayList<>(); static { blackList.add("0:0:0:0:0:0:0:1"); } @override public <Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) { ServerHttpRequest request = exchange.getRequest(); ServerHttpResponse response =exchange.getResponse(); String clientIp = request.getRemoteAddress().getHostString(); if (blackList.contains(clientIp)){ response.setStatusCode(HttpStatus.UNAUTHORIZED); Log. error(clientIp+" access denied in blacklist "); String data = "request be denied"; DataBuffer wrap = response.bufferFactory().wrap(data.getBytes()); return response.writeWith(Mono.just(wrap)); } return chain.filter(exchange); } @Override public int getOrder() { return 0; }}Copy the code

The filter blocks the requests in the blacklist (this operation can be implemented with data storage such as mysql or Redis in practice) to achieve the effect

GateWay’s high availability

GateWay as a very core component, if the failure, then all requests may not be routed processing, so we need to do GateWay high availability. The high availability of GateWay is simple: multiple GateWay instances can be started to achieve high availability, and load balancing devices such as Nginx can be used upstream of GateWay for load forwarding to achieve high availability. Start multiple GateWay instances (let’s say two, one port 9002 and one port 9003), and all that is left is to use Nginx and others to complete the load proxy.

Code address github.com/zhendiao/de…

Welcome to search attention to my public number [micro view technology], and summary of classified interview questions github.com/zhendiao/Ja…