Spring基礎:快速入門spring cloud):API閘道器之Zuul
Spring Cloud是Spring總多的Project中的一個,它提供了一整套的工具幫助系統架構師們在進行分散式設計的時候可以拿來即用, 在建立和釋出微服務時極為便捷和有效。
本系列文章將會使用最簡單的例子和最為容易的方式來學習Spring Cloud。本文將會介紹如何引入Zuul在微服務的架構中簡單實現API閘道器。
構成
專案 | 詳細 |
---|---|
Config Service | Spring Cloud Config:統一配置管理服務 |
Dashboard Service | Hystrix Dashboard |
Api Route Service | Zuul:Api Gateway |
Discovery Service | Eureka:服務發現 |
User Service | RESTFUL的使用者相關的服務 |
Org Service | RESTFUL的組織相關的服務 |
微服務架構
>Api Gateway是微服務架構中需要考慮的情況之一,詳細資訊此處不再展開有興趣的可以檢視下面這篇文章
URL | http://microservices.io/patterns/microservices.html |
---|
Api Gateway
在很多的場景下,儘量少的修改程式碼或者不修改程式碼而將不同服務的入口進行統一管理,將API閘道器作為使用者的唯一入口,可以參看Richardson的文章裡面的例子來對此概念的展開進行進一步地理解。
URL | http://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>
- 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
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
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);
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
設定檔案
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
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
專案 | 詳細 |
---|---|
server.port | apirouteService提供服務所用Port,將作為使用者統一埠訪問各個服務 |
spring.application.name | 向Eureka Server進行註冊時使用的服務名 |
eureka.client.serviceUrl.defaultZone | http://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的各種其他強大功能模組。
再分享一下我老師大神的人工智慧教程吧。零基礎!通俗易懂!風趣幽默!希望你也加入到我們人工智慧的隊伍中來!http://www.captainbed.net