gateway url適配

由於版本迭代,有的時候需要做老接口的兼容適配,有很多種方式,比如在nginx配置路由規則,在網關配置路由規則,本篇通過使用反射的方式在網關層全局過濾器中重寫request請求的方式實現url適配。

import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang3.StringUtils;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

import java.lang.reflect.Field;
import java.net.URI;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * URL適配過濾器
 */
@Log4j2
@Component
public class AdapterURLFilter implements GlobalFilter, Ordered {

    /**
     * 路由轉換:k-舊URL,v-新URL
     */
    private static final Map<String, String> adapterURLMap;

    static {
        adapterURLMap = new ConcurrentHashMap<>();
        adapterURLMap.put("oldUrlPath", "newUrlPath");
    }

    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        ServerHttpRequest request = exchange.getRequest();
        URI uri = request.getURI();
        log.info("request original url={}", uri.getPath());
        String newPath = adapterURLMap.get(uri.getPath());
        if (StringUtils.isNotBlank(newPath)) {
            try {
                Class<?> clazz = URI.class;
                Field pathField = clazz.getDeclaredField("path");
                pathField.setAccessible(true);
                pathField.set(uri, newPath);
            } catch (Exception e) {
                log.error("@@AdapterURLFilter@@ error! oldPath={}, newPath={}", uri.getPath(),
                        adapterURLMap.get(uri.getPath()), e);
            }
        }
        return chain.filter(exchange);
    }

    @Override
    public int getOrder() {
        return 0;
    }
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章