一、概述
1、什么是zuul(官方资料)
Zuul包含了对请求的路由和过滤两个最主要的功能
(1)路由功能负责将外部请求转发到具体的微服务实例上,是实现外部访问统一入口的基础。
(2)过滤功能则负责对请求的处理过程进行干预,是实现请求校验、服务聚合等功能的基础。
Zuul和Eureka进行整合,将Zuul自身注册为Eureka服务治理下的应用,同时从Eureka中获得其他微服务的消息,也即以后的访问微服务都是通过Zuul跳转后获得。
二、路由基本配置
1、新建Module模块microservicecloud-zuul-gateway-9527
pom.xml
4.0.0 com.atguigu.springcloud microservicecloud 0.0.1-SNAPSHOT microservicecloud-zuul-gateway-9527 org.springframework.cloud spring-cloud-starter-zuul org.springframework.cloud spring-cloud-starter-eureka org.springframework.boot spring-boot-starter-actuator org.springframework.cloud spring-cloud-starter-hystrix org.springframework.cloud spring-cloud-starter-config com.atguigu.springcloud microservicecloud-api ${project.version} org.springframework.boot spring-boot-starter-jetty org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-test org.springframework springloaded org.springframework.boot spring-boot-devtools
application.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 修改:添加 127.0.0.1 myzuul.com
新建启动类 Zuul_9527_StartSpringCloudApp.java
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); } }
2、启动 eureka、microservicecloud-provider-dept-8001、microservicecloud-zuul-gateway-9527 测试
输入 http://localhost:7001/ 路由网关微服务已经注册到 eureka
输入 http://localhost:8001/dept/get/2 不用路由,测试成功
输入http://myzuul.com:9527/microservicecloud-dept/dept/get/2 使用路由,测试成功
三、路由访问映射规则
1、代理微服务名称
修改microservicecloud-zuul-gateway-9527的application.yml,添加
zuul: routes: mydept.serviceId: microservicecloud-dept mydept.path: /mydept/**
启动 eureka、microservicecloud-provider-dept-8001、microservicecloud-zuul-gateway-9527 测试
输入:http://myzuul.com:9527/microservicecloud-dept/dept/get/2 测试成功
输入:http://myzuul.com:9527/mydept/dept/get/1 测试成功
2、忽略真实的微服务名称
application.yml 做如下修改,使只能访问代理后的路径
zuul: ignored-services: microservicecloud-dept routes: mydept.serviceId: microservicecloud-dept mydept.path: /mydept/**
输入:http://myzuul.com:9527/microservicecloud-dept/dept/get/2 测试失败
输入:http://myzuul.com:9527/mydept/dept/get/1 测试成功
3、忽略所有的真实的微服务名称
忽略单个的微服务名称可以用具体名称,如上,忽略所有微服务名称可以使用 “*”,修改 application.yml 如下:
zuul: ignored-services: "*" routes: mydept.serviceId: microservicecloud-dept mydept.path: /mydept/**
4、设置统一公共前缀
如果想在访问微服务之前添加统一的前缀,可以修改 application.yml 如下:
zuul: prefix: /atguigu ignored-services: "*" routes: mydept.serviceId: microservicecloud-dept mydept.path: /mydept/**
输入:http://myzuul.com:9527/atguigu/mydept/dept/get/1 测试成功