Zuul简介
Zuul的核心是一系列的过滤器,主要完成以下功能:
1.身份认证与安全
2.审查与监控
3.动态路由
4.压力测试
5.负载分配
6.静态相应处理
7.多区域弹性
Zuul使用
1、创建maven工程,添加pom.xml依赖
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.RELEASE</version>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-zuul</artifactId>
</dependency>
<!-- 注册到eureka上 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Edgware.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
2、增加启动类,添加@EnableZuulProxy注解,声明zuul代理,该代理使用Ribbon来定位注册在EurekaServer中的微服务;同时,该代理还整合了Hystrix,从而实现了容错,所有经过Zuul的请求都会在Hystrix命令中执行。
@SpringBootApplication
@EnableZuulProxy
public class ZuulApplication {
public static void main(String[] args) {
SpringApplication.run(ZuulApplication.class, args);
}
}
3、编写配置文件
server:
port: 8040
spring:
application:
name: gateway-zuul
management:
security:
enabled: false
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
instance:
prefer-ip-address: true
默认情况下,Zuul会代理所有注册到Eureka Server的微服务,并且Zuul的路由规则如下:
http://zuul_host:port/微服务在eureka上的serviceId/* 会被转发到serviceId对应的微服务。
管理端点
当@EnabledZuulProxy 与Spring Boot Actuator配合使用时,Zuul会暴露两个端点:/routes和/filters。借助>这些断点,可方便、直观地查看以及管理Zuul。
访问:http://zuul_host:port/routes?format=details,展示注册到Eureka上的服务详情
{
"/provider-user/**":{
"id":"provider-user",
"fullPath":"/provider-user/**",
"location":"provider-user",
"path":"/**",
"prefix":"/provider-user",
"retryable":false,
"customSensitiveHeaders":false,
"prefixStripped":true
}
}
访问:http://zuul_host:port/filters,展示error、post、pre、route四种类型过滤器有哪些,以及执行顺序、是否启用等信息。
{
"error":[
{
"class":"org.springframework.cloud.netflix.zuul.filters.post.SendErrorFilter",
"order":0,
"disabled":false,
"static":true
}
],
"post":[
{
"class":"org.springframework.cloud.netflix.zuul.filters.post.SendResponseFilter",
"order":1000,
"disabled":false,
"static":true
}
],
"pre":[
{
"class":"org.springframework.cloud.netflix.zuul.filters.pre.DebugFilter",
"order":1,
"disabled":false,
"static":true
},
{
"class":"org.springframework.cloud.netflix.zuul.filters.pre.FormBodyWrapperFilter",
"order":-1,
"disabled":false,
"static":true
},
{
"class":"org.springframework.cloud.netflix.zuul.filters.pre.Servlet30WrapperFilter",
"order":-2,
"disabled":false,
"static":true
},
{
"class":"org.springframework.cloud.netflix.zuul.filters.pre.ServletDetectionFilter",
"order":-3,
"disabled":false,
"static":true
},
{
"class":"org.springframework.cloud.netflix.zuul.filters.pre.PreDecorationFilter",
"order":5,
"disabled":false,
"static":true
}
],
"route":[
{
"class":"org.springframework.cloud.netflix.zuul.filters.route.SimpleHostRoutingFilter",
"order":100,
"disabled":false,
"static":true
},
{
"class":"org.springframework.cloud.netflix.zuul.filters.route.RibbonRoutingFilter",
"order":10,
"disabled":false,
"static":true
},
{
"class":"org.springframework.cloud.netflix.zuul.filters.route.SendForwardFilter",
"order":500,
"disabled":false,
"static":true
}
]
}
路由配置
1.自定义指定微服务访问路径
zuul:
routes:
serviceId(替换真实serviceId): /访问路径/**
完成设置后,serviceId微服务就会被映射到 /访问路径/**路径
2.忽略指定微服务
zuul:
ignored-services: serviceId1,serviceId2
完成设置后,zuul忽略这两个微服务,只代理其他微服务
3.忽略所有微服务,只代理指定微服务
zuul:
ignored-services: '*'
routes:
serviceId(替换真实serviceId): /访问路径/**
完成设置后,zuul只代理serviceId微服务
4.同时指定微服务的serviceId和对应路径或者url和路径
zuul:
routes:
user-route: /**user-route只是给路由起个名字*/
service-id: serviceId /**service-id为注册Eureka微服务*/
path: /访问路径/*
配置效果同示例1
5.同时指定path和url
zuul:
routes:
user-route: /**user-route只是给路由起个名字*/
url: http://localhost:8080/
path: /访问路径/*
完成配置后将 /访问路径/** 映射到http://localhost:8080/** 上,但注意这种方式不会作为Hystrix命令执行,也不能使用Ribbon来负载均衡多个url,示例6可以解决
6.同时指定path和url,并且不破坏Zuul的Hystrix和Ribbon特性
zuul:
routes:
user-route: /**user-route只是给路由起个名字*/
path: /访问路径/*
service-id: serviceId
ribbon:
eureka:
enabled: false #为Ribbobn禁用Eureka#
serviceId:
ribbon:
listOfServices: localhost:8080,localhost:8081
7.使用正则表达式指定zuul的路由匹配规则
@Bean
public PatternServiceRouteMapper serviceRouteMapper() {
//第一个参数指定微服务正则
//第二个参数指定路由正则
return new PatternServiceRouteMapper("(?<name>^.+)-(?<version>v.+$)", "${version}/${name}");
}
8.路由前缀
zuul:
prefix: /api
strip-prefix: false
routes:
serviceId(替换真实serviceId): /访问路径/**
示例1:访问Zuul的/api/serviceId/1路径的请求会被转发到serviceId 的/api/1
zuul:
routes:
serviceId(替换真实serviceId):
path: /访问路径/**
strip-prefix: false
示例2: 访问zuul的/访问路径/1 会被转发到 serviceId的 /访问路径/1
9.忽略某些路径
zuul:
ignoredPatterns: /**/admin/**
routes:
serviceId(替换真实serviceId): /访问路径/**
可以将微服务映射到 /访问路径/**路径,但会忽略微服务中包含/admin/的路径。
10.本地转发
zuul:
routes:
route-name:
path: /path-a/**
url: forward:/path-b
完成配置访问Zuul的/path-a/,将会转发到Zuul的/path-b/。
可以将com.netflix包的日志级别设为DEBUG,这样zuul就会打印转发的所有细节,更好的排查问题和理解路由的规则
logging:
level:
com.netflix: DEBUG
网友评论