PV(page view)即页面浏览量,通常是衡量一个网络新闻频道或网站甚至一条网络新闻的主要指标。网页浏览数是评价网站流量最常用的指标之一,简称为PV。
案例概述
本案例设计采用四层模式来实现,主要分为前端反向代理、web层、数据库缓存层、数据库层。
前端反向代理层采用主备模式,Web层采用群集模式,数据库缓存层采用主备模式,数据库层采用主备模式
架构拓扑图:实现是正常情况数据流向,虚线是异常情况下的数据流向。
主机名 | IP | 用途 |
---|---|---|
master | 192.168.200.128 | 前端nginx反向代理主机、redis缓存主机、mysql数据主库 |
backup | 192.168.200.129 | 前端nginx反向代理备机、redis缓存备机、mysql数据备库 |
web1 | 192.168.200.130 | tomcat服务器、显示网站 |
web2 | 192.168.200.131 | tomcat服务器、显示网站 |
VIP | 192.168.200.200 |
安装带有nginx rpm软件包的源
# systemctl stop firewalld.service # setenforce 0 # rpm -ivh http://nginx.org/packages/centos/7/noarch/RPMS// # nginx-release-centos-7-0.el7.ngx.noarch.rpm
注意主从配置 3处不同,已经标注
# yum install -y keepalived nginx # vi /etc/keepalived/keepalived.conf //从上修改三个参数 ! Configuration File for keepalived global_defs { route_id NGINX_HA //备份为 NGINX_HB } //下面删除4行 //触发脚本↓↓ vrrp_script nginx { script "/opt/shell/nginx.sh" interval 2 } vrrp_instance VI_1 { state MASTER //备份为BACKUP interface ens33 virtual_router_id 51 priority 100 //备份优先级小于主 advert_int 1 authentication { auth_type PASS auth_pass 1111 } track_script { nginx } virtual_ipaddress { 192.168.200.200 } } # mkdir /opt/shell # vi /opt/shell/nginx.sh //编写触发nginx启动的脚本 #!/bin/bash k=`ps -ef | grep keepalived | grep -v grep | wc -l` if [ $k -gt 0 ];then /bin/systemctl start nginx.service else /bin/systemctl stop nginx.service fi # chmod +x /opt/shell/nginx.sh
主备配置一样
# vi /etc/nginx/nginx.conf //在include 上面一行新增 upstream tomcat_pool { server 192.168.200.130:8080; //web节点服务器 server 192.168.200.131:8080; ip_hash; #会话稳固功能,否则无法通过vip地址登陆 } server { listen 80; server_name 192.168.200.200; #虚拟出的IP location / { proxy_pass http://tomcat_pool; proxy_set_header X-Real-IP $remote_addr; } }
# nginx -t -c /etc/nginx/nginx.conf //测试配置文件语法 # systemctl start keepalived.service //nginx启动会等待一会
数据库的安装配置
# yum install -y mariadb-server mariadb # systemctl start mariadb.service # systemctl enable mariadb.service # netstat -anpt | grep 3306 # mysql_secure_installation //常规安全设置输入( 回车 n n n n y)
导入数据库
# mysql -u root -p < slsaledb-2014-4-10.sql # mysql -uroot -p # show databases; > GRANT all ON slsaledb.* TO 'root'@'%' IDENTIFIED BY 'abc123'; > flush privileges;
安装配置tomcat (节点服务器 两台都要做)
# systemctl stop firewalld.service # setenforce 0 # tar xf apache-tomcat-8.5.23.tar.gz -C /opt # tar xf jdk-8u144-linux-x64.tar.gz -C /opt # cp -rv jdk1.8.0_144/ /usr/local/java # vi /etc/profile //末尾添加下面4行 export JAVA_HOME=/usr/local/java export JRE_HOME=/usr/local/java/jre export PATH=$PATH:/usr/local/java/bin export CLASSPATH=./:/usr/local/java/lib:/usr/local/java/jre/lib # source /etc/profile # java -version //查看版本 # cp -r apache-tomcat-8.5.23 /usr/local/tomcat8 # ln -s /usr/local/tomcat8/bin/startup.sh /usr/bin/tomcatup # ln -s /usr/local/tomcat8/bin/shutdown.sh /usr/bin/tomcatdown # tomcatup //启动tomcat # netstat -anpt | grep 8080 //查看端口是否启动
# vi /usr/local/tomcat8/webapps/ROOT/index.jsp //修改默认网页内容 <h1>Server 130</h1> //节点1的网页内容 <h1>Server 131</h1> //节点2的网页内容
# cd /usr/local/tomcat8/conf/ # vi server.xml //跳到行尾,在Host name下新增 在149行 <Context path="" docBase="SLSaleSystem" reloadable="true" debug="0"></Context> 日志调试信息debug为0表示信息越少,docBase指定访问目录 # tomcatup //启动tomcat
# tar xf SLSaleSystem.tar.gz -C /usr/local/tomcat8/webapps/ # cd /usr/local/tomcat8/webapps/SLSaleSystem/WEB-INF/classes # vi jdbc.properties //修改数据库IP地址是VRRP的虚拟IP,以及授权的用户名root和密码abc123。 # tomcatdown //停止tomcat # tomcatup //启动tomcat
部署redis主从和群集
# yum install -y epel-release # yum install redis -y # cat /etc/redis.conf | grep -v "^#" | grep -v "^$" # vi /etc/redis.conf bind 0.0.0.0 从服务器上266行多如下一行配置 slaveof 192.168.200.128 6379 //主服务器的IP不是虚拟IP # systemctl start redis.service # netstat -anpt | grep 6379 # redis-cli -h 192.168.200.128 -p 6379 //测试连接 192.168.200.128:6379> set name test //设置name 值是test 192.168.200.128:6379> get name //获取name值 "test" # redis-cli -h 192.168.200.129 -p 6379 //登录从,获取值,成功说明主从同步成功 192.168.200.129:6379> get name "test"
配置商城项目中连接redis的参数
# vi /usr/local/tomcat8/webapps/SLSaleSystem/WEB-INF/classes/applicationContext-mybatis.xml 47 <constructor-arg value="192.168.200.200"/> 48 <constructor-arg value="6379"/> # tomcatdown //停止tomcat # tomcatup //启动tomcat
keyspace_hits: 或者 keyspace_misses://关注这个值,命中数和未命中数
# redis-cli -h 192.168.200.200 -p 6379 192.168.200.200:6379> info
只在主服务器是操作
# redis-cli -h 192.168.200.128 info Replication //获取当前服务器的角色
# vi /etc/redis-sentinel.conf (17行 68行 98行) 17 protected-mode no 68 sentinel monitor mymaster 192.168.200.128 6379 1 //1表示1台从 注意:修改 98 sentinel down-after-milliseconds mymaster 3000 //故障切换时间单位是毫秒 # service redis-sentinel start //启动集群 # netstat -anpt | grep 26379 # redis-cli -h 192.168.200.128 -p 26379 info Sentinel //查看集群信息
在主上
# systemctl stop redis.service # redis-cli -h 192.168.200.128 -p 26379 info Sentinel //发现主变成了129
mysql主服务器配置
# vi /etc/my.cnf // [mysqld]下 binlog-ignore-db=mysql,information_schema character_set_server=utf8 log_bin=mysql_bin server_id=1 log_slave_updates=true sync_binlog=1
# systemctl restart mariadb # netstat -anpt | grep 3306 # mysql -u root > show master status; //记录日志文件名称和 位置值 > grant replication slave on *.* to 'rep'@'192.168.200.%' identified by '123456'; > flush privileges;
mysql从服务器配置
# vi /etc/my.cnf / /[mysqld]下 server_id=2 # systemctl restart mariadb # netstat -anpt | grep 3306 # mysql -u root > change master to master_host='192.168.200.128',master_user='rep',master_password='123456',master_log_file='mysql_bin.000001',master_log_pos=245; > start slave; > show slave status/G;