前言:

微服務(wù)架構(gòu)是現(xiàn)在很火熱的技術(shù)話題,其本質(zhì)是將龐大而復(fù)雜的系統(tǒng),拆分成微小的服務(wù)模塊,使服務(wù)之間實(shí)現(xiàn)解耦,能夠?qū)崿F(xiàn)各模塊的獨(dú)立開(kāi)發(fā)、升級(jí)、部署。大大降低了系統(tǒng)的運(yùn)維及開(kāi)發(fā)難度。
然而由于服務(wù)的拆分,客戶端可能同時(shí)需要和多個(gè)服務(wù)進(jìn)行交互,隨著微服務(wù)規(guī)模的增大,這樣的交互模式在性能和管理上都有很大的不便。那么基于微服務(wù)的客戶端如何能夠更好的去訪問(wèn)這些獨(dú)立的服務(wù)呢,這時(shí)我們就需要一個(gè)統(tǒng)一的入口來(lái)向外提供服務(wù)。這就是我們所說(shuō)的API網(wǎng)關(guān),
API Gateway是一個(gè)在客戶端和服務(wù)之間的中間人,客戶端不用直接訪問(wèn)服務(wù)器,而是通過(guò)API Gateway來(lái)傳遞中間消息。API Gateway能夠?qū)崿F(xiàn)負(fù)載均衡、緩存、訪問(wèn)控制、API 計(jì)費(fèi)監(jiān)控等等功能。下面為網(wǎng)上關(guān)于API Gateway的圖。

Kong 是由Mashape公司開(kāi)發(fā)的一款A(yù)PI Gateway的軟件,Kong是基于nginx開(kāi)發(fā),用來(lái)接收客戶端的API請(qǐng)求,同時(shí)還需要一個(gè)數(shù)據(jù)庫(kù)來(lái)存儲(chǔ)操作數(shù)據(jù)。寫(xiě)這篇文章時(shí)Kong的最新版是0.9.3,其支持?jǐn)?shù)據(jù)庫(kù)為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ù)庫(kù)
kong支持 PostgreSQL 9.4+ 和Cassandra 2.2.x .
如果使用PostgreSQL數(shù)據(jù)庫(kù),請(qǐng)創(chuàng)建用戶和對(duì)應(yīng)的數(shù)據(jù)庫(kù)
$ CREATE USER kong; CREATE DATABASE kong OWNER kong;(3):?jiǎn)?dòng)
$ kong start # Kong is running $ curl 127.0.0.1:8001Kong啟動(dòng)后,會(huì)分別監(jiān)聽(tīng)8000端口和8001端口。8000端口是用來(lái)提供服務(wù),8001是用來(lái)對(duì)API進(jìn)行管理。
docker
(1):啟動(dòng)數(shù)據(jù)庫(kù)
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):?jiǎn)?dòng)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:提供服務(wù)的后端url。
request_path:使用path參數(shù)時(shí),加入該路徑的服務(wù)接口。
request_host 使用這個(gè)參數(shù)時(shí),將加入該host的所有服務(wù)接口:
(2):查詢已經(jīng)添加的API
$curl localhost:8001/apis/(3):訪問(wèn)API
$curl -i -X POST --url http://localhost:8000/ --header 10.100.55.1(4):刪除API
根據(jù)API_NAME or API_ID來(lái)刪除指定api
$curl -i -X DELETE localhost:8001/apis/00f90ca9-cf2d-4830-c842-3b90f6cd08af $curl -i -X DELETE localhost:8001/apis/test1(5):添加實(shí)例
實(shí)例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/'訪問(wèn)接口:
$curl -i -X GET --url http://localhost:8000/hello1/所謂的request_path,就是固定只能訪問(wèn)hello1下的內(nèi)容,如果該host的www目錄下還有hello2、hello3、那么是不能通過(guò)網(wǎng)關(guān)去訪問(wèn)這個(gè)目錄下的內(nèi)容,例如下面是訪問(wèn)不到的,因?yàn)闆](méi)有加入到Kong中。
$curl -i -X GET --url http://localhost:8000/hello2/實(shí)例2 (request_host可以訪問(wèn)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'訪問(wèn)接口:
使用request_host后,該host所有api都加入到Kong中,下面的都能夠通過(guò)網(wǎng)關(guān)訪問(wè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實(shí)例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'訪問(wèn)接口:
$curl -i -X GET --url http://localhost:8000/ --header host:10.100.55.2實(shí)例4 (復(fù)雜url的添加和訪問(wèn))
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'訪問(wèn)接口:
$ curl -i -X GET --url http://localhost:8000/opj/list?serviceId=box&c=nanjing三:創(chuàng)建認(rèn)證
(1)給API配置pulgin認(rèn)證
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 訪問(wèn)正常 Connect Success...2:添加plugin認(rèn)證
$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 訪問(wèn)失敗 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去訪問(wèn)
$curl -i -X GET --url http://localhost:8000/ --header host:10.100.55.1 --header apikey:helloworld 訪問(wèn)成功 Connect Success...通過(guò)上面的兩步,就可以實(shí)現(xiàn)對(duì)API接口的權(quán)限控制。
未完待續(xù)。。。。。。。
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)cdcxhl.cn,海內(nèi)外云服務(wù)器15元起步,三天無(wú)理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國(guó)服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡(jiǎn)單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場(chǎng)景需求。
網(wǎng)站題目:【1】微服務(wù)架構(gòu)-開(kāi)源API網(wǎng)關(guān)Kong的部署與使用-創(chuàng)新互聯(lián)
URL標(biāo)題:http://www.chinadenli.net/article46/deedeg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站收錄、網(wǎng)站設(shè)計(jì)、品牌網(wǎng)站設(shè)計(jì)、動(dòng)態(tài)網(wǎng)站、軟件開(kāi)發(fā)、電子商務(wù)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容