API网关服务SpringCloudZuul

布满荆棘的人生 2022-05-08 05:52 460阅读 0赞

API网关服务SpringCloudZuul

文章目录

    • API网关服务SpringCloudZuul
      • 快速入门
      • 请求路由
        • 传统路由方式
        • 面向服务的路由
      • 请求过滤
      • 路由详解
        • 传统路由配置
        • 服务路由配置
      • 忽略表达式
      • 路由前缀
      • Cookie 与头信息
      • 重定向问题
      • Hystrix 和 Ribbon 支持

快速入门

创建项目名为 zuul

pom 依赖

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  3. <modelVersion>4.0.0</modelVersion>
  4. <groupId>com.zk.springcloud.zuul</groupId>
  5. <artifactId>springcloud-zuul</artifactId>
  6. <version>0.0.1-SNAPSHOT</version>
  7. <packaging>jar</packaging>
  8. <name>springcloud-zuul</name>
  9. <description>Demo project for Spring Boot</description>
  10. <parent>
  11. <groupId>org.springframework.boot</groupId>
  12. <artifactId>spring-boot-starter-parent</artifactId>
  13. <version>2.0.5.RELEASE</version>
  14. <relativePath/> <!-- lookup parent from repository -->
  15. </parent>
  16. <properties>
  17. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  18. <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
  19. <java.version>1.8</java.version>
  20. <spring-cloud.version>Finchley.SR1</spring-cloud.version>
  21. </properties>
  22. <dependencies>
  23. <dependency>
  24. <groupId>org.springframework.cloud</groupId>
  25. <artifactId>spring-cloud-starter-netflix-zuul</artifactId>
  26. </dependency>
  27. <dependency>
  28. <groupId>org.springframework.cloud</groupId>
  29. <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
  30. </dependency>
  31. </dependencies>
  32. <dependencyManagement>
  33. <dependencies>
  34. <dependency>
  35. <groupId>org.springframework.cloud</groupId>
  36. <artifactId>spring-cloud-dependencies</artifactId>
  37. <version>${spring-cloud.version}</version>
  38. <type>pom</type>
  39. <scope>import</scope>
  40. </dependency>
  41. </dependencies>
  42. </dependencyManagement>
  43. <build>
  44. <plugins>
  45. <plugin>
  46. <groupId>org.springframework.boot</groupId>
  47. <artifactId>spring-boot-maven-plugin</artifactId>
  48. </plugin>
  49. </plugins>
  50. </build>
  51. </project>

启动类

  1. @SpringBootApplication @EnableZuulProxy @EnableEurekaClient public class SpringcloudZuulApplication { public static void main(String[] args) { SpringApplication.run(SpringcloudZuulApplication.class, args); } }

配置文件

  1. spring.application.name=api-gateway
  2. server.port=9002
  3. eureka.client.service-url.defaultZone=http://localhost:9001/eureka/,http://localhost:9004/eureka/

请求路由

传统路由方式

添加如下配置

  1. zuul.routes.feign-consumer.path=/feign-consumer/**
  2. zuul.routes.feign-consumer.url=http://localhost:9010/

访问 http://localhost:9002/feign-consumer/feign-consumer 可获取到数据

面向服务的路由

添加如下配置

  1. zuul.routes.feign-consumer.path=/feign-consumer/**
  2. zuul.routes.feign-consumer.service-id=FEIGN-CONSUMER

访问 http://localhost:9002/feign-consumer/feign-consumer 获得相同结果

请求过滤

  1. package com.zk.springcloud.zuul; import com.netflix.zuul.ZuulFilter; import com.netflix.zuul.context.RequestContext; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import javax.servlet.http.HttpServletRequest; public class AccessFilter extends ZuulFilter { private static Logger log = LoggerFactory.getLogger(AccessFilter.class); @Override public String filterType() { return "pre"; } @Override public int filterOrder() { return 0; } @Override public boolean shouldFilter() { return true; } @Override public Object run() { RequestContext ctx = RequestContext.getCurrentContext(); HttpServletRequest request = ctx.getRequest(); log.info("send {} request to {}", request.getMethod(), request.getRequestURL().toString()); Object accessToken = request.getParameter("accessToken"); if(accessToken == null) { log.warn("access token is empty"); ctx.setSendZuulResponse(false); ctx.setResponseStatusCode(401); return null; } log.info("access token ok"); return null; } }

启动类添加

  1. @Bean public AccessFilter accessFilter(){ return new AccessFilter(); }

访问 http://localhost:9002/feign-consumer/feign-consumer?accessToken 正常返回数据

访问 http://localhost:9002/feign-consumer/feign-consumer 返回 401

路由详解

传统路由配置

单例模式

  1. zuul.routes.feign-consumer.path=/feign-consumer/**
  2. zuul.routes.feign-consumer.url=http://localhost:9010/

多实例配置

  1. zuul.routes.feign-consumer.path=/service-user/**
  2. zuul.routes.feign-consumer.service-id=SERIVCE-USER-CUSTOM
  3. ribbon.eureka.enabled=false
  4. SERIVCE-USER-CUSTOM.ribbon.listOfServers=http://localhost:9003,http://localhost:9006

服务路由配置

  1. zuul.routes.feign-consumer.path=/feign-consumer/**
  2. zuul.routes.feign-consumer.service-id=FEIGN-CONSUMER

也可以使用

  1. zuul.routes.SERIVCE-USER=/service-user/**

忽略表达式

  1. zuul.ignored-patterns=/**/hello/**
  2. zuul.routes.service-user.path=/service-user/**
  3. zuul.routes.service-user.service-id=SERIVCE-USER

访问 http://localhost:9002/service-user/hello?accessToken 此时这个链接不能访问

路由前缀

增加前缀

  1. zuul.prefix=/api

关闭移除代理前缀

  1. zuul.stripPrefix=false

关闭指定路由移除代理前缀

  1. zuul.routes.<route>.strip-prefix=true

默认的敏感头信息通过 zuul.sensitiveHeaders 参数定义,包括 Cookie、Set-Cookie、Authorization 进行过滤

设置 Cookie 可传递

通过设置全局参数为空来覆盖默认值

  1. zuul.sensitiveHeaders=

或者通过指定路由的参数配置

  1. # 方法一:对指定路由开启自定义敏感头
  2. zuul.routes.<router>.customSensitiveHeaders=true
  3. # 方法二:将指定路由的敏感头设置为空
  4. zuul.routes.<router>.sensitiveHeaders=

重定向问题

  1. zuul.addHostHeader=true

只在 Camden 版本起作用

Hystrix 和 Ribbon 支持

  1. #设置API网关中路由转发请求的 HystrixCommand 执行超时时间,单位为毫秒
  2. hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds
  3. #设置路由转发,创建请求连接的超时时间
  4. ribbon.ConnectTimeout
  5. #设置路由转发请求的超时时间
  6. ribbon.ReadTimeout
  7. #关闭重试机制
  8. zuul.retryable=false
  9. zuul.routes.<route>.retryable=false

发表评论

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

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

相关阅读