Spring Cloud(十三)Zuul 服务网关

 

 

Table of Contents

1.Zuul 介绍

2.pom.xml中添加依赖

3.代码实现

4.zuul 请求过滤

 


 

1.Zuul 介绍

https://github.com/Netflix/zuul/wiki

Zuul是从设备和网站到Netflix流媒体应用程序后端的所有请求的前门。作为边缘服务应用程序,Zuul旨在实现动态路由,监控,弹性和安全性。它还可以根据需要将请求路由到多个Amazon Auto Scaling组。

Netflix API流量的数量和多样性有时会导致生产问题迅速出现而没有警告。我们需要一个允许我们快速改变行为以对这些情况做出反应的系统。

Zuul使用了各种不同类型的过滤器,这使我们能够快速灵活地将功能应用于边缘服务。这些过滤器帮助我们执行以下功能:

  • 身份验证和安全性-识别每个资源的身份验证要求,并拒绝不满足要求的请求。

  • 见解和监控-在边缘跟踪有意义的数据和统计信息,以便为我们提供准确的生产视图。

  • 动态路由-根据需要将请求动态路由到不同的后端群集。

  • 压力测试-逐渐增加到群集的流量以评估性能。

  • 减载-为每种类型的请求分配容量,并丢弃超出限制的请求。

  • 静态响应处理-直接在边缘构建一些响应,而不是将其转发到内部集群

  • 多区域弹性-跨AWS区域路由请求,以多样化我们的ELB使用并将我们的优势拉近我们的成员

 

2.pom.xml中添加依赖

<dependencies>
        <!--整合 zuul-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-zuul</artifactId>
        </dependency>
        <!--eureka-server-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <dependency>
            <groupId>com.ak.demo</groupId>
            <artifactId>api-commons</artifactId>
            <version>${project.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
        </dependency>
    </dependencies>

3.代码实现

server:
  port: 9528

spring:
  application:
    name: gateway-zuul-service
zuul:
  routes:
    PAYMENT-SERVICE: /ps/**

eureka:
  client:
    register-with-eureka: true #表示向注册中心注册自己 默认为true
    fetch-registry: true #是否从EurekaServer抓取已有的注册信息,默认为true,单节点无所谓,集群必须设置为true才能配合ribbon使用负载均衡
    service-url:
#      defaultZone: http://eureka7001.com:7001/eureka/, http://eureka7002.com:7002/eureka/ # 入驻集群地址
      defaultZone: http://eureka7001.com:7001/eureka/
  instance:
    hostname: gateway-zuul-service
@EnableZuulProxy
@EnableDiscoveryClient
@EnableEurekaClient
@SpringBootApplication
public class GatewayZuul9528 {
    public static void main(String[] args) {
        SpringApplication.run(GatewayZuul9528.class,args);
    }
}

 

4.zuul 请求过滤

@Slf4j
@Component
public class AccessFilter extends ZuulFilter {
    @Override
    public String filterType() {
        return FilterConstants.PRE_TYPE;
    }

    @Override
    public int filterOrder() {
        //该过滤器顺序要 > 5,才能得到 serviceid
        return FilterConstants.PRE_DECORATION_FILTER_ORDER+1;
    }

    @Override
    public boolean shouldFilter() {
        //对指定的serviceid过滤,如果要过滤所有服务,直接返回 true
        RequestContext ctx = RequestContext.getCurrentContext();
        String serviceId = (String) ctx.get(FilterConstants.SERVICE_ID_KEY);
        log.info("serviceId:{}",serviceId);
        if(!serviceId.equalsIgnoreCase("PAYMENT-SERVICE")) {
            log.info("w┭┮﹏┭┮");
            return false;
        }
        return true;
    }

    @Override
    public Object run() throws ZuulException {
        RequestContext ctx = RequestContext.getCurrentContext();
        HttpServletRequest req = ctx.getRequest();
        String token = req.getParameter("token");
        if (token == null) {
            //此设置会阻止请求被路由到后台微服务
            ctx.setSendZuulResponse(false);
            //向客户端的响应
            ctx.setResponseStatusCode(200);
            ctx.setResponseBody("error : not token");
        }
        //zuul过滤器返回的数据设计为以后扩展使用,
        //目前该返回值没有被使用
        return null;
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值