前言:
微服務架構(gòu)是現(xiàn)在很火熱的技術(shù)話題,其本質(zhì)是將龐大而復雜的系統(tǒng),拆分成微小的服務模塊,使服務之間實現(xiàn)解耦,能夠?qū)崿F(xiàn)各模塊的獨立開發(fā)、升級、部署。大大降低了系統(tǒng)的運維及開發(fā)難度。
然而由于服務的拆分,客戶端可能同時需要和多個服務進行交互,隨著微服務規(guī)模的增大,這樣的交互模式在性能和管理上都有很大的不便。那么基于微服務的客戶端如何能夠更好的去訪問這些獨立的服務呢,這時我們就需要一個統(tǒng)一的入口來向外提供服務。這就是我們所說的API網(wǎng)關(guān),
API Gateway是一個在客戶端和服務之間的中間人,客戶端不用直接訪問服務器,而是通過API Gateway來傳遞中間消息。API Gateway能夠?qū)崿F(xiàn)負載均衡、緩存、訪問控制、API 計費監(jiān)控等等功能。下面為網(wǎng)上關(guān)于API Gateway的圖。
Kong 是由Mashape公司開發(fā)的一款API Gateway的軟件,Kong是基于nginx開發(fā),用來接收客戶端的API請求,同時還需要一個數(shù)據(jù)庫來存儲操作數(shù)據(jù)。寫這篇文章時Kong的最新版是0.9.3,其支持數(shù)據(jù)庫為PostgreSQL 9.4+ 和Cassandra 2.2.x .
一:安裝
centos
(1):安裝kong
$ sudo yum install epel-release $ sudo yum install kong-0.9.3.*.noarch.rpm --nogpgcheckor
Download kong-0.9.3.el7.noarch.rpm
$ wget kong-0.9.3.el7.noarch.rpm(2):配置數(shù)據(jù)庫
kong支持 PostgreSQL 9.4+ 和Cassandra 2.2.x .
如果使用PostgreSQL數(shù)據(jù)庫,請創(chuàng)建用戶和對應的數(shù)據(jù)庫
$ CREATE USER kong; CREATE DATABASE kong OWNER kong;(3):啟動
$ kong start # Kong is running $ curl 127.0.0.1:8001Kong啟動后,會分別監(jiān)聽8000端口和8001端口。8000端口是用來提供服務,8001是用來對API進行管理。
docker
(1):啟動數(shù)據(jù)庫
cassandra
$ docker run -d --name kong-database \ -p 9042:9042 \ cassandra:2.2OR PostgreSQL
$ docker run -d --name kong-database \ -p 5432:5432 \ -e "POSTGRES_USER=kong" \ -e "POSTGRES_DB=kong" \ postgres:9.4(2):啟動kong
$ docker run -d --name kong \ --link kong-database:kong-database \ -e "KONG_DATABASE=cassandra" \ -e "KONG_CASSANDRA_CONTACT_POINTS=kong-database" \ -e "KONG_PG_HOST=kong-database" \ -p 8000:8000 \ -p 8443:8443 \ -p 8001:8001 \ -p 7946:7946 \ -p 7946:7946/udp \ kong附上docker-compose.yml
kong-database: p_w_picpath: postgres:9.4 ports: - 5432:5432/tcp environment: - POSTGRES_USER=kong - POSTGRES_DB=kong kong: p_w_picpath: kong links: - kong-database:kong-database environment: - KONG_DATABASE=postgres - KONG_CASSANDRA_CONTACT_POINTS=kong-database - KONG_PG_HOST=kong-database ports: - 8000:8000/tcp - 8443:8443/tcp - 8001:8001/tcp - 7946:7946/tcp - 7946:7946/udp二:使用kong
(1):添加api到kong
$ curl -i -X POST \ --url http://localhost:8001/apis/ \ --data 'name=baidu' \ --data 'upstream_url=http://baidu.com/' \ --data 'request_host=baidu.com'--url:8001端口是Kong的管理端口。
upstream_url:提供服務的后端url。
request_path:使用path參數(shù)時,加入該路徑的服務接口。
request_host 使用這個參數(shù)時,將加入該host的所有服務接口:
(2):查詢已經(jīng)添加的API
$curl localhost:8001/apis/(3):訪問API
$curl -i -X POST --url http://localhost:8000/ --header 10.100.55.1(4):刪除API
根據(jù)API_NAME or API_ID來刪除指定api
$curl -i -X DELETE localhost:8001/apis/00f90ca9-cf2d-4830-c842-3b90f6cd08af $curl -i -X DELETE localhost:8001/apis/test1(5):添加實例
實例1 (request_path限制path內(nèi)的接口)
URL:
http://10.100.55.1/hello1/index.html
添加:
$curl -i -X POST --url http://localhost:8001/apis/ \ --data name=test1 \ --data 'upstream_url=http://10.100.55.1' \ --data 'request_path=/hello1/'訪問接口:
$curl -i -X GET --url http://localhost:8000/hello1/所謂的request_path,就是固定只能訪問hello1下的內(nèi)容,如果該host的www目錄下還有hello2、hello3、那么是不能通過網(wǎng)關(guān)去訪問這個目錄下的內(nèi)容,例如下面是訪問不到的,因為沒有加入到Kong中。
$curl -i -X GET --url http://localhost:8000/hello2/實例2 (request_host可以訪問host所有接口)
URL:
http://10.100.55.1
添加:
$ curl -i -X POST --url http://localhost:8001/apis/ \ --data name=test2 \ --data 'upstream_url=http://10.100.55.1' \ --data 'request_host=10.100.55.1'訪問接口:
使用request_host后,該host所有api都加入到Kong中,下面的都能夠通過網(wǎng)關(guān)訪問。
$curl -i -X GET --url http://localhost:8000/hello1 --header host:10.100.55.1 $curl -i -X GET --url http://localhost:8000/hello2 --header host:10.100.55.1實例3 (request_host port:8080)
URL:
http://10.100.55.2:8080
添加:
$curl -i -X POST --url http://localhost:8001/apis/ \ --data name=test3 \ --data 'upstream_url=http://10.100.55.2:8080' \ --data 'request_host=10.100.55.2'訪問接口:
$curl -i -X GET --url http://localhost:8000/ --header host:10.100.55.2實例4 (復雜url的添加和訪問)
URL:
http://10.100.55.3:8000/opj/list?serviceId=box&c=nanjing
添加:
$ curl -i -X POST --url http://localhost:8001/apis/ --data 'name=test4' --data 'upstream_url=http://10.100.55.3:8000/' --data 'request_path=/opj/list'訪問接口:
$ curl -i -X GET --url http://localhost:8000/opj/list?serviceId=box&c=nanjing三:創(chuàng)建認證
(1)給API配置pulgin認證
1:添加api
$curl -i -X POST --url http://localhost:8001/apis/ --data 'name=test5' --data 'upstream_url=http://10.100.55.1/' --data 'request_host=10.100.55.1' $curl -i -X GET --url http://localhost:8000/ --header host:10.100.55.1 訪問正常 Connect Success...2:添加plugin認證
$curl -i -X POST --url http://localhost:8001/apis/test5/plugins/ --data 'name=key-auth' $curl -i -X GET --url http://localhost:8000/ --header host:10.100.55.1 訪問失敗 HTTP/1.1 401 Unauthorized WWW-Authenticate: Key realm="kong" Server: kong/0.9.3 {"message":"No API key found in headers or querystring"}(2)添加用戶
1:創(chuàng)建用戶
$curl -i -X POST --url http://localhost:8001/consumers/ --data "username=heqin" {"username":"heqin","created_at":1477382339000,"id":"8e6273c9-f332-4d68-b74c-73ae9f82f150"}2:給用戶創(chuàng)建key
$curl -i -X POST --url http://localhost:8001/consumers/heqin/key-auth/ --data 'key=helloworld' {"created_at":1477382483000,"consumer_id":"8e6273c9-f332-4d68-b74c-73ae9f82f150","key":"helloworld","id":"62c0d640-b1bd-4f3b-aa6e-ba3adaf8ec38"}3:帶著key去訪問
$curl -i -X GET --url http://localhost:8000/ --header host:10.100.55.1 --header apikey:helloworld 訪問成功 Connect Success...通過上面的兩步,就可以實現(xiàn)對API接口的權(quán)限控制。
未完待續(xù)。。。。。。。
另外有需要云服務器可以了解下創(chuàng)新互聯(lián)cdcxhl.cn,海內(nèi)外云服務器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務器、裸金屬服務器、高防服務器、香港服務器、美國服務器、虛擬主機、免備案服務器”等云主機租用服務以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應用場景需求。
網(wǎng)站題目:【1】微服務架構(gòu)-開源API網(wǎng)關(guān)Kong的部署與使用-創(chuàng)新互聯(lián)
URL標題:http://chinadenli.net/article46/deedeg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站收錄、網(wǎng)站設(shè)計、品牌網(wǎng)站設(shè)計、動態(tài)網(wǎng)站、軟件開發(fā)、電子商務
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容