SpringCloud之路由网关zuul(五)

系统管理员 2022-05-14 06:37 337阅读 0赞

在微服务架构中,需要几个基础的服务治理组件,包括服务注册与发现、服务消费、负载均衡、断路器、智能路由、配置管理等,由这几个基础组件相互协作,共同组建了一个简单的微服务系统。一个简答的微服务系统如下图:

70

注意:A服务和B服务是可以相互调用的,作图的时候忘记了。并且配置服务也是注册到服务注册中心的。

在Spring Cloud微服务系统中,一种常见的负载均衡方式是,客户端的请求首先经过负载均衡(zuul、Ngnix),再到达服务网关(zuul集群),然后再到具体的服。,服务统一注册到高可用的服务注册中心集群,服务的所有的配置文件由配置服务管理(下一篇文章讲述),配置服务的配置文件放在git仓库,方便开发人员随时改配置。

一、Zuul简介

Zuul的主要功能是路由转发和过滤器。路由功能是微服务的一部分,比如/api/user转发到到user服务,/api/shop转发到到shop服务。zuul默认和Ribbon结合实现了负载均衡的功能。

zuul有以下功能:

  • Authentication
  • Insights
  • Stress Testing
  • Canary Testing
  • Dynamic Routing
  • Service Migration
  • Load Shedding
  • Security
  • Static Response handling
  • Active/Active traffic management

二、准备工作

继续使用上一节的工程。在原有的工程上,创建一个新的工程。

三、创建service-zuul工程

其pom.xml文件如下:

  1. <?xml version=”1.0” encoding=”UTF-8”?>

    4.0.0
  1. <groupId>com.forezp</groupId>
  2. <artifactId>service-zuul</artifactId>
  3. <version>0.0.1-SNAPSHOT</version>
  4. <packaging>jar</packaging>
  5. <name>service-zuul</name>
  6. <description>Demo project for Spring Boot</description>
  7. <parent>
  8. <groupId>com.forezp</groupId>
  9. <artifactId>sc-f-chapter5</artifactId>
  10. <version>0.0.1-SNAPSHOT</version>
  11. </parent>
  12. <dependencies>
  13. <dependency>
  14. <groupId>org.springframework.cloud</groupId>
  15. <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
  16. </dependency>
  17. <dependency>
  18. <groupId>org.springframework.boot</groupId>
  19. <artifactId>spring-boot-starter-web</artifactId>
  20. </dependency>
  21. <dependency>
  22. <groupId>org.springframework.cloud</groupId>
  23. <artifactId>spring-cloud-starter-netflix-zuul</artifactId>
  24. </dependency>
  25. </dependencies>
  26. </project>

在其入口applicaton类加上注解@EnableZuulProxy,开启zuul的功能:

  1. @SpringBootApplication

    @EnableZuulProxy

    @EnableEurekaClient

    @EnableDiscoveryClient

    public class ServiceZuulApplication {

  1. public static void main(String[] args) {
  2. SpringApplication.run( ServiceZuulApplication.class, args );
  3. }
  4. }

加上配置文件application.yml加上以下的配置代码:

  1. eureka:

    client:

    serviceUrl:

    defaultZone: http://localhost:8761/eureka/

    server:

    port: 8769

    spring:

    application:

    name: service-zuul

    zuul:

    routes:

    api-a:

    path: /api-a/**

    serviceId: service-ribbon

    api-b:

    path: /api-b/**

    serviceId: service-feign

首先指定服务注册中心的地址为http://localhost:8761/eureka/,服务的端口为8769,服务名为service-zuul;以/api-a/ 开头的请求都转发给service-ribbon服务;以/api-b/开头的请求都转发给service-feign服务;

四、服务过滤

zuul不仅只是路由,并且还能过滤,做一些安全验证。继续改造工程;

  1. @Component

    public class MyFilter extends ZuulFilter {

  1. private static Logger log = LoggerFactory.getLogger(MyFilter.class);
  2. @Override
  3. public String filterType() {
  4. return "pre";
  5. }
  6. @Override
  7. public int filterOrder() {
  8. return 0;
  9. }
  10. @Override
  11. public boolean shouldFilter() {
  12. return true;
  13. }
  14. @Override
  15. public Object run() {
  16. RequestContext ctx = RequestContext.getCurrentContext();
  17. HttpServletRequest request = ctx.getRequest();
  18. log.info(String.format("%s >>> %s", request.getMethod(), request.getRequestURL().toString()));
  19. Object accessToken = request.getParameter("token");
  20. if(accessToken == null) {
  21. log.warn("token is empty");
  22. ctx.setSendZuulResponse(false);
  23. ctx.setResponseStatusCode(401);
  24. try {
  25. ctx.getResponse().getWriter().write("token is empty");
  26. }catch (Exception e){}
  27. return null;
  28. }
  29. log.info("ok");
  30. return null;
  31. }
  32. }

filterType:返回一个字符串代表过滤器的类型,在zuul中定义了四种不同生命周期的过滤器类型,具体如下:

  • pre:路由之前
  • routing:路由之时
  • post: 路由之后
  • error:发送错误调用
  • filterOrder:过滤的顺序
  • shouldFilter:这里可以写逻辑判断,是否要过滤,本文true,永远过滤。
  • run:过滤器的具体逻辑。可用很复杂,包括查sql,nosql去判断该请求到底有没有权限访问。

发表评论

表情:
评论列表 (有 0 条评论,337人围观)

还没有评论,来说两句吧...

相关阅读

    相关 SpringCloudzuul

    1.场景还原      在微服务架构中,zuul作为springcloud重要的网关路由组件,为前端统一的ip请求提供了极大的便捷;笔者在实际项目中,通过微服务模块名路由

    相关 SpringCloudzuul

    在微服务架构中,需要几个基础的服务治理组件,包括服务注册与发现、服务消费、负载均衡、断路器、智能路由、配置管理等,由这几个基础组件相互协作,共同组建了一个简单的微服务系统。一个