PV

PV(Page View,页面浏览量)即点击量,通常意义上说PV的多少是衡量一个网络新闻频道或网站甚至一条网络新闻的主要指标。pv的解释是这样的:一个访问者在24小时(0点-23点)内到底看了网站的几个页面。需要注意的是:同一个人浏览网站的同一个页面,不重复计算pv量,点击100次页只算1次。

实验概述

采用四层模式实现,主要分为前端反向代理层、Web层、数据库缓存层和数据库层。前端反向代理层层采用主备模式,Web层采用群集模式,数据库缓存层采用主备模式,数据库层采用主从模式。

 

实验环境表


主机名IP地址用途
CentOS7-1(master)192.168.10.10nginx反向代理(主)、redis缓存处理器(主)、mysql数据库(主)
CentOS7-2(backup)192.168.10.11nginx反向代理(备)、redis缓存处理器(备)、mysql数据库(从)
CentOS7-3(tomcat1)192.168.10.12tomcat(主)
CentOS7-4(tomcat2)192.168.10.13tomcat(备)

 

实验步骤

安装部署nginx、keepalived服务(主从服务器7-1,7-2)

关闭防火墙及SELinux

systemctl stop firewalld.service
setenforce 0

安装带有nginx的rpm软件包的源

rpm -ivh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm

使用yum仓库安装nginx、keepalived服务

yum install -y keepalived nginx

修改keepalived配置文件

global_defs {

router_id NGINX_A                  #修改为不同名称,我设置为NGINX_B

}

vrrp_script nginx {                    #定义脚本
    script "/opt/shell/nginx.sh"      #添加创建脚本
    interval 2                                #每2s自动执行上面的脚本
}

vrrp_instance VI_1 {
    state MASTER                  #主服务器master,从服务器为backup
    interface ens33                  #改为自己的网卡名
    virtual_router_id 51     #从服务器ID要不同,我设置为52
    priority 100                  #从服务器低于主服务器优先级,我设置为90
}

track_script {                    #调用上面的脚本

nginx

}

virtual_ipaddress {
    193.168.10.150             #定义虚拟IP
    }
}

#下面多余的配置文件可以删除

这里方便修改可以吧主服务器的配置文件直接复制到从服务器,然后在做微调即可。

scp /etc/keepalived/keepalived.conf root@192.168.10.11:/etc/keepalived/

NGINX的启动脚本

mkdir /opt/shell
vim /opt/shell/nginx.sh

#!/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

nginx返向代理配置

vim /etc/nginx/nginx.conf

upstream tomcat_pool{
        server 192.168.10.12:8080;                    #两台tomcat服务器的IP地址
        server 192.168.10.13:8080;
        ip_hash;               #会话稳固功能
}
    server {
        listen 80;
        server_name 192.168.10.150;       #虚拟IP
        location / {
                        proxy_pass http://tomcat_pool;
                        proxy_set_header X-Real-IP $remote_addr;
                }
    }

测试一下语法

[root@localhost ~]# nginx -t -c /etc/nginx/nginx.conf
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

开启keepalived服务,检测nginx是否随之启动

systemctl start keepalived.service

netstat -ntap | grep nginx
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      75256/nginx: master

 

部署tomcat服务(两台web节点服务器7-3,7-4)
搭建java环境,安装tomcat

tar xf apache-tomcat-8.5.23.tar.gz -C /usr/local/
tar xf jdk-8u144-linux-x64.tar.gz -C /usr/local/

重命名

cd /usr/local/
mv jdk1.8.0_144/ java
mv apache-tomcat-8.5.23/ tomcat8

添加环境变量

vim /etc/profile

行尾添加
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环境是否安装成功

# java -version
openjdk version "1.8.0_161"
OpenJDK Runtime Environment (build 1.8.0_161-b14)
OpenJDK 64-Bit Server VM (build 25.161-b14, mixed mode)

apached的优化与软链接的创建

ln -s /usr/local/tomcat8/bin/startup.sh /usr/bin/tomcatup
ln -s /usr/local/tomcat8/bin/shutdown.sh /usr/bin/tomcatdown

这时候已经可以用VIP访问站点了。

11016233004


 

修改server.xml配置文件

vim /usr/local/tomcat8/conf/server.xml

148行

<Context path="" docBase="SLSaleSystem" reloadable="true" debug="0"></Context>

 

mysql数据库的安装部署(主从服务器7-1,7-2)

为了安装更轻便,这里使用mariadb代替MySQL数据库

yum install -y mariadb-server mariadb

systemctl start mariadb.service
systemctl enable mariadb.service

mariadb的常规安全设置


[root@localhost shell]# mysql_secure_installation      #开始常规安全设置

NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
      SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!

In order to log into MariaDB to secure it, we'll need the current
password for the root user.  If you've just installed MariaDB, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.

Enter current password for root (enter for none):    #默认空密码,直接回车
OK, successfully used password, moving on...

