SpringCloud之Zuul路由网关
Zuul包含了对请求的路由和过滤两个最主要的功能:
其中路由功能负责将外部请求转发到具体的微服务实例上,是实现外部访问统一入口的基础而过滤器功能则负责对请求的处理过程进行干预,是实现请求校验、服务聚合等功能的基础.
Zuul和Eureka进行整合,将Zuul自身注册为Eureka服务治理下的应用,同时从Eureka中获得其他微服务的消息,也即以后的访问微服务都是通过Zuul跳转后获得。
注意:Zuul服务最终还是会注册进Eureka
提供=代理+路由+过滤三大功能
官网资料
https://github.com/Netflix/zuul/wiki/Getting-Started
案例实战
新建Module模块microservicecloud-zuul-gateway-9527,添加zuul相关依赖
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-zuul</artifactId> </dependency>
完整POM
<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"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>com.atguigu.springcloud</groupId> <artifactId>microservicecloud</artifactId> <version>0.0.1-SNAPSHOT</version> </parent> <artifactId>microservicecloud-zuul-gateway-9527</artifactId> <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.atguigu.springcloud</groupId> <artifactId>microservicecloud-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: microservicecloud-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: atguigu-microcloud company.name: www.atguigu.com build.artifactId: $project.artifactId$ build.version: $project.version$
hosts文件修改:
修改: C:\Windows\System32\drivers\etc 下的hosts文件
添加 127.0.0.1 myzuul.com
主启动类添加@EnableZuulProxy注解
package com.atguigu.springcloud; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.zuul.EnableZuulProxy; @SpringBootApplication @EnableZuulProxy public class Zuul_9527_StartSpringCloudApp { public static void main(String[] args) { SpringApplication.run(Zuul_9527_StartSpringCloudApp.class, args); } }
测试
启动三个eureka集群
启动一个服务提供类microservicecloud-provider-dept-8001
启动microservicecloud-zuul-gateway-9527
不用路由: 访问 http://localhost:8001/dept/get/2
启用路由 :http://myzuul.com:9527/microservicecloud-dept/dept/get/2
路由访问映射规则
之前我们可以通过 http://myzuul.com:9527/microservicecloud-dept/dept/get/2 路径去访问服务,但是这种路径却暴露了我们的服务名称 microservicecloud-dept。那么有没有方法来隐私服务名,并定义我们想要让别人访问的路径。
修改 microservicecloud-zuul-gateway-9527 工程的yml文件,添加如下
zuul: ignored-services: microservicecloud-dept #忽略(即隐藏)服务名 routes: mydept.serviceId: microservicecloud-dept mydept.path: /mydept/** #将之前的microservicecloud-dept路径修改为 mydept
完整的yml
server: port: 9527 spring: application: name: microservicecloud-zuul-gateway eureka: client: service-url: defaultZone: http://eureka7001.com:7001/eureka instance: instance-id: gateway-9527.com prefer-ip-address: true zuul: ignored-services: microservicecloud-dept #忽略(即隐藏)服务名 routes: mydept.serviceId: microservicecloud-dept mydept.path: /mydept/** #将之前的microservicecloud-dept路径修改为 mydept info: app.name: atguigu-microcloud company.name: www.atguigu.com build.artifactId: $project.artifactId$ build.version: $project.version$
启动三个eureka集群
启动一个服务提供类microservicecloud-provider-dept-8001
启动microservicecloud-zuul-gateway-9527
再测试:
http://myzuul.com:9527/mydept/dept/get/2
以及:http://myzuul.com:9527/microservicecloud-dept/dept/get/2
可以发现已经实现了我们想要的效果。
但之前的配置仍是存在问题的,如果新增一个服务,就要去配置一条忽略原真实服务名。所以我们就直接配置忽略所有,并设置统一公共前缀:
完整的yml配置如下:
server: port: 9527 spring: application: name: microservicecloud-zuul-gateway eureka: client: service-url: defaultZone: http://eureka7001.com:7001/eureka instance: instance-id: gateway-9527.com prefer-ip-address: true zuul: ignored-services: "*" #忽略(即隐藏)服务名 prefix: /atguigu routes: mydept.serviceId: microservicecloud-dept mydept.path: /mydept/** #将之前的microservicecloud-dept路径修改为 mydept info: app.name: atguigu-microcloud company.name: www.atguigu.com build.artifactId: $project.artifactId$ build.version: $project.version$
再次分别启动:
启动三个eureka集群
启动一个服务提供类microservicecloud-provider-dept-8001
启动microservicecloud-zuul-gateway-9527
测试:
http://myzuul.com:9527/atguigu/mydept/dept/get/1