1.配置文件:

# 服务名称
spring:
  application:
    name: spring-cloud-gateway
  # 开启 Gateway 服务注册中心服务发现
  cloud:
    gateway:
      discovery:
        locator:
          enabled: true
      default-filters:
        - CustomRewriteRouteFilter=true     ###自定义过滤器必须有
      routes:
        - id: search
          uri: http://11.11.111.1111:1111  ###这个随便写个,不能为空
          order: 0
          predicates:
            - Path=/search/**
# Eureka Server 配置
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/
# 配置Gateway日志等级,输出转发细节信息
logging:
  level:
    org.springframework.cloud.gateway: debug

2.自定义过滤器

package org.lexue.gateway.filter;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.cloud.gateway.filter.GatewayFilter;
import org.springframework.cloud.gateway.filter.factory.AbstractGatewayFilterFactory;
import org.springframework.cloud.gateway.route.Route;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.util.MultiValueMap;
import org.springframework.web.util.UriComponentsBuilder;

import java.net.InetSocketAddress;
import java.net.URI;

import static org.springframework.cloud.gateway.support.ServerWebExchangeUtils.GATEWAY_ROUTE_ATTR;

/**
 *  @ClassName CustomRewriteRouteFilter
 *  @Author ShuYu Liu
 *  @Description 重置url
 *  @Date 2020/6/20 15:27
 */
public class CustomRewriteRouteFilter extends AbstractGatewayFilterFactory {
    private static final Log log = LogFactory.getLog(CustomRewriteRouteFilter.class);
    
    @Override
    public GatewayFilter apply(Object config) {
        return (exchange, chain) -> {
            //获取url地址
            ServerHttpRequest request = exchange.getRequest();
            String rawPath = request.getURI().getRawPath();
            //请求头
            HttpHeaders headers = request.getHeaders();
            //请求方法
            HttpMethod method = request.getMethod();
            //请求参数
            MultiValueMap < String, String > queryParams = request.getQueryParams();
            String appid = headers.get("appid").get(0);
            URI uri;
            boolean isTrue = appid.equals("a1");
            log.info(isTrue);
            if (isTrue){
               log.info("201");
                uri = UriComponentsBuilder.fromHttpUrl("http://110.110.200.201:10000"+rawPath).queryParams(queryParams).build().toUri();
            }else if (appid.equals("a2")){
                log.info("211");
                uri = UriComponentsBuilder.fromHttpUrl("http://110.110.200.211:10000"+rawPath).queryParams(queryParams).build().toUri();
            }else {
                throw new NullPointerException("找不到");
            }
            //替换新的url地址
            ServerHttpRequest serverHttpRequest = request.mutate().uri(uri).method(method).headers(
                httpHeaders -> httpHeaders=headers).build();
            Route route = exchange.getAttribute(GATEWAY_ROUTE_ATTR);
            //从新设置Route地址
            Route newRoute =
                Route.async().asyncPredicate(route.getPredicate()).filters(route.getFilters()).id(route.getId())
                    .order(route.getOrder()).uri(uri).build();
            exchange.getAttributes().put(GATEWAY_ROUTE_ATTR,newRoute);
            return chain.filter(exchange.mutate().request(serverHttpRequest).build());
        };
    }
}

3.过滤工厂,注入过滤器

package org.lexue.gateway.config;

import org.lexue.gateway.filter.CustomRewriteRouteFilter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 *  @ClassName FilterFactory
 *  @Author ShuYu Liu
 *  @Description 过滤工厂
 *  @Date 2020/6/20 15:52
 */
@Configuration
public class FilterFactory {
    @Bean()
    public CustomRewriteRouteFilter buildCustomRewriteRouteFilter() {
        return new CustomRewriteRouteFilter();
    }
}

 

Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