美文网首页编程语言爱好者RxJavaJava服务器端编程
Zuul的反向代理、过滤及动态网关配置实例

Zuul的反向代理、过滤及动态网关配置实例

作者: 迦叶_金色的人生_荣耀而又辉煌 | 来源:发表于2021-11-21 09:55 被阅读0次

上一篇 <<<Gateway网关转发demo
下一篇 >>>Gateway高可用集群与动态网关


Spring Cloud Zuul可以理解为一个集网关(路由)、负载均衡、校验过滤、结合服务治理框架、请求转发时熔断机制、服务聚合等 一系列功能。我们可以将Zuul当成一个门面,所有外部请求都经过Zuul的转发到具体的服务实例,减少了每个服务之间互相鉴权代码冗余问题,统一交给Zuul进行鉴权,在此基础上集成上边说的高级功能。路由功能相当于反向代理。

1.反向代理功能

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-zuul</artifactId>
</dependency>

### 配置网关反向代理    
zuul:
  routes:
    api-a:
     ### 以 /api-member/访问转发到会员服务
      path: /api-member/**
      serviceId: app-jarye-member
    api-b:
        ### 以 /api-order/访问转发到订单服务
      path: /api-order/**
      serviceId: app-jarye-order

2.网关的过滤功能

@Component
public class TokenFilter extends ZuulFilter {

    public Object run() throws ZuulException {
        // 获取上下文
        RequestContext currentContext = RequestContext.getCurrentContext();
        HttpServletRequest request = currentContext.getRequest();
        String userToken = request.getParameter("userToken");
        if (StringUtils.isEmpty(userToken)) {
            currentContext.setSendZuulResponse(false);
            currentContext.setResponseStatusCode(401);
            currentContext.setResponseBody("userToken is null");
            return null;
        }
        // 否则正常执行业务逻辑.....
        return null;
    }

    // 判断过滤器是否生效
    public boolean shouldFilter() {

        return true;
    }

    // 过滤器的执行顺序。当请求在一个阶段的时候存在多个多个过滤器时,需要根据该方法的返回值依次执行
    public int filterOrder() {

        return 0;
    }

    // 过滤器类型 pre 表示在 请求之前进行拦截
    public String filterType() {

        return "pre";
    }

}

3.动态网关配置

Git动态配置
### 配置网关反向代理    
zuul:
  routes:
    api-a:
     ### 以 /api-member/访问转发到会员服务
      path: /api-member/**
      serviceId: app-jarye-member
    api-b:
        ### 以 /api-order/访问转发到订单服务
      path: /api-order/**
serviceId: app-jarye-order


<!-- actuator监控中心 -->
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
    <!-- springcloud config 2.0 -->
    <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-config-client</artifactId>
    </dependency>

###网关名称  
spring:
  application:
    name: service-zuul
  cloud:
    config:
    ####读取后缀
      profile: dev
      ####读取config-server注册地址
      discovery:
        service-id: config-server
        enabled: true    

###默认服务读取eureka注册服务列表 默认间隔30秒

###开启所有监控中心接口
management:
  endpoints:
    web:
      exposure:
        include: "*"

// zuul配置能够使用config实现实时更新
  @RefreshScope
  @ConfigurationProperties("zuul")
  public ZuulProperties zuulProperties() {
    return new ZuulProperties();
  }

手动刷新:
http://127.0.0.1/actuator/refresh


推荐阅读:
<<<网关背景分类及常用框架
<<<微服务网关与过滤器的区别
<<<Nginx与Zuul的区别
<<<Zuul与Gateway有哪些区别
<<<Nginx与网关的区别
<<<如何保证微服务接口的安全
<<<Gateway网关转发demo
<<<Gateway高可用集群与动态网关
<<<Gateway的谓词配置实例
<<<Gateway配置及流程分析

相关文章

网友评论

    本文标题:Zuul的反向代理、过滤及动态网关配置实例

    本文链接:https://www.haomeiwen.com/subject/suppzltx.html