kong Api網關使用(搭配konga)
搭建docker運行環境:
docker-compose :參考https://github.com/Kong/docker-kong/blob/master/compose/docker-compose.yml
可以去dockerHub看部署方式: https://hub.docker.com/_/kong
我這裏由於一些原因,寫成了bash命令啓動模式(使用了hos模式,沒有去掉暴露端口)
#!/bin/bash
docker run -d --name kong-database \
-p 5432:5432 \
-e "POSTGRES_USER=kong" \
-e "POSTGRES_DB=kong" \
postgres:9.6
sleep 10
docker run --rm \
--link kong-database:kong-database \
-e "KONG_DATABASE=postgres" \
-e "KONG_PG_HOST=kong-database" \
-e "KONG_CASSANDRA_CONTACT_POINTS=kong-database" \
kong kong migrations bootstrap
docker run -d --name kong \
-e "KONG_DATABASE=postgres" \
-e "KONG_PG_HOST=127.0.0.1" \
-e "KONG_CASSANDRA_CONTACT_POINTS=127.0.0.1" \
-e "KONG_PROXY_ACCESS_LOG=/dev/stdout" \
-e "KONG_ADMIN_ACCESS_LOG=/dev/stdout" \
-e "KONG_PROXY_ERROR_LOG=/dev/stderr" \
-e "KONG_ADMIN_ERROR_LOG=/dev/stderr" \
-e "KONG_ADMIN_LISTEN=0.0.0.0:8001, 0.0.0.0:8444 ssl" \
-p 8000:8000 \
-p 8443:8443 \
-p 8001:8001 \
-p 8444:8444 \
--volume "/home/luslin/flume/:/home/kong/" \
--network=host \
kong
docker run -d -p 1337:1337 \
--name konga \
-e "NODE_ENV=production" \
-e "TOKEN_SECRET=asd12386" \
--network=host \
pantsel/konga
sleep 10 原因是等postgres能對外提供服務
ui使用的是konga
關於kong 與 nginx 配置的關係可以參考:https://www.cnblogs.com/duanxz/p/9776316.html
打開127.0.0.1:1337, 註冊用戶
寫kong admin 的監控鏈接8001端口
在servies中添加
這裏的host
一個典型的 Nginx 配置
upstream helloUpstream {
server localhost:3000 weight=100;
}
server {
listen 80;
location /hello {
proxy_pass http://host; 就是這裏的host
}
}
點擊剛剛創建的service添加router
這裏的router,記得每個輸入要回車確認
一個典型的 Nginx 配置
upstream helloUpstream {
server localhost:3000 weight=100;
}
server {
listen 80;
location /hello { 相當於這裏的hello
proxy_pass http://host; 就是這裏的host
}
}
host與path作用類似,只是訪問8000端口的方式不同。在host中添加的需要在訪問的頭文件中添加Host:touyan
在path中添加的,需要在路徑最前面填上/touyan 然後是你要訪問的路徑。
比如一個訪問
POST http://127.0.0.1:8000/api/user/login header 中要加入 Host:touyan
或者 在path中寫入 /touyan 去掉host
POST http://127.0.0.1:8000/touyan/api/user/login
8000端口是kong 監聽代理鏈接的端口