文章目录
- 概述
- 什么是Zuul?
- Zuul 能干嘛?
- Zuul入门案例
- pom依赖
- application.yml
- 启动类
- 隐藏真实路径
概述
什么是Zuul?
Zuul包含了对请求的路由(用来跳转的)和过滤两个最主要功能:
- 其中路由功能负责将外部请求转发到具体的微服务实例上,是实现外部访问统一入口的基础。
- 而过滤器功能则负责对请求的处理过程进行干预,是实现请求校验,服务聚合等功能的基础。
Zuul和Eureka进行整合,将Zuul自身注册为Eureka服务治理下的应用,同时从Eureka中获得其他服务的消息,也即以后的访问微服务都是通过Zuul跳转后获得。
注意:Zuul 服务最终还是会注册进 Eureka
提供:代理 + 路由 + 过滤 三大功能!
Zuul 能干嘛?
- 路由
- 过滤
官方文档:https://github.com/Netflix/zuul/
Zuul入门案例
新建springcloud-zuul模块,并导入依赖
pom依赖
<dependencies><!--导入zuul依赖--><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-zuul</artifactId><version>1.4.6.RELEASE</version></dependency><!--Hystrix依赖--><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-hystrix</artifactId><version>1.4.6.RELEASE</version></dependency><!--dashboard依赖--><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-hystrix-dashboar</artifactId><version>1.4.6.RELEASE</version></dependency><!--Ribbon--><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-ribbon</artifactId><version>1.4.6.RELEASE</version></dependency><!--Eureka--><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-eureka</artifactId><version>1.4.6.RELEASE</version></dependency><!--实体类+web--><dependency><groupId>com.haust</groupId><artifactId>springcloud-api</artifactId><version>1.0-SNAPSHOT</version></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><!--热部署--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-devtools</artifactId></dependency>
</dependencies>
application.yml
server:port: 9527
spring:application:name: springcloud-zuul #微服务名称
# eureka 注册中心配置
eureka:client:service-url:defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/instance: #实例的idinstance-id: zuul9527.comprefer-ip-address: true # 显示ip
info:app.name: qf.springcloud # 项目名称company.name: 深圳大学 # 公司名称
启动类
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;/*** @Auther: qf* @Date: 2025/05/20/20:53* @Description: Zull路由网关主启动类*/
@SpringBootApplication
@EnableZuulProxy // 开启Zuul
public class ZuulApplication_9527 {public static void main(String[] args) {SpringApplication.run(ZuulApplication_9527.class,args);}
}
启动项目,输入http://127.0.0.1:9527/test-provider-dept/dept/get/1
访问可能需要等待
隐藏真实路径
相关配置
# zull 路由网关配置
zuul:# 路由相关配置routes:mydept.serviceId: test-provider-dept # eureka注册中心的服务提供方路由名称mydept.path: /mydept/** # 将eureka注册中心的服务提供方路由名称 改为自定义路由名称
# ignored-services: springcloud-provider-dept # 忽略指定服务名称# 不能再使用这个路径访问了,*: 忽略,隐藏全部的服务名称~ignored-services: "*"# 设置公共的前缀prefix: /qfcloud
访问服务接口
http://127.0.0.1:9527/qfcloud/mydept/dept/get/1
相关文章:
Spring Cloud Netflix学习笔记01
Spring Cloud Netflix学习笔记02-Eureka
Spring Cloud Netflix学习笔记03-Ribbon
Spring Cloud Netflix学习笔记04-Feign
Spring Cloud Netflix学习笔记05-Hystrix
Spring Cloud Netflix学习笔记06-Zuul
Spring Cloud Netflix学习笔记07-Spring Cloud Config