Zuul网关

柔情只为你懂 2022-01-21 07:11 459阅读 0赞

引入依赖

  1. <dependency>
  2. <groupId>org.springframework.cloud</groupId>
  3. <artifactId>spring-cloud-starter-netflix-zuul</artifactId>
  4. </dependency>

1.启动类中注解 @EnableZuulProxy
2.配置转发规则
在这里插入图片描述

默认去除前缀,访问的路径会从path后直接拼接
但是有个问题,服务路径写死了,没有引入负载均衡等
所以不推荐路由规则中写URL 应该写入service ID

引入eureka:

在这里插入图片描述
这样访问可以由注册中心来调度服务

可以简化route为 “服务名:拦截规则”的形式

  1. zuul:
  2. routes:
  3. user-service: /user-service/**

在使用Zuul的过程中,上面讲述的规则已经大大的简化了配置项。但是当服务较多时,配置也是比较繁琐的。因此Zuul就指定了默认的路由规则:

  • 默认情况下,一切服务的映射路径就是服务名本身。

    • 例如服务名为:user-service,则默认的映射路径就是:/user-service/**

也就是说,刚才的映射规则我们完全不配置也是OK.

另外routes里面有一个prefix属性 指定路由前缀

eg:

  1. zuul:
  2. prefix: /api #路径/api/user-service/user/1将会被代理到/user-service/user/1

不想对外暴露的服务,可以设置一个set

  1. zuul:
  2. ignored-services:
  3. - customer-serviece
  4. - hello-service

去除前缀

默认是去除后拼接路径,
strip-prefix属性可以设置为false将原访问path直接接到url或者转发服务上,该属性可以全局配置

过滤器

ZuulFilter是父类,内含4个主要方法

  1. String filterType();//过滤器类型
  2. int filterOrder();//过滤器优先级
  3. boolean shouldFilter();//是否过滤
  4. object run() throws ZuulException;//过滤逻辑

filterType分为4种 pre前置、post后置、error异常、routing路由时执行
在这里插入图片描述
pre用到的最多,多用于身份校验、限流等

拿到request的方法可以参照ZuulFilter的其他前置拦截实现类

  1. RequestContext ctx = RequestContext.getCurrentContext();
  2. HttpServletRequest req = ctx.getRequest(); // 2) 从上下文中获取request对象
  3. String token = req.getParameter("access-token");// 3) 从请求中获取token

拿到token再进行判断

自定义过滤器判断access-token登录filter的Demo代码:

  1. package cn.vivi.filter;
  2. import com.netflix.zuul.ZuulFilter;
  3. import com.netflix.zuul.context.RequestContext;
  4. import com.netflix.zuul.exception.ZuulException;
  5. import org.apache.commons.lang3.StringUtils;
  6. import org.springframework.cloud.netflix.zuul.filters.ZuulProperties;
  7. import org.springframework.cloud.netflix.zuul.filters.support.FilterConstants;
  8. import org.springframework.http.HttpStatus;
  9. import org.springframework.stereotype.Component;
  10. import javax.servlet.http.HttpServletRequest;
  11. @Component
  12. public class LoginFilter extends ZuulFilter {
  13. @Override
  14. public String filterType() {
  15. return FilterConstants.PRE_TYPE;
  16. }
  17. @Override
  18. public int filterOrder() {
  19. return FilterConstants.PRE_DECORATION_FILTER_ORDER - 1;
  20. }
  21. @Override
  22. public boolean shouldFilter() {
  23. return true;
  24. }
  25. @Override
  26. public Object run() throws ZuulException {
  27. RequestContext currentContext = RequestContext.getCurrentContext();
  28. HttpServletRequest request = currentContext.getRequest();
  29. String token = request.getParameter("access-token");
  30. if(StringUtils.isBlank(token)){
  31. // 未登录则拦截 默认为ture放行
  32. currentContext.setSendZuulResponse(false);
  33. // 返回403
  34. currentContext.setResponseStatusCode(HttpStatus.FORBIDDEN.value());
  35. }
  36. return null;
  37. }
  38. }

StringUtils源自于

  1. <dependency>
  2. <groupId>org.apache.commons</groupId>
  3. <artifactId>commons-lang3</artifactId>
  4. </dependency>

可以用于快速判null及空

另外需要设置白名单,在shouldFilter判断是否进行拦截检查,不是所有页面都需要身份验证。

Zuul集成的负载均衡及熔断

  1. zuul:
  2. retryable: true
  3. ribbon:
  4. ConnectTimeout: 250 # 连接超时时间(ms)
  5. ReadTimeout: 2000 # 通信超时时间(ms)
  6. OkToRetryOnAllOperations: true # 是否对所有操作重试
  7. MaxAutoRetriesNextServer: 2 # 同一服务不同实例的重试次数
  8. MaxAutoRetries: 1 # 同一实例的重试次数
  9. hystrix:
  10. command:
  11. default:
  12. execution:
  13. isolation:
  14. thread:
  15. timeoutInMilliseconds: 6000 #超时时长

ribbon的超时时长=(ConnectTimeout+ReadTimeout) * (MaxAutoRetriesNextServer+1)* ( MaxAutoRetries+1)
默认(ConnectTimeout+ReadTimeout)*2
hystrix的熔断时长应大于ribbon的timeout。

zuul对各个服务实现了负载均衡,但是为了保证zuul的高可用,必须将Zuul做成集群,但是用户不能直接调用集群,所以在zuul之上还需要nginx来做zuul的负载均衡,nginx可以利用ip漂移

以下报错信息是由于
版本升级到Dalston.SR4之后feign使用hystrix时启动报如上错误
解决办法:引入hystrix依赖包,不然找不到hystrix-contrib包

  1. java.lang.IllegalStateException: Error processing condition on org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration.propertySourcesPlaceholderConfigurer
  2. at org.springframework.boot.autoconfigure.condition.SpringBootCondition.matches(SpringBootCondition.java:64) ~[spring-boot-autoconfigure-2.0.4.RELEASE.jar:2.0.4.RELEASE]
  3. at org.springframework.context.annotation.ConditionEvaluator.shouldSkip(ConditionEvaluator.java:108) ~[spring-context-5.0.8.RELEASE.jar:5.0.8.RELEASE]
  4. at org.springframework.context.annotation.ConfigurationClassBeanDefinitionReader.loadBeanDefinitionsForBeanMethod(ConfigurationClassBeanDefinitionReader.java:180) ~[spring-context-5.0.8.RELEASE.jar:5.0.8.RELEASE]
  5. at org.springframework.context.annotation.ConfigurationClassBeanDefinitionReader.loadBeanDefinitionsForConfigurationClass(ConfigurationClassBeanDefinitionReader.java:141) ~[spring-context-5.0.8.RELEASE.jar:5.0.8.RELEASE]
  6. at org.springframework.context.annotation.ConfigurationClassBeanDefinitionReader.loadBeanDefinitions(ConfigurationClassBeanDefinitionReader.java:117) ~[spring-context-5.0.8.RELEASE.jar:5.0.8.RELEASE]
  7. at org.springframework.context.annotation.ConfigurationClassPostProcessor.processConfigBeanDefinitions(ConfigurationClassPostProcessor.java:328) ~[spring-context-5.0.8.RELEASE.jar:5.0.8.RELEASE]
  8. at org.springframework.context.annotation.ConfigurationClassPostProcessor.postProcessBeanDefinitionRegistry(ConfigurationClassPostProcessor.java:233) ~[spring-context-5.0.8.RELEASE.jar:5.0.8.RELEASE]
  9. at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanDefinitionRegistryPostProcessors(PostProcessorRegistrationDelegate.java:271) ~[spring-context-5.0.8.RELEASE.jar:5.0.8.RELEASE]
  10. at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:91) ~[spring-context-5.0.8.RELEASE.jar:5.0.8.RELEASE]
  11. at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:694) ~[spring-context-5.0.8.RELEASE.jar:5.0.8.RELEASE]
  12. at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:532) ~[spring-context-5.0.8.RELEASE.jar:5.0.8.RELEASE]
  13. at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:140) ~[spring-boot-2.0.4.RELEASE.jar:2.0.4.RELEASE]
  14. at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:762) [spring-boot-2.0.4.RELEASE.jar:2.0.4.RELEASE]
  15. at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:398) [spring-boot-2.0.4.RELEASE.jar:2.0.4.RELEASE]
  16. at org.springframework.boot.SpringApplication.run(SpringApplication.java:330) [spring-boot-2.0.4.RELEASE.jar:2.0.4.RELEASE]
  17. at org.springframework.boot.SpringApplication.run(SpringApplication.java:1258) [spring-boot-2.0.4.RELEASE.jar:2.0.4.RELEASE]
  18. at org.springframework.boot.SpringApplication.run(SpringApplication.java:1246) [spring-boot-2.0.4.RELEASE.jar:2.0.4.RELEASE]
  19. at com.vivi.DgItemApplication.main(DgItemApplication.java:10) [classes/:na]
  20. Caused by: java.lang.IllegalStateException: Failed to introspect Class [org.springframework.cloud.netflix.hystrix.HystrixCircuitBreakerConfiguration] from ClassLoader [sun.misc.Launcher$AppClassLoader@18b4aac2]
  21. at org.springframework.util.ReflectionUtils.getDeclaredMethods(ReflectionUtils.java:659) ~[spring-core-5.0.8.RELEASE.jar:5.0.8.RELEASE]
  22. at org.springframework.util.ReflectionUtils.doWithMethods(ReflectionUtils.java:556) ~[spring-core-5.0.8.RELEASE.jar:5.0.8.RELEASE]
  23. at org.springframework.util.ReflectionUtils.doWithMethods(ReflectionUtils.java:541) ~[spring-core-5.0.8.RELEASE.jar:5.0.8.RELEASE]
  24. at org.springframework.util.ReflectionUtils.getUniqueDeclaredMethods(ReflectionUtils.java:599) ~[spring-core-5.0.8.RELEASE.jar:5.0.8.RELEASE]
  25. at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.getTypeForFactoryMethod(AbstractAutowireCapableBeanFactory.java:718) ~[spring-beans-5.0.8.RELEASE.jar:5.0.8.RELEASE]
  26. at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.determineTargetType(AbstractAutowireCapableBeanFactory.java:659) ~[spring-beans-5.0.8.RELEASE.jar:5.0.8.RELEASE]
  27. at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.predictBeanType(AbstractAutowireCapableBeanFactory.java:627) ~[spring-beans-5.0.8.RELEASE.jar:5.0.8.RELEASE]
  28. at org.springframework.beans.factory.support.AbstractBeanFactory.isFactoryBean(AbstractBeanFactory.java:1489) ~[spring-beans-5.0.8.RELEASE.jar:5.0.8.RELEASE]
  29. at org.springframework.beans.factory.support.AbstractBeanFactory.isFactoryBean(AbstractBeanFactory.java:1012) ~[spring-beans-5.0.8.RELEASE.jar:5.0.8.RELEASE]
  30. at org.springframework.boot.autoconfigure.condition.BeanTypeRegistry.addBeanTypeForNonAliasDefinition(BeanTypeRegistry.java:180) ~[spring-boot-autoconfigure-2.0.4.RELEASE.jar:2.0.4.RELEASE]
  31. at org.springframework.boot.autoconfigure.condition.BeanTypeRegistry.addBeanTypeForNonAliasDefinition(BeanTypeRegistry.java:160) ~[spring-boot-autoconfigure-2.0.4.RELEASE.jar:2.0.4.RELEASE]
  32. at org.springframework.boot.autoconfigure.condition.BeanTypeRegistry.addBeanType(BeanTypeRegistry.java:153) ~[spring-boot-autoconfigure-2.0.4.RELEASE.jar:2.0.4.RELEASE]
  33. at org.springframework.boot.autoconfigure.condition.BeanTypeRegistry.updateTypesIfNecessary(BeanTypeRegistry.java:215) ~[spring-boot-autoconfigure-2.0.4.RELEASE.jar:2.0.4.RELEASE]
  34. at org.springframework.boot.autoconfigure.condition.BeanTypeRegistry.getNamesForType(BeanTypeRegistry.java:115) ~[spring-boot-autoconfigure-2.0.4.RELEASE.jar:2.0.4.RELEASE]
  35. at org.springframework.boot.autoconfigure.condition.OnBeanCondition.collectBeanNamesForType(OnBeanCondition.java:265) ~[spring-boot-autoconfigure-2.0.4.RELEASE.jar:2.0.4.RELEASE]
  36. at org.springframework.boot.autoconfigure.condition.OnBeanCondition.getBeanNamesForType(OnBeanCondition.java:254) ~[spring-boot-autoconfigure-2.0.4.RELEASE.jar:2.0.4.RELEASE]
  37. at org.springframework.boot.autoconfigure.condition.OnBeanCondition.getMatchingBeans(OnBeanCondition.java:196) ~[spring-boot-autoconfigure-2.0.4.RELEASE.jar:2.0.4.RELEASE]
  38. at org.springframework.boot.autoconfigure.condition.OnBeanCondition.getMatchOutcome(OnBeanCondition.java:116) ~[spring-boot-autoconfigure-2.0.4.RELEASE.jar:2.0.4.RELEASE]
  39. at org.springframework.boot.autoconfigure.condition.SpringBootCondition.matches(SpringBootCondition.java:47) ~[spring-boot-autoconfigure-2.0.4.RELEASE.jar:2.0.4.RELEASE]
  40. ... 17 common frames omitted
  41. Caused by: java.lang.NoClassDefFoundError: com/netflix/hystrix/contrib/javanica/aop/aspectj/HystrixCommandAspect
  42. at java.lang.Class.getDeclaredMethods0(Native Method) ~[na:1.8.0_131]
  43. at java.lang.Class.privateGetDeclaredMethods(Class.java:2701) ~[na:1.8.0_131]
  44. at java.lang.Class.getDeclaredMethods(Class.java:1975) ~[na:1.8.0_131]
  45. at org.springframework.util.ReflectionUtils.getDeclaredMethods(ReflectionUtils.java:641) ~[spring-core-5.0.8.RELEASE.jar:5.0.8.RELEASE]
  46. ... 35 common frames omitted
  47. Caused by: java.lang.ClassNotFoundException: com.netflix.hystrix.contrib.javanica.aop.aspectj.HystrixCommandAspect
  48. at java.net.URLClassLoader.findClass(URLClassLoader.java:381) ~[na:1.8.0_131]
  49. at java.lang.ClassLoader.loadClass(ClassLoader.java:424) ~[na:1.8.0_131]
  50. at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:335) ~[na:1.8.0_131]
  51. at java.lang.ClassLoader.loadClass(ClassLoader.java:357) ~[na:1.8.0_131]
  52. ... 39 common frames omitted
  53. 2019-07-03 11:08:05.485 INFO 9268 --- [ main] ConfigServletWebServerApplicationContext : Closing org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@3561c410: startup date [Wed Jul 03 11:08:05 CST 2019]; parent: org.springframework.context.annotation.AnnotationConfigApplicationContext@1189dd52
  54. 2019-07-03 11:08:05.486 WARN 9268 --- [ main] o.s.boot.SpringApplication : Unable to close ApplicationContext
  55. java.lang.IllegalStateException: Failed to introspect Class [org.springframework.cloud.netflix.hystrix.HystrixCircuitBreakerConfiguration] from ClassLoader [sun.misc.Launcher$AppClassLoader@18b4aac2]
  56. at org.springframework.util.ReflectionUtils.getDeclaredMethods(ReflectionUtils.java:659) ~[spring-core-5.0.8.RELEASE.jar:5.0.8.RELEASE]
  57. at org.springframework.util.ReflectionUtils.doWithMethods(ReflectionUtils.java:556) ~[spring-core-5.0.8.RELEASE.jar:5.0.8.RELEASE]
  58. at org.springframework.util.ReflectionUtils.doWithMethods(ReflectionUtils.java:541) ~[spring-core-5.0.8.RELEASE.jar:5.0.8.RELEASE]
  59. at org.springframework.util.ReflectionUtils.getUniqueDeclaredMethods(ReflectionUtils.java:599) ~[spring-core-5.0.8.RELEASE.jar:5.0.8.RELEASE]
  60. at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.getTypeForFactoryMethod(AbstractAutowireCapableBeanFactory.java:718) ~[spring-beans-5.0.8.RELEASE.jar:5.0.8.RELEASE]
  61. at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.determineTargetType(AbstractAutowireCapableBeanFactory.java:659) ~[spring-beans-5.0.8.RELEASE.jar:5.0.8.RELEASE]
  62. at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.predictBeanType(AbstractAutowireCapableBeanFactory.java:627) ~[spring-beans-5.0.8.RELEASE.jar:5.0.8.RELEASE]
  63. at org.springframework.beans.factory.support.AbstractBeanFactory.isFactoryBean(AbstractBeanFactory.java:1489) ~[spring-beans-5.0.8.RELEASE.jar:5.0.8.RELEASE]
  64. at org.springframework.beans.factory.support.DefaultListableBeanFactory.doGetBeanNamesForType(DefaultListableBeanFactory.java:419) ~[spring-beans-5.0.8.RELEASE.jar:5.0.8.RELEASE]
  65. at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanNamesForType(DefaultListableBeanFactory.java:389) ~[spring-beans-5.0.8.RELEASE.jar:5.0.8.RELEASE]
  66. at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeansOfType(DefaultListableBeanFactory.java:510) ~[spring-beans-5.0.8.RELEASE.jar:5.0.8.RELEASE]
  67. at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeansOfType(DefaultListableBeanFactory.java:502) ~[spring-beans-5.0.8.RELEASE.jar:5.0.8.RELEASE]
  68. at org.springframework.context.support.AbstractApplicationContext.getBeansOfType(AbstractApplicationContext.java:1198) ~[spring-context-5.0.8.RELEASE.jar:5.0.8.RELEASE]
  69. at org.springframework.boot.SpringApplication.getExitCodeFromMappedException(SpringApplication.java:892) [spring-boot-2.0.4.RELEASE.jar:2.0.4.RELEASE]
  70. at org.springframework.boot.SpringApplication.getExitCodeFromException(SpringApplication.java:878) [spring-boot-2.0.4.RELEASE.jar:2.0.4.RELEASE]
  71. at org.springframework.boot.SpringApplication.handleExitCode(SpringApplication.java:864) [spring-boot-2.0.4.RELEASE.jar:2.0.4.RELEASE]
  72. at org.springframework.boot.SpringApplication.handleRunFailure(SpringApplication.java:813) [spring-boot-2.0.4.RELEASE.jar:2.0.4.RELEASE]
  73. at org.springframework.boot.SpringApplication.run(SpringApplication.java:341) [spring-boot-2.0.4.RELEASE.jar:2.0.4.RELEASE]
  74. at org.springframework.boot.SpringApplication.run(SpringApplication.java:1258) [spring-boot-2.0.4.RELEASE.jar:2.0.4.RELEASE]
  75. at org.springframework.boot.SpringApplication.run(SpringApplication.java:1246) [spring-boot-2.0.4.RELEASE.jar:2.0.4.RELEASE]
  76. at com.vivi.DgItemApplication.main(DgItemApplication.java:10) [classes/:na]
  77. Caused by: java.lang.NoClassDefFoundError: com/netflix/hystrix/contrib/javanica/aop/aspectj/HystrixCommandAspect
  78. at java.lang.Class.getDeclaredMethods0(Native Method) ~[na:1.8.0_131]
  79. at java.lang.Class.privateGetDeclaredMethods(Class.java:2701) ~[na:1.8.0_131]
  80. at java.lang.Class.getDeclaredMethods(Class.java:1975) ~[na:1.8.0_131]
  81. at org.springframework.util.ReflectionUtils.getDeclaredMethods(ReflectionUtils.java:641) ~[spring-core-5.0.8.RELEASE.jar:5.0.8.RELEASE]
  82. ... 20 common frames omitted
  83. Caused by: java.lang.ClassNotFoundException: com.netflix.hystrix.contrib.javanica.aop.aspectj.HystrixCommandAspect
  84. at java.net.URLClassLoader.findClass(URLClassLoader.java:381) ~[na:1.8.0_131]
  85. at java.lang.ClassLoader.loadClass(ClassLoader.java:424) ~[na:1.8.0_131]
  86. at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:335) ~[na:1.8.0_131]
  87. at java.lang.ClassLoader.loadClass(ClassLoader.java:357) ~[na:1.8.0_131]
  88. ... 24 common frames omitted
  89. <dependency>
  90. <groupId>org.springframework.cloud</groupId>
  91. <artifactId>spring-cloud-starter-hystrix</artifactId>
  92. </dependency>

发表评论

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

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

相关阅读

    相关 Zuul 简介

    Zuul 网关介绍 由于微服务“各自为政的特性”是微服务的使用非常麻烦 通常会设立“传播者”作为一个统一入口 网关主要实现请求过滤和请求转发

    相关 springcloud zuul

    网关可以对请求进行过滤拦截,转发等,这种跟过滤器不一样,它可以拦截入口,而不是单独一个服务。 业务场景,会员服务和订单服务,用户必须登录才能调用相关接口(即含有userTok

    相关 Zuul

    zuul包含了对请求进行路由和过滤的两个主要功能。 路由功能负责将外部的请求转发到具体的微服务实例上,是实现外部访问统一入口的基础。 过滤功能则负责将请求的处理过程进行干预

    相关 Zuul服务

    微服务调用过程     前面的博客文章已经介绍过spring cloud的服务的相关内容信息,那么我们回顾一下多个微服务的调用过程。微服务一般会由不同的团队去维护,,那么

    相关 Zuul过滤器

    一、zuul网关过滤器 > Zuul中提供了过滤器定义,可以用来过滤代理请求,提供额外功能逻辑。如:权限验证,日志记录等。 > Zuul提供的过滤器是一个父类。父类是

    相关 Zuul

    一、Zuul网关简介 1. zuul是spring cloud中的微服务网关。 网关: 是一个网络整体系统中的前置门户入口。请求首先通过网关,进行路径的路由