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

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

=>  Steeltoe目录快速导航java

1. 基于Steeltoe使用Spring Cloud Eurekagit

2. 基于Steeltoe使用Spring Cloud Zuulgithub

3. 基于Steeltoe使用Spring Cloud Hystrixspring

4. 基于Steeltoe使用Spring Cloud Config安全

5. 基于Steeltoe使用Zipkin架构

1、关于Spring Cloud Zuul

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

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

  

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

2、快速构建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

3、快速验证测试

  基于第一篇的三个已注册到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实例,验证负载均衡)

  

4、小结

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

 

做者:周旭龙

出处:http://edisonchou.cnblogs.com

本文版权归做者和博客园共有,欢迎转载,但未经做者赞成必须保留此段声明,且在文章页面明显位置给出原文连接。

相关文章
相关标签/搜索