概述
是什么
Zuul包含了对请求的路由和过滤两个最主要的功能:
其中路由功能负责将外部请求转发到具体的微服务实例上,是实现外部访问统一入口的基础而过滤器功能则负责对请求的处理过程进行干预,是实现请求校验、服务聚合等功能的基础 Zuul 和 Eureka 进行整合,将Zuul自身注册为Eureka服务治理下的应用,同时从Eureka中获得其他微服务的消息,也即以后的访问微服务都是通过Zuul跳转后获得。
注意:Zuul服务最终还是会注册进Eureka
总结:zuul = 代理 + 路由 + 过滤三大功能
能干嘛
- 路由
- 过滤
官网资料
路由基本配置
-
新建 Module 模块 icodecloud-zuul-gateway-9527
-
POM
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <parent> <artifactId>icodecloud</artifactId> <groupId>com.icode.case.cloud</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>icodecloud-zuul-gateway-9527</artifactId> <description>服务网关</description> <dependencies> <!-- zuul路由网关 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-zuul</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency> <!-- actuator监控 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <!-- hystrix容错--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-hystrix</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency> <!-- 日常标配 --> <dependency> <groupId>com.icode.case.cloud</groupId> <artifactId>icodecloud-api</artifactId> <version>${project.version}</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jetty</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> </dependency> <!-- 热部署插件 --> <dependency> <groupId>org.springframework</groupId> <artifactId>springloaded</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> </dependency> </dependencies> </project>
-
YML
server: port: 9527 spring: application: name: icodecloud-zuul-gateway eureka: client: service-url: defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka,http://eureka7003.com:7003/eureka instance: instance-id: gateway-9527.com prefer-ip-address: true info: app.name: icode-icodecloud company.name: www.icode.com build.artifactId: ${project.artifactId} build.version: ${project.version} build.description: ${project.description}
-
hosts修改
新增:127.0.0.1 myzuul.com
-
主启动类
@SpringBootApplication @EnableZuulProxy public class Zuul_9527_StartSpringCloudApp { public static void main(String[] args) { SpringApplication.run(Zuul_9527_StartSpringCloudApp.class, args); } }
-
启动
三个eureka集群
一个服务提供类icodecloud-provider-dept-8001
一个路由
-
测试
路由访问映射规则
-
工程 icodecloud-zuul-gateway-9527
-
代理名称
zuul: routes: mydept.serviceId: icodecloud-dept mydept.path: /mydept/**
-
此时问题
原路径与代理路径都可以访问
-
忽略真实服务名称
zuul: ignored-services: icodecloud-dept #多个可用 "*" 代替 routes: mydept.serviceId: icodecloud-dept mydept.path: /mydept/**
-
设置统一公共前缀
zuul: prefix: /icode ignored-services: icodecloud-dept #多个可用 "*" 代替 routes: mydept.serviceId: icodecloud-dept mydept.path: /mydept/**