简介

上图的API路由网关服务由Zuul实现,主要就是对外提供服务接口的时候,起到了请求的路由和过滤作用,也因此能够隐藏内部服务的接口细节,从来有利于保护系统的安全性

配置过程

新建Module

该Module为Maven项目,命名为:microservice-zuul-3001

引入依赖

引入zuul路由网关依赖

1
2
3
4
5
<!-- zuul路由网关 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zuul</artifactId>
</dependency>

完整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
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<!--继承microservice依赖版本-->
<parent>
<groupId>org.example</groupId>
<artifactId>microservice</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>

<artifactId>microservice-zuul-3001</artifactId>

<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>

<dependencies>
<!-- zuul路由网关 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zuul</artifactId>
</dependency>
<!--引入microservice-common依赖,引入后可以使用microservice-common的实体类,工具类等-->
<dependency>
<groupId>org.example</groupId>
<artifactId>microservice-common</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</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>org.springframework</groupId>
<artifactId>springloaded</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
</dependencies>

</project>

application.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: 3001
context-path: /

spring:
application:
name: microservice-zuul

eureka:
instance:
instance-id: microservice-zuul:3001 #客户端实例名称
prefer-ip-address: true #显示IP
client:
service-url:
defaultZone: http://eureka2001.ledao.com:2001/eureka/,http://eureka2002.ledao.com:2002/eureka/,http://eureka2003.ledao.com:2003/eureka/ # 集群

info:
groupId: @project.groupId@
artifactId: @project.artifactId@
version: @project.version@
负责人: 王五
联系电话: 110

修改hosts

找到 C:\Windows\System32\drivers\etc 打开hosts,底部加配置:

1
127.0.0.1  zuul.ledao.com

添加启动类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
package com.ledao;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;

/**
* @author LeDao
* @company
* @create 2021-08-17 22:59
*/
@EnableZuulProxy
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class, HibernateJpaAutoConfiguration.class})
public class ZuulApplication_3001 {

public static void main(String[] args) {
SpringApplication.run(ZuulApplication_3001.class, args);
}
}

测试

启动

启动以下项目:

microservice-eureka-server-2001

microservice-eureka-server-2002

microservice-eureka-server-2003

microservice-student-provider-hystrix-1004

microservice-zuul-3001

microservice-student-consumer-feign-80

eureka注册中心有两个服务:

访问

①直接访问

直接通过 http://localhost/student/list 访问

②通过zuul路由

通过 http://zuul.ledao.com:3001/microservice-student/student/list 访问