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同意暴露。
有关Zuul的更多内容,请参考我的这一篇:《Spring Cloud微服务架构学习笔记与示例》,这里不是本文重点,不再赘述。
二、快速构建Zuul Server
(1)pom.xml添加相关依赖包:本示例的版本 => Spring Boot 1.5.15.RELEASE,Spring Cloud Edgware.SR3
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <!-- zuul --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-zuul</artifactId> </dependency> <!-- eureka --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency> <!-- 热启动,热部署依赖包,为了调试方便,加入此包 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <optional>true</optional> </dependency> </dependencies> <!-- spring cloud dependencies --> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Edgware.SR3</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>
(2)启动类添加@EnableZuulProxy注解
@SpringBootApplication @EnableZuulProxy public class ZuulServiceApplication { public static void main(String[] args) { SpringApplication.run(ZuulServiceApplication.class, args); } }
(3)添加必要配置(application.yml):主要是针对Eureka的配置,本示例将Zuul也作为一个Eureka Client注册到Eureka Server中。
server: port: 5000 spring: application: name: zuul-gateway-service eureka: client: serviceUrl: defaultZone: http://localhost:8761/eureka/ instance: prefer-ip-address: true # 优先注册IP地址而不是hostname instance-id: zuul-gateway-container:${server.port} healthcheck: enabled: true # 启用健康检查,注意:需要引用spring boot actuator management: security: enabled: false # 默认为true,改为false以便可以看到routes
启动Eureka Server和Zuul Server之后:
示例代码: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之后的服务列表:
(1)通过Zuul访问Agent-Service
(2)通过Zuul访问Premium-Service
(3)通过Zuul访问Client-Service (多Client-Service实例,验证负载均衡)
四、小结
本文极简地介绍了一下Spring Cloud Zuul,并使用Java快速地编写了一个API网关Zuul Server,然后基于上一篇的三个ASP.NET Core演示了一下API网关的效果。当然,对于Zuul还有很多内容,这里就不再一一演示,有兴趣的童鞋或者对这种混搭式的架构感兴趣的童鞋可以去了解一下。
低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。
持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。
转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。
- 上一篇
ASP.NET Core微服务之基于Steeltoe使用Eureka实现服务注册与发现
Tip: 此篇已加入.NET Core微服务基础系列文章索引 一、关于Steeltoe与Spring Cloud Steeltoe的官方地址:http://steeltoe.io/,其官方介绍如下: Steeltoe is an open source project that enables .NET developers to implement industry standard best practices when building resilient microservices for the cloud. The Steeltoe client libraries enable .NET Core and .NET Framework apps to easily leverage Netflix Eureka, Hystrix, Spring Cloud Config Server, and Cloud Foundry services. 我们主要关注的就是这句话:_enable .NET Core and .NET Framework apps to easily l...
- 下一篇
ASP.NET Core微服务之基于Steeltoe使用Hystrix熔断保护与监控
Tip: 此篇已加入.NET Core微服务基础系列文章索引 _=>_ Steeltoe目录快速导航: 基于Steeltoe使用Spring Cloud Eureka 基于Steeltoe使用Spring Cloud Zuul 基于Steeltoe使用Spring Cloud Hystrix 一、关于Spring Cloud Hystrix 在微服务架构中,我们将系统拆分为很多个服务,各个服务之间通过注册与订阅的方式相互依赖,由于各个服务都是在各自的进程中运行,就有可能由于网络原因或者服务自身的问题导致调用故障或延迟,随着服务的积压,可能会导致服务崩溃。为了解决这一系列的问题,断路器等一系列服务保护机制出现了。 断路器本身是一种开关保护机制,用于在电路上保护线路过载,当线路中有电器发生短路时,断路器能够及时切断故障电路,防止发生过载、发热甚至起火等严重后果。 针对上述问题,Spring Cloud Hystrix 实现了断路器、线路隔离等一系列服务保护功能。它也是基于 Netflix 的开源框架 Hystrix 实现的,该框架的目标在于通过控制那些访问远程系统、服务和第三方库的节...
相关文章
文章评论
共有0条评论来说两句吧...