ASP.NET Core微服务之基于Steeltoe集成Zuul实现统一API网关

释放双眼,带上耳机,听听看~!

Tip: 此篇已加入.NET Core微服务基础系列文章索引,本篇接上一篇《基于Steeltoe使用Eureka实现服务注册与发现》,所演示的示例也是基于上一篇的基础上而扩展的。

一、关于Spring Cloud Zuul

API Gateway(API GW / API 网关),顾名思义,是出现在系统边界上的一个面向API的、串行集中式的强管控服务,这里的边界是企业IT系统的边界。

Zuul 是Netflix 提供的一个开源组件,致力于在云平台上提供动态路由,监控,弹性,安全等边缘服务的框架,也有很多公司使用它来作为网关的重要组成部分。Spring Cloud 体系收录的该模块,主要用于提供动态路由、监控、安全控制、限流配额等,可以将内部微服务API同意暴露。

ASP.NET Core微服务之基于Steeltoe集成Zuul实现统一API网关

有关Zuul的更多内容,请参考我的这一篇:《Spring Cloud微服务架构学习笔记与示例》,这里不是本文重点,不再赘述。

二、快速构建Zuul Server

(1)pom.xml添加相关依赖包:本示例的版本 => Spring Boot 1.5.15.RELEASE,Spring Cloud Edgware.SR3


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
1    <dependencies>
2        <dependency>
3            <groupId>org.springframework.boot</groupId>
4            <artifactId>spring-boot-starter</artifactId>
5        </dependency>
6
7        <!-- zuul -->
8        <dependency>
9            <groupId>org.springframework.cloud</groupId>
10            <artifactId>spring-cloud-starter-zuul</artifactId>
11        </dependency>
12
13        <!-- eureka -->
14        <dependency>
15            <groupId>org.springframework.cloud</groupId>
16            <artifactId>spring-cloud-starter-eureka</artifactId>
17        </dependency>
18
19        <!-- 热启动,热部署依赖包,为了调试方便,加入此包 -->
20        <dependency>
21            <groupId>org.springframework.boot</groupId>
22            <artifactId>spring-boot-devtools</artifactId>
23            <optional>true</optional>
24        </dependency>
25    </dependencies>
26
27    <!-- spring cloud dependencies -->
28    <dependencyManagement>
29        <dependencies>
30            <dependency>
31                <groupId>org.springframework.cloud</groupId>
32                <artifactId>spring-cloud-dependencies</artifactId>
33                <version>Edgware.SR3</version>
34                <type>pom</type>
35                <scope>import</scope>
36            </dependency>
37        </dependencies>
38    </dependencyManagement>
39

(2)启动类添加@EnableZuulProxy注解


1
2
3
4
5
6
7
8
1@SpringBootApplication
2@EnableZuulProxy
3public class ZuulServiceApplication {
4    public static void main(String[] args) {
5        SpringApplication.run(ZuulServiceApplication.class, args);
6    }
7}
8

(3)添加必要配置(application.yml):主要是针对Eureka的配置,本示例将Zuul也作为一个Eureka Client注册到Eureka Server中。


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
1server:
2  port: 5000
3
4spring:
5  application:
6    name: zuul-gateway-service
7
8eureka:
9  client:
10    serviceUrl:
11      defaultZone: http://localhost:8761/eureka/
12  instance:
13    prefer-ip-address: true # 优先注册IP地址而不是hostname
14    instance-id: zuul-gateway-container:${server.port}
15  healthcheck:
16    enabled: true # 启用健康检查,注意:需要引用spring boot actuator
17
18management:
19  security:
20    enabled: false # 默认为true,改为false以便可以看到routes
21

启动Eureka Server和Zuul Server之后:

ASP.NET Core微服务之基于Steeltoe集成Zuul实现统一API网关

示例代码:https://github.com/EdisonChou/Microservice.PoC.Steeltoe/tree/master/springcloud/zuul-service

三、快速验证测试

基于第一篇的三个已注册到Eureka的ASP.NET Core WebAPI示例项目(示例代码:https://github.com/EdisonChou/Microservice.PoC.Steeltoe/tree/master/src/Chapter1-ServiceDiscovery),无须做任何修改,启动并注册到Eureka之后的服务列表:

ASP.NET Core微服务之基于Steeltoe集成Zuul实现统一API网关

(1)通过Zuul访问Agent-Service

ASP.NET Core微服务之基于Steeltoe集成Zuul实现统一API网关

(2)通过Zuul访问Premium-Service

ASP.NET Core微服务之基于Steeltoe集成Zuul实现统一API网关

(3)通过Zuul访问Client-Service (多Client-Service实例,验证负载均衡)

ASP.NET Core微服务之基于Steeltoe集成Zuul实现统一API网关

四、小结

本文极简地介绍了一下Spring Cloud Zuul,并使用Java快速地编写了一个API网关Zuul Server,然后基于上一篇的三个ASP.NET Core演示了一下API网关的效果。当然,对于Zuul还有很多内容,这里就不再一一演示,有兴趣的童鞋或者对这种混搭式的架构感兴趣的童鞋可以去了解一下。

给TA打赏
共{{data.count}}人
人已打赏
安全经验

Git 版本回退

2021-10-11 16:36:11

安全经验

安全咨询服务

2022-1-12 14:11:49

个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索