1. 程式人生 > >03 基於閘道器服務的OAuth2驗證(OAuth2 Authentication Code Grant 授權碼模式)

03 基於閘道器服務的OAuth2驗證(OAuth2 Authentication Code Grant 授權碼模式)

https://getkong.org/plugins/oauth2-authentication

我們演示還是用books 的Restful api資料介面,把Kong Gateway - 01範例中PostgresSQL中的kong資料庫刪掉,

匯入一個已經配置好的乾乾淨淨的後臺資料庫kong-20180427.bak
(參看安裝篇 How to Install kong-community-edition On Cent OS 7)

[[email protected] ~]# pg_dump --help  
[[email protected] ~]# psql --help  
[

[email protected] ~]# dropdb --help  
[[email protected] ~]# createdb --help  
[[email protected] ~]# kong stop  # kong 服務必須先停止執行
[[email protected] ~]# dropdb -h 127.0.0.1 -p 5432 -U postgres kong   # 刪除kong資料庫  
Password: 123456  
[[email protected] ~]# createdb -h 127.0.0.1 -p 5432 -U postgres kong   # 建立kong資料庫  
Password: 123456  
[
[email protected]
~]# psql -h 127.0.0.1 -p 5432 -U postgres -d kong < /opt/kong-20180427.bak   # 恢復kong資料庫  
Password for user postgres: 123456 
[[email protected] ~]# kong start
Kong started

用Kong配置一個book服務
在安裝並啟動Kong之後,使用Kong的管理API埠8001新增一個名稱為book的服務
[[email protected] ~]# curl -i -X POST \
--url http://localhost:8001/services/ \
--data 'name=book' \
--data 'url=http://contoso.com/v1/books'

HTTP/1.1 201 Created
Date: Sun, 06 May 2018 16:25:47 GMT
Content-Type: application/json; charset=utf-8
Transfer-Encoding: chunked
Connection: keep-alive
Access-Control-Allow-Origin: *
Server: kong/0.13.1
 
{
    "host": "contoso.com", 
    "created_at": 1525595147, 
    "connect_timeout": 60000, 
    "id": "2d3d56de-02c4-4517-b786-2dc4037bf23d", 
    "protocol": "http", 
    "name": "book", 
    "read_timeout": 60000, 
    "port": 80, 
    "path": "/v1/books", 
    "updated_at": 1525595147, 
    "retries": 5, 
    "write_timeout": 60000
}
以下幾條命令不必執行,以後會用到
查詢已分配了服務名稱的路由列表
curl -i -X GET \
--url http://localhost:8001/services/book/routes
 
查詢所有路由列表
curl -i -X GET \
--url http://localhost:8001/routes
 
根據路由id查詢1條路由
curl -i -X GET \
--url http://localhost:8001/routes/4e0ddea7-ec70-41b9-bdd1-9b7c893b8ede 
 
根據路由id刪除1條路由
curl -i -X DELETE \
--url http://localhost:8001/routes/4e0ddea7-ec70-41b9-bdd1-9b7c893b8ede
 
根據id,hosts修改1條路由,根據同一名稱的book服務,配置methods引數無
法用不同的路由來區分控制器方法的許可權,故不用設定methods引數;
修改路由的方式無法設定引數的null值,我們只能刪掉路由,然後建立路由來實現
curl -i -X PATCH \
--url http://localhost:8001/routes/4e0ddea7-ec70-41b9-bdd1-9b7c893b8ede \
--data 'hosts[]=contoso.com' \
--data 'paths[]=/v1/books' 
新增一個路由(paths[]的值必須與book服務中的/v1/books一致)
使book服務暴露出來以供使用者訪問,book服務沒必要新增多個路由。
[[email protected] ~]# curl -i -X POST \
--url http://localhost:8001/services/book/routes \
--data 'hosts[]=contoso.com' \
--data 'paths[]=/v1/books'
HTTP/1.1 201 Created
Date: Sun, 06 May 2018 16:27:51 GMT
Content-Type: application/json; charset=utf-8
Transfer-Encoding: chunked
Connection: keep-alive
Access-Control-Allow-Origin: *
Server: kong/0.13.1
 
