1.前言
根据教材、博客文章的实例实操,基本都是单层拦截,没有找到多层拦截的具体写法 ,让我走了很多弯路,我将其写在这里,以待以后参考。
2.环境
spring boot : 2.1.6.RELEASE
spring cloud : Greenwich.SR2
3.准备一个端口为5001 的 Zuul网关
目录结构
导入依赖
org.springframework.cloud
spring-cloud-starter-netflix-zuul
pom文件
4.0.0
cen.cloud
cen-mycloud
0.0.1-SNAPSHOT
com.example
zuul-server-5001
0.0.1-SNAPSHOT
zuul-server-5001
Demo project for Spring Boot
1.8
org.springframework.boot
spring-boot-starter
org.springframework.boot
spring-boot-starter-test
test
org.junit.vintage
junit-vintage-engine
org.springframework.cloud
spring-cloud-starter-netflix-eureka-client
org.springframework.cloud
spring-cloud-starter-netflix-zuul
org.springframework.boot
spring-boot-maven-plugin
View Code
application.properties 文件 ,设置自定义映射,路径含有/bd/则路由到 百度首页 ,用于测试
spring.application.name=zuul-server-5001server.port=5001#
eureka.client.service-url.defaultZone=http://localhost:7001/eureka/
#全局添加前缀,如 localhost:114/myzuul/test/bb ,用于识别是否需要转发路由操作
#不可使用/zuul ,猜测这是保留字
zuul.prefix=/mzuul
#//默认是false,这里是全局配置
#zuul.strip-prefix: //是否将这个代理前缀去掉
#
#忽略所有的,表示禁用默认路由,只认我们自己配置的路由.
#zuul.ignored-services="*"#
#自定义路由设置
#拦截路径
zuul.routes.bd.path=/bd/**#拦截后访问的指定地址
zuul.routes.bd.url=h