Zuul 简介
Zuul 微服务网关是为 Spring Cloud Netflix 提供动态路由, 监控, 弹性, 安全等服务的框架. 可以和 Eureka,Ribbon,Hystrix 等组件配合使用.
Zuul 主要功能
1, 身份认证与安全: 识别每个资源的验证要求, 并拒绝那些与要求不符的请求.
2, 审查与监控: 在边缘位置追踪有意义的数据和统计结果, 从而为我们带来精确的生产视图.
3, 动态路由: 动态地将请求路由到不同的后端集群;
4, 压力测试: 逐渐增加指向集群的流量, 以了解性能;
5, 为每一种负载类型分配对应容量, 并弃用超出限定值的请求;
Zuul 带了什么优势
没有使用网关服务的时候:
1, 客户端会多次请求不同的微服务, 增加了客户端的复杂性.
2, 存在跨域请求, 在一定场景下处理相对复杂.
3, 认证复杂, 每个服务都需要独立认证.
使用服务网关后:
1, 易于监控, 可在微服务网关收集监控数据并将其推送到外部系统进行分析.
2, 易于认证, 可在微服务网关上进行认证, 然后再将请求转发到后端的微服务, 而无须在每个微服务中进行认证.
3, 减少了客户端与各个微服务之间的交互次数.
Zuul 入门
- <!--1. 配置 pom 文件, 引入 spring-cloud-starter-netflix-zuul 包 -->
- <dependency>
- <groupId>org.springframework.cloud</groupId>
- <artifactId>spring-cloud-starter-netflix-zuul</artifactId>
- </dependency>
- <dependency>
- <groupId>org.springframework.cloud</groupId>
- <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
- </dependency>
- <!-- 2. 配置属性文件 -->
- server.port=9004
- # 注册到 eureka 服务端的微服务名称
- spring.application.name=ms-gateway-zuul
- # 注册到 eureka 服务端的地址
- eureka.client.service-url.defaultZone=http://localhost:9000/eureka/
- # 点击具体的微服务, 右下角是否显示 ip
- eureka.instance.prefer-ip-address=true
- # 显示微服务的名称
- eureka.instance.instance-id=ms-gateway-zuul-9004
- <!-- 3. 在 Spring 的启动入口添加 @EnableZuulProxy 注解 -->
- @SpringBootApplication
- @EnableZuulProxy
- public class ZuulApplication {
- public static void main(String[] args) {
- SpringApplication.run(ZuulApplication.class, args);
- }
- }
通过 Url 访问: http://localhost:9004/zuul/ms-consumer-user/userController/getUserInfo/{loginName}
Zuul 的常规配置
1, 设置访问路径, 默认值为 zuul
zuul.servlet-path=/zuul
2, 关闭通过微服务名称路访问 (避免暴露服务名称)
全部设置: zuul.ignored-services=*
分服务设置: zuul.ignored-services=ms-provider-order,ms-consumer-user
3, 通过服务实例指定映射路径
zuul.routes.ms-provider-order=/order-service/**
访问: http://localhost:9004/zuul/order-service/userController/getUserInfo/{loginName}
4, 通过服务的 serviceId 指定映射路径
zuul.routes.use-routing.serviceId=ms-provider-order
zuul.routes.use-routing.path=/order-service/**
5, 通过 url 指定映射路径 (路由不会作为 HystrixCommand 执行, 同时也不能使用 Ribbon 来负载均衡多个 URL)
zuul.routes.use-routing.url=http://localhost:8004/
zuul.routes.use-routing.path=/order-service/**
6, 统一设置路由前缀
全局设置: zuul.prefix=/order-API
zuul.strip-prefix=true(是否剥离前缀, 默认是 true)
zuul.routes.use-routing.serviceId=ms-provider-order
zuul.routes.use-routing.path=/order-service/**
访问: http://localhost:9004/order-API/order-service/userController/getUserInfo/{loginName}
分服务设置: zuul.strip-prefix=true
zuul.routes.use-routing.serviceId=ms-provider-order
zuul.routes.use-routing.path=/order-service/**
zuul.routes.use-routing.stripPrefix=true
访问: http://localhost:9004/zuul/order-service/userController/getUserInfo/{loginName}
7, 过滤敏感路径
zuul.ignored-patterns=/**/admin/**
8, 过滤敏感头信息 (通过 zuul 网关的时候, 会过滤掉敏感的头信息, 比如 cookie 等其他的)
全局设置: zuul.sensitive-headers=
分模块设置: zuul.routes.use-routing.serviceId=ms-provider-order
- zuul.routes.use-routing.path=/order-service/**
- zuul.routes.use-routing.sensitiveHeaders=
来源: https://www.cnblogs.com/jiangyaxiong1990/p/12404292.html