Nginx主备+Tomcat+Redis

环境配置

Windows 10
VMware Workstation 15
redis 			192.168.0.106
VIP				192.168.0.200
Nginx1			192.168.0.40
Nginx2			192.168.0.41
Tomcat01	192.168.0.50
Tomcat02	192.168.0.51
						VIP
					  /     \
				Nginx1		 Nginx2
				   |		    |
			    Tomcat01	 Tomcat02
Nginx安装:
# rpm -ivh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
# yum install nginx -y
Tomcat安装:
# wget http://mirror.bit.edu.cn/apache/tomcat/tomcat-9/v9.0.30/bin/apache-tomcat-9.0.30.tar.gz
# tar -zxvf apache-tomcat-9.0.30.tar.gz
#注意  要下载两个包
# redisson-all-3.9.1.jar   redisson-tomcat-9-3.9.1.jar
修改/编辑Tomcat

修改tomcat配置文件目录下的context.xml

# vi apache-tomcat-9.0.30/conf/context.xml
	  </Context>
			<Manager className="org.redisson.tomcat.RedissonSessionManager"
      configPath="${catalina.base}/conf/redisson.conf" readMode="MEMORY" updateMode="DEFAULT"/> 
      </Context>

新建文件到tomcat配置文件目录下

# vi apache-tomcat-9.0.30/conf/redisson.conf
{
  "singleServerConfig":{
  "idleConnectionTimeout":10000,
  "pingTimeout":1000,
  "connectTimeout":10000,
  "timeout":3000,
  "retryAttempts":3,
  "retryInterval":1500,
  "password":null,
  "subscriptionsPerConnection":5,
  "clientName":null,
  "address": "redis://192.168.0.50:6379",
  "subscriptionConnectionMinimumIdleSize":1,
  "subscriptionConnectionPoolSize":50,
  "connectionMinimumIdleSize":32,
  "connectionPoolSize":64,
  "database":0,
  "dnsMonitoringInterval":5000
   },
"threads":0,
"nettyThreads":0,
"codec":{
  	"class":"org.redisson.codec.JsonJacksonCodec"
 },
"transportMode":"NIO"
}

注意:
第一个tomcat配置中address配置为192.168.0.50
第二个tomcat配置中address配置为192.168.0.51
切换到Tomcat的目录下的webapps下:

# rm -rf * 
# 将事先准备好的app放到该目录,改名为ROOT.war,运行一遍tomcat

# ../../startup.sh  # 这一步将自动解压ROOT.war
# 更改index.jsp中CustomString下一行的为Tomcat*(在哪台服务器上配置就改为tomcat相应序列)
安装keepalived
# yum install keepalived -y
配置keepalived和写检测nginx的脚本

Nginx1配置:

# vi /etc/keepalived/keepalived.conf
vrrp_script chech_nginx_alive {
script "/etc/keepalived/check_nginx_alive.sh"
interval 2
weight -10
}
global_defs {
	notification_email {
	}
router_id LVS_DEVEL_100 #每个设备配置不同id
}

vrrp_instance VI_1 {
	state MASTER  
	interface ens33  #所用端口
	virtual_router_id 50  #这个主备相同
	priority 120	#vrrp优先级,数值越大,优先级越大
    advert_int 1
    authentication {   # 安全验证
        auth_type PASS
        auth_pass password
    }
    virtual_ipaddress {  #虚拟IP
        192.168.0.200
    }
    track_script {
        check_nginx_alive  #vrrp脚本,并不是脚本文件名
    }
}

virtual_server 192.168.0.200 80 {
    delay_loop 6
    lb_algo rr
    lb_kind DR
    nat_mask 255.255.255.0
    protocol TCP
    real_server 192.168.0.50 80 { #真实服务器ip 也就是上面的nginx01 
        weight 1
        TCP_CHECK {
            connect_timeout 3
            nb_get_retry 3
            retry 3
            delay_before_retry 3
            connect_port 80
        }
    }
}

Nginx2配置:

vrrp_script chech_nginx_alive {
   script "/etc/keepalived/check_nginx_alive.sh"
   interval 2
   weight -10
}

global_defs {
    notification_email {
    }
    router_id LVS_DEVEL_101
}

vrrp_instance VI_1 {
    state BACKUP
    interface ens33
    virtual_router_id 50
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass password
    }
    virtual_ipaddress {
        192.168.0.200
    }
    track_script {
        check_nginx_alive
    }
}

virtual_server 192.168.0.200 80 {
    delay_loop 6
    lb_algo rr
    lb_kind DR
    nat_mask 255.255.255.0
    protocol TCP
    real_server 192.168.0.51 80 {
        weight 1
        TCP_CHECK {
            connect_timeout 3
            nb_get_retry 3
            retry 3
            delay_before_retry 3
            connect_port 80
        }
    }
}

两个Nginx服务器都要配置的

# vi /etc/keepalived/check_nginx_alive.sh
#!/bin/bash
if [ "$(ps -ef | grep "nginx: master process"| grep -v grep )" == "" ]
    then
        service keepalived stop
        service nginx start
        sleep 5
        if [ "$(ps -ef | grep "nginx: master process"| grep -v grep )" != "" ]
            then
               service keepalived start
        fi
fi

#编辑nginx.conf 在include /etc/nginx/conf.d/*.conf;上一行添加 以下内容
# vi /etc/nginx/nginx.conf 
upstream www.run-monkey.com {
    server 192.168.0.40:8080 weight=1;
    server 192.168.0.41:8080 weight=1;
}
#编辑/etc/nginx/conf.d/default.conf 将没注释的location改为以下内容
# vi /etc/nginx/conf.d/default.conf 
    location / {
    	#root   /usr/share/nginx/html;
    	#index  index.html index.htm;
    	proxy_pass http://www.run-monkey.com; #这儿的域名是上面的upstream名称
 	}

Redis

直接安装到Windows上启动就行

写在最后

错误可能是有的,当成长的绊脚石吧。当然我成功了 哈哈 不过可以留言一起讨论讨论 有些坑就慢慢填。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值