高可用实践之一 负载均衡

来源:互联网 发布:双十一淘宝热销产品 编辑:程序博客网 时间:2024/06/06 11:05

概述

  • (1) 客户端层,这一层是浏览器或者APP,第一步先访问DNS-server,由域名拿到nginx的外网IP
  • (2)负载均衡层,nginx是整个服务端的入口,负责反向代理与负载均衡工作
  • (3)站点层,web-server层,典型的是tomcat或者apache
  • (4)服务层,service层,典型的是dubbo或者thrift等提供RPC调用的后端服务
  • (5)数据层,包含cache和db,典型的是主从复制读写分离的db架构

Nginx 负载均衡配置

  • 假设我有三台服务器
  • 192.168.202.3 //做nginx负载均衡
  • 192.168.202.4 //做服务
  • 192.168.202.5 //做服务

那么nginx 在192.168.202.3上的配置如下

   upstream slb_test {       #ip_hash;  #根据ip哈希,进行转发        server 192.168.202.4;       server 192.168.202.5;    }    server{        listen 80 default_server;        server_name www.lnmp.org;        index index.html index.htm index.php;        root  /home/wwwroot/default;        location / {           proxy_pass  http://slb_tuzuu;         }        access_log  /home/wwwlogs/access.log;    }     备注: 这里如果做所有的请求转发的就按照以上的配置,去掉php的配置

nginx 在192.168.202.4 和 5 上的配置和nginx 正常一样

server{        listen 80 default_server;        server_name www.lnmp.org;        index index.html index.htm index.php;        root  /home/wwwroot/default;        #添加php配置        include enable-php.conf;        location /nginx_status        {            stub_status on;            access_log   off;        }        #添加静态资源配置        location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$        {            expires      30d;        }        location ~ .*\.(js|css)?$        {            expires      12h;        }        location ~ /\.        {            deny all;        }        access_log  /home/wwwlogs/access.log;}

如果进对于PHP进行负载的话,只需要修改Nginx配置文件中关于php的一部分

把原来的fastcgipass 127.0.0.1:9000;改为 fastcgipass fastcgi;

  location ~ \.php$ {          root    /balance;          include /etc/nginx/fastcgi_params;          #将webserver接收的客户端请求通过fastcgi负载均衡到php5-fpm的池          fastcgi_pass    slb_test;      } 

Session 共享机制

    开启session    session_start();    $_SESSION['views']=1;    使用服务存储session ,然后 根据sessionid从服务里获取$_SESSION

0 0
原创粉丝点击