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