{
    "created_at": 1525595271, 
    "strip_path": true, 
    "hosts": [
        "contoso.com"
    ], 
    "preserve_host": false, 
    "regex_priority": 0, 
    "updated_at": 1525595271, 
    "paths": [
        "/v1/books"
    ], 
    "service": {
        "id": "2d3d56de-02c4-4517-b786-2dc4037bf23d"
    }, 
    "methods": null, 
    "protocols": [
        "http", 
        "https"
    ], 
    "id": "bacfd048-dbcc-453a-bbce-a29e8d3f86b7"
}
通過Kong在8000埠暴露出來的服務地址獲得所有的書籍
[[email protected] ~]# curl -i -X GET \
--url http://localhost:8000/v1/books \
--header 'Host: contoso.com'
HTTP/1.1 200 OK
Content-Type: application/json; charset=utf-8
Content-Length: 244
Connection: keep-alive
Date: Sun, 06 May 2018 16:28:40 GMT
Server: Apache/2.4.6 (CentOS) OpenSSL/1.0.2k-fips PHP/7.1.13
X-Powered-By: PHP/7.1.13
X-Kong-Upstream-Latency: 29
X-Kong-Proxy-Latency: 49
Via: kong/0.13.1
 
[
    {
        "id": 1, 
        "title": "Fashion That Changed the World", 
        "author": "Jennifer Croll"
    }, 
    {
        "id": 2, 
        "title": "Brigitte Bardot - My Life in Fashion", 
        "author": "Henry-Jean Servat and Brigitte Bardot"
    }, 
    {
        "id": 3, 
        "title": "The Fashion Image", 
        "author": "Thomas Werner"
    }
]
curl http://localhost:8001/services/book
curl http://localhost:8001/services/book/plugins

為book服務啟用OAuth 2.0 Authentication外掛,並激活授權碼模式
[[email protected] ~]# curl -i -X POST \
--url http://localhost:8001/services/book/plugins \
--data "name=oauth2"  \
--data "config.scopes=email,phone,address" \
--data "config.mandatory_scope=true" \
--data "config.enable_authorization_code=true"

HTTP/1.1 201 Created
Date: Sun, 06 May 2018 16:30:11 GMT
Content-Type: application/json; charset=utf-8
Transfer-Encoding: chunked
Connection: keep-alive
Access-Control-Allow-Origin: *
Server: kong/0.13.1
 
{
    "created_at": 1525624193000, 
    "config": {
        "refresh_token_ttl": 1209600, 
        "scopes": [
            "email", 
            "phone", 
            "address"
        ], 
        "mandatory_scope": true, 
        "provision_key": "5o5KnTRlpySbf7ViwYSkWPAZZ4vufSwe", 
        "hide_credentials": false, 
        "enable_authorization_code": true, 
        "enable_implicit_grant": false, 
        "global_credentials": false, 
        "accept_http_if_already_terminated": false, 
        "enable_password_grant": false, 
        "enable_client_credentials": false, 
        "anonymous": "", 
        "token_expiration": 7200, 
        "auth_header_name": "authorization"
    }, 
    "id": "acacd3e0-1c16-4301-8572-51221b46e997", 
    "enabled": true, 
    "service_id": "2d3d56de-02c4-4517-b786-2dc4037bf23d", 
    "name": "oauth2"
}
新增1個username為jack的消費者,{custom_id}引數可省略,此引數是個自定義唯一標識,
它作用是把消費者jack對映到另外一個數據庫上
[[email protected] ~]# curl -i -X POST \
--url http://localhost:8001/consumers/  \
--data "username=jack"
HTTP/1.1 201 Created
Date: Sun, 06 May 2018 16:33:14 GMT
Content-Type: application/json; charset=utf-8
Transfer-Encoding: chunked
Connection: keep-alive
Access-Control-Allow-Origin: *
Server: kong/0.13.1
 
{
    "created_at": 1525624395000, 
    "username": "jack", 
    "id": "786d2951-2744-4de2-bcf2-448b6b0ac954"
}
為消費者jack建立1個名稱為Book App的應用,redirect_uri引數定義傳送code和state的回撥地址
引數{client_id}和{client_secret}可自定義,省略時由系統隨機生成
[[email protected] ~]# curl -i -X POST \
--url http://localhost:8001/consumers/jack/oauth2/ \
--data "name=Book App" \
--data "redirect_uri=http://getkong.org/" 
HTTP/1.1 201 Created
Date: Sun, 06 May 2018 16:34:16 GMT
Content-Type: application/json; charset=utf-8
Transfer-Encoding: chunked
Connection: keep-alive
Access-Control-Allow-Origin: *
Server: kong/0.13.1
 
