Zuul
2024-07-20 18:33:34 0 举报
AI智能生成
网关
作者其他创作
大纲/内容
网关存在的意义
架构图
DEMO配置
spring:<br> application:<br>name: zuul-gateway<br>eureka:<br> instance:<br>hostname: localhost<br> client:<br>serviceUrl:<br> defaultZone: http://localhost:8761/eureka/<br>zuul:<br> routes:<br>ServiceB:<br> path: /demo/**<br>#所有针对http://localhost:9000/demo/**的请求,都会转发给服务B的其中一个服务
请求头配置
zuul:<br> sensitiveHeaders: accept-language, cookiei<br> routes:<br>demo:<br> sensitiveHeaders: cookie
路由映射信息
management.security.enabled = false
zuul-gateway中引入actuator项目通过<br>访问/routes地址看到路由的映射信息<br>
hystrix配置
与ribbon整合转发时,会使用RibbonRoutingilter,转发会使用hystrix包裹请求,如果请求失败,会执行fallback逻辑
子主题
zuul:<br> routes:<br>ServiceB:<br> path: /ServiceB/**
禁用过滤器<br>
zuul:<br> SendForwardFilter:<br>route:<br> disable: true
@EnableZuulServer<br>
自动禁用掉 PreDecorationFilter、RibbonRoutingFilter、SimpleHostRoutingFilter<br>
@EnableZuulProxy<br>
启用Zuul网关
源码分析
全局流程图
请求的入口处理流程图
子主题
源码跟踪
子主题
Zuul过滤器
pre过滤器
优先级
-3:ServletDetectionFilter<br>-2:Servlet30WrapperFilter<br>-1:FromBodyWrapperFilter<br>1:DebugFilter<br>5:PreDecorationFilter<br>
处理流程图
pre过滤器处理2
routing过滤器<br>
优先级
10:RibbonRoutingFilter<br>
ribbon路由<br>
配置
zuul:<br> routes:<br>ServiceB:<br> path: /demo/**<br> serviceId: ServiceB<br>或<br>zuul:<br> routes:<br>ServiceB:<br> path: /demo/**
类<br>
自定义路由规则<br>
@Configuration<br>public class MyRouteRuleConfig {<br><br>@Bean<br>public PatternServiceRouteMapper patternServiceRouteMapper() {<br>return new PatternServiceRouteMapper(“(zuul)-(?<test>.+)-(service)”, “${test}/**”);<br>}<br><br>}<br>请求:test/**的路径,转发给zuul-test-service<br>
默认
RibbonRoutingFilter
100:SimpleHostRoutingFilter
简单路由<br>
配置<br>
zuul:<br> routes:<br>demo:<br> path: /ServiceB/**<br> url: http://localhost:9090/ServiceB
配置连接到目标主机的最大http连接数(默认200)<br>
zuul.host.maxTotalConnections
每个主机的初始连接数(默认20)
zuul.host.maxPerRouteConnections
类
SimpleHostRoutingFilter<br>
500:SendForwardFilter
跳转路由<br>
配置<br>
zuul:<br> routes:<br>demo:<br> path: /test/**<br> url: forward: /gateway/sayHello
自己跳转到自己网关工程里的一个接口
类
SendForwardFilter<br>
route过滤器处理
post过滤器
优先级
1000:SendResponseFilter
处理流程图见 route
error过滤器<br>
0:SendErrorFilter
子主题
自定义过滤器<br>
启动就加载
子主题
动态加载过滤器
pom引入
<dependency><br><groupId>org.codehaus.groovy</groupId><br><artifactId>groovy-all</artifactId><br><version>2.4.12</version><br></dependency><br>
在Application类里<br>
子主题
自定义过滤器
子主题
0 条评论
下一页