Setting the root password ensures that nobody can log into the MariaDB
root user without the proper authorisation.

Set root password? [Y/n] y      #创建新密码
New password:
Re-enter new password:
Password updated successfully!
Reloading privilege tables..
... Success!


By default, a MariaDB installation has an anonymous user, allowing anyone
to log into MariaDB without having to have a user account created for
them.  This is intended only for testing, and to make the installation
go a bit smoother.  You should remove them before moving into a
production environment.

Remove anonymous users? [Y/n] n   #不删除匿名用户
... skipping.

Normally, root should only be allowed to connect from 'localhost'.  This
ensures that someone cannot guess at the root password from the network.

Disallow root login remotely? [Y/n] n      #远程ROOT登陆
... skipping.

By default, MariaDB comes with a database named 'test' that anyone can
access.  This is also intended only for testing, and should be removed
before moving into a production environment.

Remove test database and access to it? [Y/n] n    #删除测试数据库
... skipping.

Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.

Reload privilege tables now? [Y/n] y     #重新加载权限表
... Success!

Cleaning up...

All done!  If you've completed all of the above steps, your MariaDB
installation should now be secure.

Thanks for using MariaDB!

导入会员商城数据库至mariadb数据库

mysql -u root -p < slsaledb.sql

确认mysql数据并授权

GRANT all ON slsaledb.* TO 'root'@'%' IDENTIFIED BY '1';

FLUSH PRIVILEGES;

节点服务器上部署商城项目(7-3,7-4)

解压会员商城的软件包至tomcat首页站点目录下,并修改配置文件

tar xf SLSaleSystem.tar.gz -C /usr/local/tomcat8/webapps/

vim /usr/local/tomcat8/webapps/SLSaleSystem/WEB-INF/classes/jdbc.properties    #修改如下

driverClassName=com.mysql.jdbc.Driver
url=jdbc\:mysql\://192.168.10.150\:3306/slsaledb?useUnicode\=true&characterEncoding\=UTF-8
uname=root
password=1
minIdle=10
maxIdle=50
initialSize=5
maxActive=100
maxWait=100
removeAbandonedTimeout=180
removeAbandoned=true

重启tomcat服务,登录网页测试


11017000325

 

安装部署redis数据库缓存服务(主从服务器7-1,7-2)

yum仓库安装redis服务

yum install epel-release -y

yum -y install redis

修改redis配置文件

vim /etc/redis.conf

61行
bind 0.0.0.0        #修改0.0.0.0   监听所有地址
 
从服务器要多修改一条
266行,添加

slaveof 192.168.10.10 6379

开启redis服务,进行主从同步测试

systemctl start redis.service

redis-cli -h 192.168.10.10 -p 6379            #登录主redis服务
set name test      #设置name的值为test
getname           #获取name值
 
redis-cli -h 192.168.10.11 -p 6379      #登录从redis服务
get name      #获取name值

配置商城项目中连接redis的参数(两个节点服务器中 )

vim /usr/local/tomcat8/webapps/SLSaleSystem/WEB-INF/classes/applicationContext-mybatis.xml

47行左右
<constructor-arg value="192.168.10.150"/>   #VIP地址

redis主备(主redis上配置)

vim /etc/redis-sentinel.conf

17行 protected-mode no
68行 sentinel monitor mymaster 192.168.10.10 6379 1 //表示1台从服务
98行 sentinel down-after-milliseconds mymaster 2000 //故障切换时间单位是毫秒

开启集群服务

systemctl start redis-sentinel.service

redis-cli -h 192.168.10.10 -p 26379 info Sentinel #查看集群信息

mysql主从同步部署

修改配置文件

vim /etc/my.cnf

[mysqld]下
binlog-ignore-db=mysql,information_schema
character_set_server=utf8
log_bin=mysql_bin
server_id=1                   #主从需不一样,从我写了2
log_slave_updates=true
sync_binlog=1

systemctl restart mariadb.service

查看主服务器的日志记录文件和偏移量

MariaDB [(none)]> show master status;
+------------------+----------+--------------+--------------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB         |
+------------------+----------+--------------+--------------------------+
| mysql_bin.000001 |      245 |              | mysql,information_schema |
+------------------+----------+--------------+--------------------------+
1 row in set (0.00 sec)

MariaDB [(none)]> grant replication slave on *.* to 'moo'@'192.168.10.%' identified by '1';
Query OK, 0 rows affected (0.01 sec)

从服务器

MariaDB [(none)]> change master to master_host='192.168.10.10',master_user='moo',master_password='1',master_log_file='mysql_bin.000001',master_log_pos=245;
Query OK, 0 rows affected (0.01 sec)

MariaDB [(none)]> start slave;
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> show slave status\G;

Slave_IO_Running: Yes

Slave_SQL_Running: Yes     #确定这两项为yes

在主服务器关闭keepalived,仍然能访问商城

 

11017004943

百万pv商城网站完成!