{
    "client_id": "LzFEyMMaQyRIHsSsqfonZfofQIHigOF4", 
    "created_at": 1525624457000, 
    "id": "4858dcb0-9f2b-4d6e-acc5-c76f9ac5ca17", 
    "redirect_uri": [
        "http://getkong.org/"
    ], 
    "name": "Book App", 
    "client_secret": "YhCHW7xISxmTPd41qJFkjDkcsurVADUV", 
    "consumer_id": "786d2951-2744-4de2-bcf2-448b6b0ac954"
}
根據{client_id}查詢消費者的應用程式資訊
[[email protected] ~]# curl -i -X GET \
--url http://localhost:8001/oauth2 \
--data "client_id=LzFEyMMaQyRIHsSsqfonZfofQIHigOF4"
HTTP/1.1 200 OK
Date: Sun, 06 May 2018 16:35:17 GMT
Content-Type: application/json; charset=utf-8
Transfer-Encoding: chunked
Connection: keep-alive
Access-Control-Allow-Origin: *
Server: kong/0.13.1
 
{
    "total": 1, 
    "data": [
        {
            "created_at": 1525624457000, 
            "client_id": "LzFEyMMaQyRIHsSsqfonZfofQIHigOF4", 
            "id": "4858dcb0-9f2b-4d6e-acc5-c76f9ac5ca17", 
            "redirect_uri": [
                "http://getkong.org/"
            ], 
            "name": "Book App", 
            "client_secret": "YhCHW7xISxmTPd41qJFkjDkcsurVADUV", 
            "consumer_id": "786d2951-2744-4de2-bcf2-448b6b0ac954"
        }
    ]
}
通過Kong在8000埠暴露出來的服務地址讀一條書籍記錄,實際上是通過Kong在轉
發我的請求,不管是讀1條記錄還讀所有書籍記錄,我們都無權獲得資料
[[email protected] ~]# curl -i -X GET \
--url http://localhost:8000/v1/books/2 \
--header 'Host: contoso.com'
HTTP/1.1 401 Unauthorized
Date: Sun, 06 May 2018 16:35:55 GMT
Content-Type: application/json; charset=utf-8
Transfer-Encoding: chunked
Connection: keep-alive
Server: kong/0.13.1
WWW-Authenticate: Bearer realm="service"
 
{
    "error_description": "The access token is missing", 
    "error": "invalid_request"
}
很顯然,命令中沒有提供訪問令牌,此命令已經無法訪問書籍介面了,
鍵-值對{username:password}字串 [email protected]:123456 
它的Base64編碼值等於amFja0Bob3RtYWlsLmNvbSUzQTEyMzQ1Ng==

curl http://localhost:8001/consumers/jack/oauth2

你的Web應用終端將驗證由客戶端傳送的username和password,

如果驗證成功此客戶端將傳送由引數{client_id},{response_type},
{scope},{provision_key},{authenticated_userid},{state}
構成的POST請求獲得一個code值,必須帶上header頭引數Authorization

{state}客戶端的當前狀態,可以指定任意值,認證伺服器會原封不動地返回這個值
{scope}表示申請的許可權範圍
{authenticated_userid}已授予許可權的終端登入使用者userid

[[email protected] ~]# curl -i -X POST \
--url https://localhost:8443/v1/books/oauth2/authorize \
--header "Authorization: Basic amFja0Bob3RtYWlsLmNvbSUzQTEyMzQ1Ng==" \
--header 'Host: contoso.com' \
--data "client_id=LzFEyMMaQyRIHsSsqfonZfofQIHigOF4" \
--data "response_type=code" \
--data "scope=email%20address" \
--data "provision_key=5o5KnTRlpySbf7ViwYSkWPAZZ4vufSwe" \
--data "authenticated_userid=1206" \
--data "state=xyz"  --insecure
HTTP/1.1 200 OK
Date: Sun, 06 May 2018 16:40:25 GMT
Content-Type: application/json; charset=utf-8
Transfer-Encoding: chunked
Connection: keep-alive
Server: kong/0.13.1
cache-control: no-store
pragma: no-cache
 
{"redirect_uri":"http:\/\/getkong.org\/?code=QEdYs44He66RewGGE4KVDxp2nm0mgweS&state=xyz"}
客戶繼續傳送第2個由引數{grant_type},{client_id},{client_secret},{code}
構成的POST請求去申請一個訪問令牌和重新整理令牌
{code}值獲得令牌後立即失效,即{code}值只能用一次

