spring cloud gateway转发静态资源

2 篇文章 0 订阅
1 篇文章 0 订阅

最近遇到spring cloud gateway转发静态资源

spring:
    gateway:
      routes:
      	#内部代理
        - id: xxl-job
          uri: lb://xxl-job
          predicates:
            - Path=/redirect/**
          filters:
           # 第一个predicates地址, 第二个是要转发的地址, 如果要做动态上下文请参考【附加动态上下文】
            - RewritePath=/redirect/(?<segment>.*), /redirect/$\{segment}
           #外部代理
        - id: proxy-url
          uri: https://blog.csdn.net
          predicates:
            - Path=/proxy/**
          filters:
           # 第一个predicates地址, 第二个是要转发的地址, 如果要做动态上下文请参考【附加动态上下文】
            - RewritePath=/proxy/(?<segment>.*), /$\{segment}
       

附加动态上下文

import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.filter.OncePerRequestFilter;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 *
 * 动态上下文
 * @author HanKeQi
 * @Date 2021/8/7 4:51 下午
 * @Version 1.0
 */
@Component
@Order(Ordered.HIGHEST_PRECEDENCE)
public class DynamicContextPathFilter extends OncePerRequestFilter {

    private final static Pattern pattern = Pattern.compile("^(?<contextPath>/[^/]+)(?<servletPath>.*)$");

    @Override
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
        Matcher matcher = pattern.matcher(request.getServletPath());
        if(matcher.matches()) {
            final String contextPath = matcher.group("contextPath");
            final String servletPath = matcher.group("servletPath");
 
       
            if(servletPath.trim().isEmpty()) {
                response.sendRedirect(atomicReference.get()+"/");
                return;
            }

            filterChain.doFilter(new HttpServletRequestWrapper(request) {
                @Override
                public String getContextPath() {
                    return contextPath;
                }
                @Override
                public String getServletPath() {
                    return servletPath;
                }
            }, response);
        } else {
            filterChain.doFilter(request, response);
        }
    }

    @Override
    protected String getAlreadyFilteredAttributeName() {
        return this.getClass().getName() + OncePerRequestFilter.ALREADY_FILTERED_SUFFIX;
    }
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Cloud Gateway和Nginx都是流量网关中的代表性产品,它们都可以用于处理请求的路由、转发、限流、负载均衡等功能,但它们之间也存在一些不同点。 Spring Cloud GatewaySpring Cloud生态系统中的一个组件,它是一个基于Spring Framework 5、Project Reactor和Spring Boot 2的反应式网关,可以用于处理HTTP请求,并提供了路由、过滤器、限流、负载均衡等功能。Spring Cloud Gateway支持多种方式的请求转发,如代理转发、反向代理转发等,可以作为微服务架构中的网关使用。Spring Cloud Gateway可以直接集成到Spring Cloud的生态系统中,与其他Spring Cloud组件协同工作,方便进行微服务治理。 Nginx是一个开源的高性能HTTP和反向代理服务器,它可以用于处理HTTP请求、负载均衡、反向代理等。Nginx具有高并发、高性能、低内存占用等特点,可以用于构建高可用的Web应用程序和分布式系统。Nginx可以作为流量网关、反向代理服务器、负载均衡器使用,并且可以用于处理静态资源、动态请求、WebSocket等。Nginx的配置和使用相对简单,可以快速实现流量控制和安全保护。 总的来说,Spring Cloud Gateway和Nginx都是流量网关中的优秀产品,它们各有特点,可以根据具体业务需求进行选择和使用。Spring Cloud Gateway更适合与Spring Cloud生态系统集成,支持反应式编程和断路器机制,而Nginx则更适合处理大量的静态资源和高并发请求。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值