集群技术

负载均衡器

session除了可以存储在redis/memcached还可存以数据库,cookies等

LVS

Nginx

是一个高性能的HTTP(OSI第7层)和反向代理服务器,也是一个IMAP/POP3/SMTP服务器。在1.9.13版本后,Nginx已经支持端口转发(OSI第4层),注意版本一定要大于1.9.1,编译的时候需要–with-stream这个模块支持
Nginx可以作为反向代理进行负载均衡的实现

nginx支持的负载均衡调度算法方式如下:
轮询 默认方式 每个请求按时间顺序分配到不同后端服务器,如果某个后端服务器宕机,能自动剔除掉。
weight 权重方式 权重数据越大,被分配到请求的几率越大
ip_hash 依据ip分配方式 每个请求按照发起客户端ip的hash结果进行匹配,这样的算法每一个固定的ip地址的客户端总会访问到同一个后端服务器,这也在一定程度上解决了集群部署环境下session共享的问题。
least_conn 最少连接方式 这有个前提,就是每个请求所占用的后端时间要差不多,如果有些请求占用的时间很长,会导致其所在的后端负载较高。在这种场景下,把请求转发给连接数较少的后端,能够达到更好的负载均衡效果,这就是least_conn算法。least_conn算法很简单,首选遍历后端集群,比较每个后端的conns/weight,选取该值最小的后端。如果有多个后端的conns/weight值同为最小的,那么对它们采用加权轮询算法。
fair(第三方) 响应时间方式 响应时间短,处理效率高的服务器分配到请求的概率高,响应时间长,处理效率低的服务器分配到的请求少 需要安装upstream_fair模块
url_hash(第三方) 依据URL分配方式 按照访问的url的hash结果分配请求,每个请求的url会指向后端固定的某个服务器,可以在nginx作为静态服务器的情况下提高缓存效率。同样要注意Nginx默认不支持这种调度算法,要使用的话需要安装nginx的hash软件包。

路径匹配转发:location 根据规则转发到不同地址

地址重定向:rewrite
rewrite 最后一项flag参数:
标记符号 说明
last 本条规则匹配完成后继续向下匹配新的location URI规则
break 本条规则匹配完成后终止,不在匹配任何规则
redirect 返回302临时重定向
permanent 返回301永久重定向

负载均衡配置demo

1
2
3
4
5
6
7
8
9
10
11
12
13
#定义集群
upstream demo {
server localhost:1111;
server localhost:2222;
}
server {
listen 8080;
server_name localhost;
location / {
proxy_pass http://demo;
}
}

location的root 用法
root与alias主要区别在于nginx如何解释location后面的uri,这会使两者分别以不同的方式将请求映射到服务器文件上。alias会把location后面配置的路径丢弃掉,把当前匹配到的目录指向到指定的目录。

1
2
3
location ^~ /infotech/ {
root /www/root/html/;
}

如果一个请求的URI是/infotech/a.html时,web服务器将会返回服务器上的/www/root/html/infotech/a.html的文件。

1
2
3
location ^~ /infotech/ {
alias /www/root/html/;
}

如果一个请求的URI是/infotech/a.html时,web服务器将会返回服务器上的/www/root/html/a.html的文件。使用alias时,目录名后面一定要加”/“。
root的处理结果是:root路径+location路径
alias的处理结果是:使用alias路径替换location路径

location的root 用法

1
2
3
4
location / {
root /www/root/html/;
index index.html index.php;
}

a “/” 请求被转换成 “/index.html” or “/index.php” 如果存在的话

nginx + Zookeeper + dubbo 中,nginx可以负责请求转发。

安装

xiaoz写好的一键脚本安装Nginx,省时、省力,直接执行下面的命令即可

1
2
3
4
5
6
#执行下面的命令,根据提示完成安装
wget https://raw.githubusercontent.com/helloxz/nginx-cdn/master/nginx.sh && bash nginx.sh
#安装完成后执行下面的命令让环境变量生效
source /etc/profile
#执行下面的命令查看nginx信息
nginx -V

配置

端口转发
在nginx.conf添加如下配置,并使用nginx -s reload重载nginx使其生效,同时注意防火墙/安全组放行对应的端口。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
stream {
#将12345端口转发到192.168.1.23的3306端口
server {
listen 12345; #源端口
proxy_connect_timeout 5s; #连接超时时间
proxy_timeout 20s; #超时时间
proxy_pass 192.168.1.23:3306; #填写转发目标的IP及端口号
}
#将udp 53端口转发到192.168.1.23 53端口
server {
listen 53 udp reuseport; #reuseport的意思:内核支持同一个端口可以有多个socket同时进行监听而不报错误,主要好处是防负载不均衡
proxy_timeout 20s;
proxy_pass 192.168.1.23:53;
}
#ipv4转发到ipv6
server {
listen 9135;
proxy_connect_timeout 10s;
proxy_timeout 30s;
proxy_pass [2607:fcd0:107:3cc::1]:9135;
}
}

Tomcat集群方案

Tomcat内部实现集群(不好对session进行管理,小集群适用)

  1. 通过组播消息实现,修改tomcat的server.xml
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    <Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"  
    channelSendOptions="8">
    <Manager className="org.apache.catalina.ha.session.DeltaManager"
    expireSessionsOnShutdown="false"
    notifyListenersOnReplication="true"/>
    <Channel className="org.apache.catalina.tribes.group.GroupChannel">
    <Membership className="org.apache.catalina.tribes.membership.McastService"
    address="228.0.0.4"
    port="45564"
    frequency="500"
    dropTime="3000"/>
    <Receiver className="org.apache.catalina.tribes.transport.nio.NioReceiver"
    address="192.168.1.245"
    port="4000"
    autoBind="100"
    selectorTimeout="5000"
    maxThreads="6"/>
    <Sender className="org.apache.catalina.tribes.transport.ReplicationTransmitter">
    <Transport className="org.apache.catalina.tribes.transport.nio.PooledParallelSender"/>
    </Sender>
    <Interceptor className="org.apache.catalina.tribes.group.interceptors.TcpFailureDetector"/>
    <Interceptor className="org.apache.catalina.tribes.group.interceptors.MessageDispatch15Interceptor"/>
    </Channel>
    <Valve className="org.apache.catalina.ha.tcp.ReplicationValve"
    filter=""/>
    <Valve className="org.apache.catalina.ha.session.JvmRouteBinderValve"/>
    <Deployer className="org.apache.catalina.ha.deploy.FarmWarDeployer"
    tempDir="/tmp/war-temp/"
    deployDir="/tmp/war-deploy/"
    watchDir="/tmp/war-listen/"
    watchEnabled="false"/>
    <ClusterListener className="org.apache.catalina.ha.session.JvmRouteSessionIDBinderListener"/>
    <ClusterListener className="org.apache.catalina.ha.session.ClusterSessionListener"/>
    </Cluster>

Tomcat+Redis集群(稳定)

  1. 下载tomcat-redis-session
  2. 安装redis
  3. 对应的jar包放入 tomcat\lib
  4. tomcat\conf\content.xml增加
    1
    2
    3
    4
    5
    6
    7
    <!-- tomcat-redis-session共享 --> 
    <ValveclassName="com.orangefunction.tomcat.redissessions.RedisSessionHandlerValve"/>
    <ManagerclassName="com.orangefunction.tomcat.redissessions.RedisSessionManager"
    host="localhost"
    port="6379"
    database="0"
    maxInactiveInterval="60" />
  5. 部署两个tomcat建立页面测试

其他开源插件

参考