[[email protected] ~]# curl -i -X POST \
--url https://localhost:8443/v1/books/oauth2/token \
--header "Host: contoso.com" \
--data "grant_type=authorization_code" \
--data "client_id=LzFEyMMaQyRIHsSsqfonZfofQIHigOF4" \
--data "client_secret=YhCHW7xISxmTPd41qJFkjDkcsurVADUV" \
--data "code=QEdYs44He66RewGGE4KVDxp2nm0mgweS" --insecure
HTTP/1.1 200 OK
Date: Sun, 06 May 2018 16:41:32 GMT
Content-Type: application/json; charset=utf-8
Transfer-Encoding: chunked
Connection: keep-alive
Server: kong/0.13.1
cache-control: no-store
pragma: no-cache
 
{
    "refresh_token": "Wj51vhdah1Ow9VGl6VTIZZKYqvlln8iv", 
    "token_type": "bearer", 
    "access_token": "90zG0QVO9m921iS51dLAFGMJnNky7IgK", 
    "expires_in": 7200
}
現在我們已經用一個隨機的code交換獲得了一個訪問令牌和一個重新整理令牌
這樣就有可以訪問書籍這個介面了
[[email protected] ~]# curl -i -X GET \
--url https://localhost:8443/v1/books \
--header "Authorization: Bearer 90zG0QVO9m921iS51dLAFGMJnNky7IgK" \
--header 'Host: contoso.com' --insecure
HTTP/1.1 200 OK
Content-Type: application/json; charset=utf-8
Content-Length: 244
Connection: keep-alive
Date: Sun, 06 May 2018 16:43:22 GMT
Server: Apache/2.4.6 (CentOS) OpenSSL/1.0.2k-fips PHP/7.1.13
X-Powered-By: PHP/7.1.13
X-Kong-Upstream-Latency: 25
X-Kong-Proxy-Latency: 44
Via: kong/0.13.1
 
[
    {
        "id": 1, 
        "title": "Fashion That Changed the World", 
        "author": "Jennifer Croll"
    }, 
    {
        "id": 2, 
        "title": "Brigitte Bardot - My Life in Fashion", 
        "author": "Henry-Jean Servat and Brigitte Bardot"
    }, 
    {
        "id": 3, 
        "title": "The Fashion Image", 
        "author": "Thomas Werner"
    }
]
[[email protected] ~]# curl -i -X GET \
--url http://localhost:8000/v1/books/2 \
--header "Authorization: Bearer 90zG0QVO9m921iS51dLAFGMJnNky7IgK" \
--header 'Host: contoso.com'
HTTP/1.1 200 OK
Content-Type: application/json; charset=utf-8
Content-Length: 106
Connection: keep-alive
Date: Sun, 06 May 2018 16:44:18 GMT
Server: Apache/2.4.6 (CentOS) OpenSSL/1.0.2k-fips PHP/7.1.13
X-Powered-By: PHP/7.1.13
X-Kong-Upstream-Latency: 27
X-Kong-Proxy-Latency: 0
Via: kong/0.13.1
 
[
    {
        "id": 2, 
        "title": "Brigitte Bardot - My Life in Fashion", 
        "author": "Henry-Jean Servat and Brigitte Bardot"
    }
]
使用一個重新整理令牌去獲得一個新的訪問令牌和一個更新的重新整理令牌,前面的重新整理令牌與訪問令牌就立即作廢了

[[email protected] ~]# curl -i -X POST https://localhost:8443/v1/books/oauth2/token \

--header 'Host: contoso.com' \
--data "grant_type=refresh_token" \
--data "client_id=LzFEyMMaQyRIHsSsqfonZfofQIHigOF4" \
--data "client_secret=YhCHW7xISxmTPd41qJFkjDkcsurVADUV" \
--data "refresh_token=Wj51vhdah1Ow9VGl6VTIZZKYqvlln8iv" --insecure
HTTP/1.1 200 OK
Date: Sun, 06 May 2018 16:45:25 GMT
Content-Type: application/json; charset=utf-8
Transfer-Encoding: chunked
Connection: keep-alive
Server: kong/0.13.1
cache-control: no-store
pragma: no-cache
 
{
    "refresh_token": "0mKdeGqC4LZRzNNwlhNj5OyaAZXRajZp", 
    "token_type": "bearer", 
    "access_token": "XNURzUlIi4gtwkjZPeWkQrv0QMZnUFET", 
    "expires_in": 7200
}
用更新的訪問令牌去刪除一條書籍資料

