抱歉,您的浏览器无法访问本站
本页面需要浏览器支持(启用)JavaScript
了解详情 >

Spring Cloud Zuul介绍

Zuul是Netflix开源的微服务网关,可以和Eureka、Ribbon、Hystrix等组件配合使用,Spring Cloud对Zuul进行了整合与增强,Zuul默认使用的HTTP客户端是Apache HTTPClient,也可以使用RestClient或okhttp3.OkHttpClient。 Zuul的主要功能是路由转发和过滤器。路由功能是微服务的一部分,如下图所示,/test/*转发到到demo服务。zuul默认和Ribbon结合实现了负载均衡的功能.。

https://img-blog.csdnimg.cn/20200331140854143.jpg?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzM2MzU3MjQy,size_16,color_FFFFFF,t_70

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

  1. 身份验证和安全性 - 确定每个资源的身份验证要求并拒绝不满足这些要求的请求
  2. 洞察和监控 - 在边缘跟踪有意义的数据和统计数据,以便为我们提供准确的生产视图
  3. 动态路由 - 根据需要动态地将请求路由到不同的后端群集
  4. 压力测试 - 逐渐增加群集的流量以衡量性能。
  5. Load Shedding - 为每种类型的请求分配容量并删除超过限制的请求静态响应处理 - 直接在边缘构建一些响应,而不是将它们转发到内部集群

Zuul实现路由转发和过滤器

consumer-zuul子项目搭建

继续在之前的聚合工程中创建子项目,consumer-zuul

项目结构

https://img-blog.csdnimg.cn/20200331141340342.jpg?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzM2MzU3MjQy,size_16,color_FFFFFF,t_70

pom文件

只需要引入zuul包即可

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

yml配置

server:
  port: 13000
eureka:
  client:
    service-url:
      defaultZone: http://eureka01:8800/eureka/,http://eureka02:8810/eureka/

#构建路由地址
zuul:
routes:
#这里可以自定义
demo1:
#匹配的路由规则
path: /consumer-zuul-a/**
#路由的目标服务名
serviceId: provider
demo2:
#匹配的路由规则
path: /consumer-zuul-b/**
#路由的目标服务名
serviceId: consumer-feign

spring:
application:
name: consumer-zuul
main:
allow-bean-definition-overriding: true

启动类

加入注解 @EnableZuulProxy

@SpringBootApplication
@EnableZuulProxy
public class ConsumerZuulApplication {

    public static void main(String[] args) {
        SpringApplication.run(ConsumerZuulApplication.class, args);
    }

}

启动
同时启动yml对应的服务

https://img-blog.csdnimg.cn/20200331141911655.jpg?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzM2MzU3MjQy,size_16,color_FFFFFF,t_70

Zuul实现过滤器

ServiceFilter类

@Component
public class ServiceFilter extends ZuulFilter {

    @Override
    public String filterType() {
        //filterType 为过滤类型,可选值有 pre(路由之前)、routing(路由之时)、post(路由之后)、error(发生错误时调用)。
        return "pre";
    }
 
    @Override
    public int filterOrder() {
        //filterOrdery 为过滤的顺序,如果有多个过滤器,则数字越小越先执行
        return 0;
    }
 
    @Override
    public boolean shouldFilter() {
        //shouldFilter 表示是否过滤,这里可以做逻辑判断,true 为过滤,false 不过滤
        return true;
    }
 
    @Override
    public Object run() throws ZuulException {
        //run 为过滤器执行的具体逻辑,在这里可以做很多事情,比如:权限判断、合法性校验等。
        //这里写校验代码
        RequestContext context = RequestContext.getCurrentContext();
        HttpServletRequest request = context.getRequest();
        String name = request.getParameter("name");
        if(!"12345".equals(name)){
            context.setSendZuulResponse(false);
            context.setResponseStatusCode(401);
            try {
                context.getResponse().setCharacterEncoding("UTF-8");
                context.getResponse().getWriter().write("名字错了");
            }catch (Exception e){}
        }
        return null;
    }
}

运行

https://img-blog.csdnimg.cn/20200331142312338.jpg?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzM2MzU3MjQy,size_16,color_FFFFFF,t_70

评论