这里写图片描述

Spring Cloud是Spring总多的Project中的一个,它提供了一整套的工具帮助系统架构师们在进行分布式设计的时候可以拿来即用, 在创建和发布微服务时极为便捷和有效。
本系列文章将会使用最简单的例子和最为容易的方式来学习Spring Cloud。本文将会介绍如何引入Zuul在微服务的架构中简单实现API网关。

构成

项目详细
Config ServiceSpring Cloud Config:统一配置管理服务
Dashboard ServiceHystrix Dashboard
Api Route ServiceZuul:Api Gateway
Discovery ServiceEureka:服务发现
User ServiceRESTFUL的用户相关的服务
Org ServiceRESTFUL的组织相关的服务

这里写图片描述

微服务架构

>Api Gateway是微服务架构中需要考虑的情况之一,详细信息此处不再展开有兴趣的可以查看下面这篇文章

URLhttp://microservices.io/patterns/microservices.html

这里写图片描述

Api Gateway

在很多的场景下,尽量少的修改代码或者不修改代码而将不同服务的入口进行统一管理,将API网关作为用户的唯一入口,可以参看Richardson的文章里面的例子来对此概念的展开进行进一步地理解。

URLhttp://microservices.io/patterns/apigateway.html

这里写图片描述

Zuul介绍

>Zuul 是Spring Cloud 子项目Spring Cloud Netflix的一个组件,它是Netflix对ApiGateway实现的一份答卷,应用非常广泛。常见的功能如下

  • 身份验证
  • 压力测试
  • Canary Testing
  • 动态路由
  • 安全控制
    ...

>Zuul作为中间这一层,如何处理相关请求。

这里写图片描述

场景

>使用我们前面创建的例子,我们有user和 org两个服务,分别在9001和9002端口提供服务,我们如果想要查询用户和组织的详细信息,一般是通过如下方式进行访问的。

项目访问方法(URL)
用户详细http://localhost:9001/user/detail
组织详细http://localhost:9002/org/detail

Pom详细

<?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>

    <groupId>com.liumiaocn.demo.springcloud</groupId>
    <artifactId>apirouteservice</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>apirouteservice</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.4.3.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-ribbon</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-zuul</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Camden.BUILD-SNAPSHOT</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

    <repositories>
        <repository>
            <id>spring-snapshots</id>
            <name>Spring Snapshots</name>
            <url>https://repo.spring.io/snapshot</url>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
        </repository>
        <repository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>https://repo.spring.io/milestone</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
    </repositories>

</project>

apirouteservice

只需要加入EnableZuulProxy注解。

package com.liumiaocn.demo.springcloud;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;

@SpringBootApplication
@EnableEurekaClient
@EnableZuulProxy
public class ApirouteserviceApplication {

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

设定文件

server.port=8001
spring.application.name=apirouteservice
eureka.client.serviceUrl.defaultZone=http://localhost:8801/eureka/

zuul.routes.userservice.path=/user/**
zuul.routes.userservice.strPrefix=true
zuul.routes.userservice.url=http://localhost:9001/user
zuul.routes.orgservice.path=/org/**
zuul.routes.orgservice.strPrefix=true
zuul.routes.orgservice.url=http://localhost:9002/org
项目详细
server.portapirouteService提供服务所用Port,将作为用户统一端口访问各个服务
spring.application.name向Eureka Server进行注册时使用的服务名
eureka.client.serviceUrl.defaultZonehttp://localhost:8801/eureka/ 注意此处的8801端口号需要跟Server端一致。
zuul.routes.服务名.path注意此处服务名要与注册的服务名一致
zuul.routes.服务名.strPrefix是否清除前缀(false
zuul.routes.服务名.url注意此处服务名要与注册的服务名一致,同时url的层次也需要注意

生成Package

项目详细
命令mvn clean package
执行场所pom所在目录
目标文件所在目录工程根目录下Target
目标文件名称apirouteservice-0.0.1-SNAPSHOT.jar

统一访问方式

项目访问方法(URL)
用户详细http://localhost:8001/user/detail
组织详细http://localhost:8001/org/detail

启动

>启动各个服务后,也启动apirouteservice

Eureka确认

这里写图片描述

User详细信息

>通过Zuul统一访问用户详细信息

这里写图片描述

Org详细信息、

>通过Zuul统一访问组织详细信息

这里写图片描述

总结

>本文使用zuul实现了Api网关最简单的功能,但是很重要的是我们了解到了如何获取封装这一层的方法,可以分发页面就可以控制负载等,在接下来的文章里我们会继续学习Spring Cloud的各种其他强大功能模块。

Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