[[email protected] ~]# curl -i -X DELETE \

--url https://localhost:8443/v1/books/2 \
--header "Authorization: Bearer XNURzUlIi4gtwkjZPeWkQrv0QMZnUFET" \
--header 'Host: contoso.com'  --insecure
HTTP/1.1 200 OK
Content-Type: application/json; charset=utf-8
Content-Length: 34
Connection: keep-alive
Date: Sun, 06 May 2018 16:48:07 GMT
Server: Apache/2.4.6 (CentOS) OpenSSL/1.0.2k-fips PHP/7.1.13
X-Powered-By: PHP/7.1.13
X-Kong-Upstream-Latency: 32
X-Kong-Proxy-Latency: 3
Via: kong/0.13.1
 
{"message":"deleted successfully"}
用更新的訪問令牌去新增一條書籍資料
[[email protected] ~]# curl -i -X POST \
--url https://localhost:8443/v1/books \
--header "Authorization: Bearer XNURzUlIi4gtwkjZPeWkQrv0QMZnUFET" \
--header 'Host: contoso.com' \
--data 'title=TiDB in Action' \
--data 'author=Tomson'  --insecure
HTTP/1.1 200 OK
Content-Type: application/json; charset=utf-8
Content-Length: 35
Connection: keep-alive
Date: Sun, 06 May 2018 16:48:47 GMT
Server: Apache/2.4.6 (CentOS) OpenSSL/1.0.2k-fips PHP/7.1.13
X-Powered-By: PHP/7.1.13
X-Kong-Upstream-Latency: 29
X-Kong-Proxy-Latency: 0
Via: kong/0.13.1
 
{"message":"inserted successfully"}

--------------------- 
作者:zhengzizhi 
來源:CSDN 
原文:https://blog.csdn.net/zhengzizhi/article/details/80221977 
版權宣告:本文為博主原創文章,轉載請附上博文連結!

相關推薦

03 基於服務OAuth2驗證OAuth2 Authentication Code Grant 授權模式

https://getkong.org/plugins/oauth2-authentication 我們演示還是用books 的Restful api資料介面,把Kong Gateway - 01範例中PostgresSQL中的kong資料庫刪掉, 匯入一個已經配置好

Ocelot.JwtAuthorize:一個基於的Jwt驗證

Ocelot作為基於.net core的API方關,有一個功能是統一驗證,它的作用是把沒有訪問許可權的請求擋在API閘道器外面,而不是到達API閘道器事端的API時才去驗證;之前我有一篇博文https://www.cnblogs.com/axzxs2001/p/8005084.html,作過說明,這篇博文說明

13 基於服務的IP白名單限制訪問Whitelist IP Restriction

用Kong配置一個book服務在安裝並啟動Kong之後,使用Kong的管理API埠8001新增一個名稱為book的服務[[email protected] ~]# curl -i -X POST \--url http://localhost:8001/servic

12 基於服務的跨源資源共享(CORS)

如果在前面11篇Kong Gateway系列的文章中,你親自動手實驗過用瀏覽器訪問以下地址:http://localhost:8000/v1/books你將無法獲得書籍介面返回的書籍記錄,本篇blog能讓你在瀏覽器中用8000埠或者8443埠能直接訪問書籍的Restful A

18 基於服務的請求大小限制Request Size Limiting

Configure a Service in Kong[[email protected] ~]# curl -i -X POST \--url http://localhost:8001/services/ \--data 'name=book' \--data

服務從零搭建——搭建api不帶驗證

環境準備 建立空的core2.1 api專案  演示使用名稱APIGateWay  過程參考上一篇 完成後在appsettings.json 新增節點 "Setting": { "Port": "5000" } 搭建過程 新增檔案configuration.json

基於.NET CORE微服務框架 -Api服務管理

最近也更新了surging新的版本 更新內容: 1. 擴充套件Zookeeper封裝 2. 增加服務元資料 3. 增加API閘道器 開源地址:https://github.com/dotnetcore/surging 2.軟體環境 IDE:Visual Studio 2017 1

.NET Core微服務基於Ocelot實現API服務

一、啥是API閘道器?   API 閘道器一般放到微服務的最前端,並且要讓API 閘道器變成由應用所發起的每個請求的入口。這樣就可以明顯的簡化客戶端實現和微服務應用程式之間的溝通方式。以前的話,客戶端不得不去請求微服務A(假設為Customers),然後再到微服務B(假設為Orders),然後是微服

.NET Core微服務基於Ocelot實現API服務

