Zuul路由网关简介与实战
1. 概述
Zuul包含了对请求的路由和过滤两个最主要的功能,其中路由功能负责将外部请求转发到具体的微服务实例上,是实现外部访问统一入口的基础而过滤器功能则负责对请求的处理过程进行干预,是实现请求校验、服务聚合等功能的基础.Zuul和Eureka进行整合,将Zuul自身注册为Eureka服务治理下的应用,同时从Eureka中获得其他微服务的消息,也即以后的访问微服务都是通过Zuul跳转后获得。
注意:Zuul服务最终还是会注册进Eureka
Zuul功能
官网 : https://github.com/Netflix/zuul/wiki/Getting-Started
2. 路由基本配置
2.1 新建Module模块
microservicecloud-zuul-gateway-9527
2.2 pom.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
| <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>com.hellodev</groupId> <artifactId>atguigu-microservicecloud</artifactId> <version>0.0.1</version> </parent> <artifactId>microservicecloud-zuul-gateway-9527</artifactId> <version>0.0.1</version> <dependencies> <dependency> <groupId>com.hellodev</groupId> <artifactId>microservicecloud-api</artifactId> <version>${project.version}</version> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-zuul</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-hystrix</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> </dependency> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-core</artifactId> </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> </dependencies> </project>
|
2.3 yml文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| 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-microservicecloud company.name: www.atguigu.com build.artifactId: ${project.artifactId} build.version: ${project.version}
|
2.4 修改hosts
2.5 主启动类
1 2 3 4 5 6 7
| @SpringBootApplication @EnableZuulProxy public class ZuulGateway9527Application { public static void main(String[] args) { SpringApplication.run(ZuulGateway9527Application.class, args); } }
|
2.6 验证
启动Eureka集群,然后启动provider-dept-8001,最后启动zuul-gateway-9527,访问接口
1 2 3 4 5 6 7 8 9 10 11 12
| 不使用路由 : curl http://localhost:8001/dept/get/2 { "deptno": 2, "dname": "人事部", "db_source": "cloudDB01" } 使用路由 : curl http://myzuul.com:9527/microservicecloud-dept/dept/get/2 { "deptno": 2, "dname": "人事部", "db_source": "cloudDB01" }
|
3. 路由访问映射规则
3.1 自定义微服务别名
修改yml
1 2 3 4 5
| zuul: routes: mydept.serviceId: microservicecloud-dept mydept.path: /mydept/**
|
此时即可通过别名的方式访问微服务
1
| curl http://myzuul.com:9527/mydept/dept/get/1
|
3.2 隐藏真实微服务名称
上面修改方式,还是可以通过真实微服务名称访问接口,这依旧是不安全的,因此需要隐藏.
修改yml
1 2 3 4 5 6 7 8
| zuul: ignored-services: "*" routes: mydept.serviceId: microservicecloud-dept mydept.path: /mydept/**
|
再次访问
1 2
| curl http://myzuul.com:9527/microservicecloud-dept/dept/get/2 {"timestamp":1566983040888,"status":404,"error":"Not Found","message":"Not Found","path":"/microservicecloud-dept/dept/get/2"}
|
3.3 设置统一公共前缀
修改yml文件
1 2 3 4 5 6 7 8 9 10
| zuul: prefix: /atguigu ignored-services: "*" routes: mydept.serviceId: microservicecloud-dept mydept.path: /mydept/**
|
之后所有的请求都需要要增加统一前缀
资源获取
公众号回复 : Zuul路由网关 获取本节代码
公众号回复 : SpringCloud思维导图
Tips
欢迎收藏和转发,感谢你的支持!(๑•̀ㅂ•́)و✧
欢迎关注我:后端小哥,专注后端开发,希望和你一起进步!