使用skipper 扩展fabio 的路由&&http proxy 功能
skipper 具有强大的http 路由功能,fabio 是一个方便的基于consul 的负载均衡软件,我们可以方便的使用skipper 路由功能进行fabio的扩展,使用registrator 进行服务注册
环境准备
- docker-compose
version: "3"
services:
fabio:
image: fabiolb/fabio
ports:
- "9999:9999"
- "9998:9998"
volumes:
- "./fabio.properties:/etc/fabio/fabio.properties"
consul:
image: consul
command: agent -config-file=/usr/local/etc/consul/server_agent.json
volumes:
- "./consul.json://usr/local/etc/consul/server_agent.json"
ports:
- "8500:8500"
web:
image: nginx
ports:
- "80:80"
environment:
- SERVICE_TAGS=urlprefix-/
- SERVICE_80_CHECK_HTTP=/
- SERVICE_80_NAME=web-app
- SERVICE_CHECK_INTERVAL=10s
- SERVICE_CHECK_TIMEOUT=5s
ip:
build:
context: ./
dockerfile: Dockerfile-ip
image: dalongrong/openresty-ip
skipper2:
image: dalongrong/skipper
environment:
- SERVICE_TAGS=urlprefix-/ip
- SERVICE_9090_NAME=skipper
- SERVICE_9090_CHECK_HTTP=/
- SERVICE_CHECK_INTERVAL=10s
- SERVICE_CHECK_TIMEOUT=5s
ports:
- "9090:9090"
volumes:
- "./router.eskip:/router.eskip"
command: skipper -enable-ratelimits -enable-prometheus-metrics -routes-file /router.eskip
registor:
image: gliderlabs/registrator:latest
command: consul://consul:8500
volumes:
- "/var/run/docker.sock:/tmp/docker.sock"
- 说明
对于skipper 的扩展使用的是nginx 重写,同时注册skipper到fabio中
ip 服务的dockerfile && nginx 配置
Dockerfile-ip
FROM openresty/openresty:alpine
COPY nginx.conf usr/local/openresty/nginx/conf/
EXPOSE 80
nginx.conf
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
gzip on;
real_ip_header X-Forwarded-For;
real_ip_recursive on;
server {
listen 80;
server_name localhost;
charset utf-8;
location / {
rewrite ^/ip(.*) $1 break;
proxy_ignore_client_abort on;
client_body_buffer_size 10M;
client_max_body_size 10G;
proxy_buffers 1024 4k;
proxy_read_timeout 300;
proxy_pass http://localhost:88;
}
location /alert {
default_type text/html;
content_by_lua_block{
ngx.say([[<script>alert("error")</script>]])
}
}
location /ip2 {
default_type text/html;
content_by_lua_block{
ngx.say(ngx.var.remote_addr)
}
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
server {
listen 88;
server_name localhost;
charset utf-8;
location / {
index index.html index.htm;
}
location /alert {
default_type text/html;
content_by_lua_block{
ngx.say([[<script>alert("error")</script>]])
}
}
location /ip2 {
default_type text/html;
content_by_lua_block{
ngx.say(ngx.var.remote_addr.."from upstream")
}
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
- fabio 配置文件
fabio.properties
registry.consul.addr = consul:8500
- skipper router 配置
hello: Path("/ip/*")->compress("text/html")-> corsOrigin()->setResponseHeader("TOKEN","dalongdemo")->responseCookie("test-session", "abc", 31536000)->
setRequestHeader("TOKEN","dalongdemo")-> "http://ip";
root: Path("/") -> status(200) -> <shunt>;
启动&&测试
- 启动
docker-compose up -d
- consul 效果
- fabio router 注册
- 访问
http://localhost:9999 // web nginx index page
skipper 添加的header
后端路由接口