nginx结合consul实现swoft动态负载均衡
http { default_type application/octet-stream; sendfile on; upstream swoft_server { server 172.22.22.110:18306 max_fails=2 fail_timeout=30s; upsync 172.22.22.30:8500/v1/kv/upstream/swoft_server upsync_timeout=1ms upsync_interval=1ms upsync_type=consul strong_dependency=on; # 这是容器中的目录 upsync_dump_path /nginx/conf/swoft_server.conf; include /nginx/conf/swoft_server.conf; } server { listen 80; server_name localhost; root /www; location / { proxy_pass http://swoft_server; } } } 解释: 172.22.22.30:8500/v1/kv/upstreams =》 连接consul的api资源地址 swoole_test =》 相当于我们自己在consul中自定义的key upsync_timeout =》 超时时间6分钟 upsync_interval =》 定时获取信息的时间 upsync_type =》 类型 strong_dependency=on; =》 是否依赖consul运行 upsync_dump_path =》 拉取之后申请配置文件
nginx upsync 动态获取consul中的服务信息 strong_dependency是否依赖consul,获取服务信息后会生成一个配置文件。
搭建过程
构建3台swoft 1台nginx 1台consul
配置nginx的upstream异步访问consul的服务端
consul实现自动注册信息到consul配置文件中
本文由:xiaoshu168.com 作者:xiaoshu发表,转载请注明来源!