九:zuul路由网关
1.概述
1.1 是什么
Zuul包含了对请求的路由和过滤两个最主要的功能:
其中路由功能负责将外部请求转发到具体的微服务实例上,是实现外部访问统一入口的基础.
而过滤器功能则负责对请求的处理过程进行干预,是实现请求校验、服务聚合等功能的基础.
Zuul和Eureka进行整合,将Zuul自身注册为Eureka服务治理下的应用,同时从Eureka中获得其他微服务的消息,也即以后的访问微服务都是通过Zuul跳转后获得。
注意:Zuul服务最终还是会注册进Eureka
提供=代理+路由+过滤三大功能
1.2 能干嘛
路由/过滤
1.3 官网资料
https://github.com/Netflix/zuul/wiki/Getting-Started
2.路由基本配置
2.1 新建Module模块microservicecloud-zuul-gateway-9527
2.2 POM
2.3 YML
2.4 hosts修改(域名映射)
2.5 主启动类
2.6 启动相关微服务(3个eureka集群/一个服务提供类/一个路由)
2.7 测试
2.2 POM
<!-- 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>
2.3 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$
2.4 hosts修改(域名映射)
127.0.0.1 myzuul.com
2.5 主启动类
@SpringBootApplication
@EnableZuulProxy // 开启zuul代理
public class Zuul_9527_StartSpringCloudApp
{
public static void main(String[] args)
{
SpringApplication.run(Zuul_9527_StartSpringCloudApp.class, args);
}
}
2.7 测试
不用路由: http://localhost:8001/dept/get/2
启用路由: http://myzuul.com:9527/microservicecloud-dept/dept/get/2
3.路由访问映射规则
3.1 代理名称
# before http://myzuul.com:9527/microservicecloud-dept/dept/get/2
zuul:
routes: # 配置路由访问映射规则
mydept.serviceId: microservicecloud-dept
mydept.path: /mydept/**
# after http://myzuul.com:9527/mydept/dept/get/1
此时问题:
路由访问OK http://myzuul.com:9527/mydept/dept/get/1
原路径访问OK http://myzuul.com:9527/microservicecloud-dept/dept/get/2
3.2 原真实服务名忽略
# 隐藏单个具体微服务
zuul:
ignored-services: microservicecloud-dept # 配置单个微服务名称失效
routes:
mydept.serviceId: microservicecloud-dept
mydept.path: /mydept/**
# 隐藏多个微服务可以用"*"
zuul:
ignored-services: "*" # 配置多个微服务名称失效
routes:
mydept.serviceId: microservicecloud-dept
mydept.path: /mydept/**
3.3 设置统一公共前缀
http://myzuul.com:9527/atguigu/mydept/dept/get/1
zuul:
prefix: /atguigu # 统一公共前缀
ignored-services: "*"
routes:
mydept.serviceId: microservicecloud-dept
mydept.path: /mydept/**
3.4 最后YML
server:
port: 9527
spring:
application:
name: microservicecloud-zuul-gateway
eureka:
client:
service-url:
defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7004/eureka,http://eureka7003.com:7003/eureka
instance:
instance-id: gateway-9527.com
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$