是什么:
Zuul包含了对请求的路由和过滤两个最主要的功能:
其中路由功能负责将外部请求转发到具体的微服务实例上,是实现外部访问统一入口的基础而过滤器功能则负责对请求的处理过程进行干预,是实现请求校验、服务聚合等功能的基础.
Zuul和Eureka进行整合,将Zuul自身注册为Eureka服务治理下的应用,同时从Eureka中获得其他微服务的消息,也即以后的访问微服务都是通过Zuul跳转后获得。
注意:Zuul服务最终还是会注册进Eureka
提供=代理+路由+过滤三大功能
路由基本配置:
- 新建microservicecloud-zuul-gateway-9527模块,pom引入依赖:
<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>
2.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 #需要更改hosts映射文件
prefer-ip-address: true
zuul:
#ignored-services: microservicecloud-dept
prefix: /atguigu
ignored-services: "*"
routes:
mydept.serviceId: microservicecloud-dept
mydept.path: /mydept/**
info:
app.name: atguigu-microcloud
company.name: www.atguigu.com
build.artifactId: $project.artifactId$
build.version: $project.version$
3.hosts修改:
127.0.0.1 myzuul.com
4.主启动类加上@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);
}
}
5.启动后测试:
红色是网关,蓝色是微服务名称,黑色是访问查询地址。
路由访问映射规则:
代理名称:
- 修改yml文件:
修改前:
http://myzuul.com:9527/microservicecloud-dept/dept/get/2
修改后:
http://myzuul.com:9527/mydept/dept/get/1
2.此时问题:
路由访问http://myzuul.com:9527/mydept/dept/get/1,是可以的。原路径http://myzuul.com:9527/microservicecloud-dept/dept/get/2访问也是可以的。
3.原真实服务名称忽略:
单个服务名称时:
zuul:
ignored-services: microservicecloud-dept
routes:
mydept.serviceId: microservicecloud-dept
mydept.path: /mydept/**
多个时:
zuul:
ignored-services: "*"
routes:
mydept.serviceId: microservicecloud-dept
mydept.path: /mydept/**
4.设置统一公共前缀:
zuul:
prefix: /DLX
ignored-services: "*"
routes:
mydept.serviceId: microservicecloud-dept
mydept.path: /mydept/**
结果:http://myzuul.com:9527/DLX/mydept/dept/get/1