一、負載均衡與請求快取 1.1 負載均衡   為了驗證負載均衡,這裡我們配置了兩個Consul Client節點,其中ClientService分別部署於這兩個節點內(192.168.80.70與192.168.80.71)。   為了更好的展示API Repsonse來自哪個節點,我們更改一下

springcloud之Zuul服務

Zuul是Netflix開源的微服務閘道器,它的核心是一系列的過濾器,這些過濾器可以完成以下功能: 身份認證與安全:識別每個資源的驗證要求,並拒絕那些與要求不符的請求。 審查與監控:在邊緣位置追蹤有意義的資料和統計結果,從而帶來精確的生產檢視。 動態路由:動態的請求路由到不同的後端叢集。

Spring Cloud ZuulAPI服務3

過濾器 在Spring Cloud Zuul中實現的過濾器必須包含4個基本特徵:過濾型別,執行順序,執行條件,具體操作。這就是ZuulFilter介面中定義的4個抽象方法: public abstract String filterType(); public abst

Spring Cloud ZuulAPI服務2

路由詳情 傳統路由配置 傳統路由配置方式就是在不依賴與服務發現機制的情況下,通過在配置檔案中具體指定每個路由表示式與服務例項的對映關係來實現API閘道器對外部請求的路由。 單例項配置:通過zuul.routes.<route>.path與zuul.routes.<r

Spring Cloud ZuulAPI服務1

API閘道器是一個智慧的應用伺服器,它的定義類似於面向物件設計模式中的Facade模式,它的存在就像是整個微服務架構系統的門面一樣,所有的外部客戶端訪問都需要經過他來進行排程和過濾。它除了要實現請求路由,負載均衡,校驗過濾等功能之外,還需要更多能力,比如與服務治理框架的結合,請求轉發時的熔斷機制

服務從零搭建——ocelot配置追蹤功能

butterfly 準備工作 首先下載buterfly release版本  解壓並通過命令啟動:dotnet Butterfly.Web.dll --EnableHttpCollector=true 可以採用bat檔案的方式  cd C:\Users\Lenovo\Desk

SpringCloud之服務(gateway)

前言 閘道器服務在SpringCloud中有很重要的作用。 可以將服務跟外網進行隔離起到一定的保護作用,同時服務間區域網通訊更加快捷。而且在閘道器中可以做限流、許可權校驗,使得服務更加專注自身業務。比如說下訂單需要登入許可權,限流,我們在本篇將介紹如何使用。 搭建閘道器專案 注意:需要新增Eureka

api服務 zuul-路由

路由是微服務架構中必須的一部分,比如,“/” 可能對映到你的WEB程式上,”/api/users “可能對映到你的使用者服務上,“/api/shop”可能對映到你的商品服務商。(註解:我理解這裡的這幾個對映就是說通過Zuul這個閘道器把服務對映到不同的服務商去處理,從而變成了微服務!) 通過Zuu

Spring Cloud:服務Zuul高階篇11

時間過的很快,寫springcloud(十):服務閘道器zuul初級篇還在半年前,現在已經是2018年了,我們繼續探討Zuul更高階的使用方式。 上篇文章主要介紹了Zuul閘道器使用模式,以及自動轉發機制,但其實Zuul還有更多的應用場景,比如:鑑權、流量轉發、請求統計等等,這些功能都可以使用Z

Spring Cloud系列二十三 API服務Spring Cloud ZuulFinchley.RC2版本

為什麼使用Spring Cloud Zuul? 通過前幾章的介紹,我們對於Spring Cloud Netflix 下的核心元件已經瞭解了大半,利用這些元件我們已經可以構建一個簡單的微服務架構系統,比如通過使用Spring Cloud Eureka實現高可用的服務註冊中

服務Zuul

SpringCloud體系最常用閘道器元件為Zuul,閘道器Zuul通過配置檔案約定的介面規則將請求轉發到對應的微服務子專案去處理,這發揮的是其路由功能。除此之外,Zuul的路由功能可以處理前端的跨越

SpringCloud實戰6-Zuul服務

為什麼需要閘道器呢? 我們知道我們要進入一個服務本身,很明顯我們沒有特別好的辦法,直接輸入IP地址+埠號,我們知道這樣的做法很糟糕的,這樣的做法大有問題,首先暴露了我們實體機器的IP地址,別人一看你的IP地址就知道服務部署在哪裡,讓別人很方便的進行攻擊操作。 第二,我