1、Zuul路由网关是什么?
答:Zuul包含了对请求的路由和过滤两个最主要的功能,其中路由功能负责将外部请求转发到具体的微服务实例上,是实现外部访问统一入口的基础而过滤器功能则负责对请求的处理过程进行干预,是实现请求校验、服务聚合等功能的基础。Zuul和Eureka进行整合,将Zuul自身注册为Eureka服务治理下的应用,同时从Eureka中获得其他微服务的消息,也即以后的访问微服务都是通过Zuul跳转后获得。
注意:Zuul服务最终还是会注册进Eureka。Zuul提供了代理、路由、过滤三大功能。
2、新建模块Module,microservicecloud-zuul-gateway-9527,修改pom.xml配置文件,如下所示:
1 <project xmlns="http://maven.apache.org/POM/4.0.0"
2 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
4 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5 <modelVersion>4.0.0</modelVersion>
6 <parent>
7 <groupId>com.bie.springcloud</groupId>
8 <artifactId>microservicecloud</artifactId>
9 <version>0.0.1-SNAPSHOT</version>
10 </parent>
11 <artifactId>microservicecloud-zuul-gateway-9527</artifactId>
12
13 <dependencies>
14 <!-- zuul路由网关 -->
15 <dependency>
16 <groupId>org.springframework.cloud</groupId>
17 <artifactId>spring-cloud-starter-zuul</artifactId>
18 </dependency>
19 <dependency>
20 <groupId>org.springframework.cloud</groupId>
21 <artifactId>spring-cloud-starter-eureka</artifactId>
22 </dependency>
23 <!-- actuator监控 -->
24 <dependency>
25 <groupId>org.springframework.boot</groupId>
26 <artifactId>spring-boot-starter-actuator</artifactId>
27 </dependency>
28 <!-- hystrix容错 -->
29 <dependency>
30 <groupId>org.springframework.cloud</groupId>
31 <artifactId>spring-cloud-starter-hystrix</artifactId>
32 </dependency>
33 <dependency>
34 <groupId>org.springframework.cloud</groupId>
35 <artifactId>spring-cloud-starter-config</artifactId>
36 </dependency>
37 <!-- 日常标配 -->
38 <dependency>
39 <groupId>com.bie.springcloud</groupId>
40 <artifactId>microservicecloud-api</artifactId>
41 <version>${project.version}</version>
42 </dependency>
43 <dependency>
44 <groupId>org.springframework.boot</groupId>
45 <artifactId>spring-boot-starter-jetty</artifactId>
46 </dependency>
47 <dependency>
48 <groupId>org.springframework.boot</groupId>
49 <artifactId>spring-boot-starter-web</artifactId>
50 </dependency>
51 <dependency>
52 <groupId>org.springframework.boot</groupId>
53 <artifactId>spring-boot-starter-test</artifactId>
54 </dependency>
55 <!-- 热部署插件 -->
56 <dependency>
57 <groupId>org.springframework</groupId>
58 <artifactId>springloaded</artifactId>
59 </dependency>
60 <dependency>
61 <groupId>org.springframework.boot</groupId>
62 <artifactId>spring-boot-devtools</artifactId>
63 </dependency>
64 </dependencies>
65
66 </project>
修改模块microservicecloud-zuul-gateway-9527的application.yml配置文件,如下所示:
1 server:
2 port: 9527 # 端口号
3
4 spring:
5 application:
6 name: microservicecloud-zuul-gateway # 微服务的名称
7
8 eureka:
9 client: # 客户端注册进eureka服务列表内
10 service-url:
11 # defaultZone: http://localhost:7001/eureka
12 defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka,http://eureka7003.com:7003/eureka
13 instance:
14 instance-id: gateway-9527.com # 将eureka-server注册中心的服务,显示你想看的名称
15 prefer-ip-address: true # 访问路径可以显示IP地址
16
17
18 info: # 微服务info内容显示详细信息
19 app.name: microservicecloud-zuul-gateway-9527 # 应用名称
20 company.name: www.baidu.com # 公司地址
21 build.artifactId: $project.artifactId$ # 构建项目artifactId
22 build.version: $project.version$ # 构建项目版本号
修改模块microservicecloud-zuul-gateway-9527的主启动类,如下所示:
1 package com.bie;
2
3 import org.springframework.boot.SpringApplication;
4 import org.springframework.boot.autoconfigure.SpringBootApplication;
5 import org.springframework.cloud.netflix.zuul.EnableZuulProxy;
6
7 /**
8 *
9 *
10 * @author biehl
11 *
12 */
13 @SpringBootApplication
14 @EnableZuulProxy // zuul服务网关相关的
15 public class MicroServiceCloudZuul9527Application {
16
17 public static void main(String[] args) {
18 SpringApplication.run(MicroServiceCloudZuul9527Application.class, args);
19 }
20
21 }
启动三个节点的Eureka Server注册中心,启动microservicecloud-provider-dept-8001服务提供者模块,启动你的microservicecloud-zuul-gateway-9527路由模块。开始进行测试如下所示:
启动路由访问如下所示:http://127.0.0.1:9527/microservicecloud-provider-dept/dept/get/1
2、Zuul路由访问映射规则。
1 server:
2 port: 9527 # 端口号
3
4 spring:
5 application:
6 name: microservicecloud-zuul-gateway # 微服务的名称
7
8 eureka:
9 client: # 客户端注册进eureka服务列表内
10 service-url:
11 # defaultZone: http://localhost:7001/eureka
12 defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka,http://eureka7003.com:7003/eureka
13 instance:
14 instance-id: gateway-9527.com # 将eureka-server注册中心的服务,显示你想看的名称
15 prefer-ip-address: true # 访问路径可以显示IP地址
16
17
18 # Zuul路由访问映射规则。
19 zuul:
20 # prefix: /zuul # 设置统一公共前缀
21 # ignored-services: microservicecloud-provider-dept # 忽略原真实服务名称。单个可以使用具体的名称,多个可以使用"*"进行忽略
22 ignored-services: "*"
23 routes:
24 mydept.serviceId: microservicecloud-provider-dept # 真实微服务应用名称
25 mydept.path: /mydept/** # 使用mydept替代microservicecloud-provider-dept进行路由访问
26 prefix: /zuul
27
28
29 info: # 微服务info内容显示详细信息
30 app.name: microservicecloud-zuul-gateway-9527 # 应用名称
31 company.name: www.baidu.com # 公司地址
32 build.artifactId: $project.artifactId$ # 构建项目artifactId
33 build.version: $project.version$ # 构建项目版本号
效果如下所示: