大型网站架构——百万PV网站
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了大型网站架构——百万PV网站相关的知识,希望对你有一定的参考价值。
实验架构:黑线是正常情况数据的流向
红色是异常情况下数据流向
实验环境:
CentOS7-1(master) 192.168.13.128 nginx反向代理(主)、redis缓存处理器(主)、mysql数据库(主)
CentOS7-2(backup) 192.168.13.129 nginx反向代理(备)、redis缓存处理器(备)、mysql数据库(从)
CentOS7-3(tomcat1) 192.168.13.130 tomcat(主)
CentOS7-4(tomcat2) 192.168.13.131 tomcat(备)
1,安装部署nginx和keepalive服务(主备都需安装)
[root@master ~]# systemctl stop firewalld.service ##关闭防火墙
[root@master ~]# setenforce 0
[root@master ~]# rpm -ivh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
##安装nginx源
[root@master ~]# yum install -y keepalived nginx ##下载nginx和keepalive服务
[root@master ~]# vim /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 { ##虚拟ip
192.168.200.200
}
}
//备服务器上修改
router_id NGINX_HB ##主备不一样
state BACKUP ##主服务
priority 90 ## 优先级,备服务器设置 90
//
[root@master ~]# mkdir /opt/shell
[root@master ~]# vim /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
[root@master ~]# chmod +x /opt/shell/nginx.sh
[root@master ~]# vim /etc/nginx/nginx.conf ##在include 上面一行新增
upstream tomcat_pool {
server 192.168.13.130:8080; ##定义后端节点服务器地址池
server 192.168.13.131:8080;
ip_hash; ##会话稳固功能,否则无法通过vip地址登陆
}
server {
listen 80;
server_name 192.168.13.100; ##虚拟出的ip
location / {
proxy_pass http://tomcat_pool;
proxy_set_header X-Real-IP $remote_addr;
}
}
[root@master ~]# nginx -t ##检查nginx配置文件的语法有没有问题
[root@master ~]# systemctl start keepalived.service ##启动keepalive服务
[root@master ~]# ip a ##查看虚拟ip
2,安装部署MySQL(主备都需要安装)
[root@master ~]# yum install -y mariadb-server mariadb
[root@master ~]# systemctl start mariadb.service
[root@master ~]# systemctl enable mariadb.service
[root@master ~]# netstat -natp | grep 3306
tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 38997/mysqld
[root@master ~]# mysql_secure_installation ##初始化数据库
Enter current password for root (enter for none): ##此处直接回车
Set root password? [Y/n] n ##设置密码
Password updated successfully!
Remove anonymous users? [Y/n] n ##是否删除匿名用户,选择不删除
Disallow root login remotely? [Y/n] n ##是否远程连接
Remove test database and access to it? [Y/n] n ##是否删除测试数据库
Reload privilege tables now? [Y/n] y ##是否重新加载
... Success!
[root@master ~]# mysql -uroot -p < slsaledb-2014-4-10.sql ##导入数据库
[root@master ~]# mysql -uroot -p ##进入数据库
MariaDB [(none)]> show databases; ##查看数据库
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| slsaledb | ##导入的数据库
| test |
+--------------------+
5 rows in set (0.00 sec)
MariaDB [(none)]> GRANT all ON slsaledb.* TO ‘root‘@‘%‘ IDENTIFIED BY ‘abc123‘;
##授权slsaledb库下的所有给root用户密码为abc123
MariaDB [(none)]> flush privileges; ##刷新权限
3,部署tomcat服务(两台节点服务器上做)
[root@tomcat01 ~]# systemctl stop firewalld.service
[root@tomcat01 ~]# setenforce 0
[root@tomcat01 ~]# tar zxvf apache-tomcat-8.5.23.tar.gz -C /opt/ ##解压压缩包
[root@tomcat01 ~]# tar zxvf jdk-8u144-linux-x64.tar.gz -C /opt/
[root@tomcat01 ~]# cp -r /opt/jdk1.8.0_144/ /usr/local/java ##复制jdk目录到/usr/local/java
[root@tomcat01 ~]# 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
[root@tomcat01 ~]# source /etc/profile
[root@tomcat01 ~]# java -version ##查看版本
[root@tomcat01 ~]# cp -r /opt/apache-tomcat-8.5.23/ /usr/local/tomcat8 ##tomcat目录
[root@tomcat01 ~]# ln -s /usr/local/tomcat8/bin/startup.sh /usr/bin/tomcatup ##建立软连接
[root@tomcat01 ~]# ln -s /usr/local/tomcat8/bin/shutdown.sh /usr/bin/tomcatdown
[root@tomcat01 ~]# tomcatup ##开启tomcat
[root@tomcat01 ~]# netstat -natp | grep 8080 ##查看tomcat端口号
tcp6 0 0 :::8080 :::* LISTEN 37752/java
[root@tomcat01 ~]# vim /usr/local/tomcat8/webapps/ROOT/index.jsp //修改默认网页内容
<h1>server 130</h1> //节点1的网页内容
<h1>server 131</h1> //节点2的网页内容
[root@tomcat01 ROOT]# cd /usr/local/tomcat8/conf/
[root@tomcat01 conf]# vim server.xml ##修改tomcat配置文件
148 <Host name="localhost" appBase="webapps"
149 unpackWARs="true" autoDeploy="true">
150 <Context path="" docBase="SLSaleSystem" reloadable="true" debug=" 0"></Context>
##日志调试信息debug为0表示信息越少,docBase指定访问目录
[root@tomcat01 conf]# tomcatup ##启动tomcat服务
4,搭建商城网站
[root@tomcat01 ~]# tar zxvf SLSaleSystem.tar.gz -C /usr/local/tomcat8/webapps/
[root@tomcat01 ~]# cd /usr/local/tomcat8/webapps/SLSaleSystem/WEB-INF/classes/
[root@tomcat01 classes]# vim jdbc.properties
driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql://192.168.13.100:3306/slsaledb?useUnicode=true&characterEncoding=UTF-8
##修改地址为虚拟ip
uname=root
password=abc123 ##授权的用户名和密码
minIdle=10
maxIdle=50
initialSize=5
maxActive=100
maxWait=100
removeAbandonedTimeout=180
removeAbandoned=true
[root@tomcat01 classes]# tomcatdown ##关闭开启tomca服务
[root@tomcat01 classes]# tomcatup
5,部署redis主从和群集(在主备上配置)
[root@master ~]# yum install -y epel-release
[root@master ~]# yum install redis -y
[root@master ~]# cat /etc/redis.conf | grep -v "^#" | grep -v "^$"
[root@master ~]# vim /etc/redis.conf
bind 0.0.0.0
##从服务器上266行多如下一行配置
slaveof 192.168.200.128 6379 ##主服务器的IP不是虚拟IP
[root@master ~]# systemctl start redis.service ##开启redis服务
[root@master ~]# netstat -natp | grep 6379
[root@master ~]# redis-cli -h 192.168.13.128 -p 6379 ##在主上设置键值
192.168.13.128:6379> set name test
OK
192.168.13.128:6379> get name
"test"
[root@master ~]# redis-cli -h 192.168.13.129 -p 6379 ##在从上查看,显示主从同步
192.168.13.129:6379> get name
"test"
6,在tomcat服务器上配置(配置项目商城中连接redis的参数)
[root@tomcat01 classes]# vim /usr/local/tomcat8/webapps/SLSaleSystem/WEB-INF/classes/applicationContext-mybatis.xml
47 <constructor-arg value="192.168.13.100"/> ##虚拟地址
48 <constructor-arg value="6379"/>
[root@tomcat01 classes]# tomcatdown ##关闭开启tomca服务
[root@tomcat01 classes]# tomcatup
7,在主备服务器上配置(测试Redis缓存效果)
//登录商城,然后反复点击需要数据库参与的操作页面,再回来检查keyspace_hits或者keyspace_misses: 值变化
//keyspace_hits: 或者 keyspace_misses://关注这个值,命中数和未命中数
[root@master ~]# redis-cli -h 192.168.13.100 -p 6379
192.168.13.100:6379> info
expired_keys:0
evicted_keys:0
keyspace_hits:2 ##命中,说明redis参与工作了
keyspace_misses:0
pubsub_channels:0
8,配置redis集群主从切换(只在主服务器上操作)
[root@master ~]# redis-cli -h 192.168.13.128 info Replication ##获取当前服务器的角色
# Replication
role:master
connected_slaves:1
slave0:ip=192.168.13.129,port=6379,state=online,offset=10800,lag=0
master_repl_offset:10800
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:2
repl_backlog_histlen:10799
[root@master ~]# vim /etc/redis-sentinel.conf ##修改redis哨兵配置文件
17 protected-mode no
69 sentinel monitor mymaster 192.168.200.128 6379 1 ##1表示1台从 注意:修改
98 sentinel down-after-milliseconds mymaster 3000 ##故障切换时间单位是毫秒
[root@master ~]# service redis-sentinel start ##启动集群
[root@master ~]# netstat -anpt | grep 26379 ##查看端口号
[root@master ~]# redis-cli -h 192.168.13.128 -p 26379 info Sentinel
# Sentinel
sentinel_masters:1
sentinel_tilt:0
sentinel_running_scripts:0
sentinel_scripts_queue_length:0
sentinel_simulate_failure_flags:0
master0:name=mymaster,status=ok,address=192.168.13.128:6379,slaves=1,sentinels=1
//验证主从切换//
[root@master ~]# systemctl stop redis.service ##关闭主的redis服务
[root@master ~]# redis-cli -h 192.168.13.128 -p 26379 info Sentinel
# Sentinel
sentinel_masters:1
sentinel_tilt:0
sentinel_running_scripts:0
sentinel_scripts_queue_length:0
sentinel_simulate_failure_flags:0
master0:name=mymaster,status=ok,address=192.168.13.129:6379,slaves=1,sentinels=1
##此时切换到129备服务器上
[root@master ~]# systemctl start redis.service ##重启开启主的redis服务
9,在主备服务器上配置(配置mysql主从复制)
//主服务器上配置//
[root@master ~]# vim /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
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
[root@master ~]# systemctl restart mariadb.service
[root@master ~]# netstat -natp | grep 3306
[root@master ~]# mysql -uroot
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 ‘rep‘@‘192.168.13.%‘ identified by ‘123456‘;
##授权复制权限
MariaDB [(none)]> flush privileges;
//备服务器上配置//
[root@backup ~]# vim /etc/my.cnf
[mysqld]
binlog-ignore-db=mysql,information_schema
character_set_server=utf8
log_bin=mysql_bin
server_id=2 ##修改id为2
log_slave_updates=true
sync_binlog=1
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
[root@backup ~]# systemctl restart mariadb.service
[root@backup ~]# mysql -uroot
MariaDB [(none)]> change master to master_host=‘192.168.13.128‘,master_user=‘rep‘,master_password=‘123456‘,master_log_file=‘mysql_bin.000001‘,master_log_pos=245;
##复制主服务器上的二进制日志文件及位置
MariaDB [(none)]> start slave; ##开启同步
Query OK, 0 rows affected (0.01 sec)
MariaDB [(none)]> show slave statusG ##查看从同步状态
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
10,关闭master主服务器,验证架构
谢谢阅读!!!
以上是关于大型网站架构——百万PV网站的主要内容,如果未能解决你的问题,请参考以下文章