标签:love rip jdbc coding cat ima 默认 php _id
简介:本项目案例结合SVN、LNMP和MySQL三种环境,部署一个社交网站,本社交网站采用PHP语言开发,搭建SVN服务器进行版本控制和集中管理PHP程序员开发的代码,以Nginx作为前端服务器,通过fastcgi协议访问后端的PHP服务器,调用PHP页面;为保证数据安全,搭建MySQL主从复制环境存储用户重要数据;另外搭建MFS分布式文件系统来存储用户照片。
1.SVN服务器:简单一点SVN就是用于多个人共同开发同一个项目,共用资源的目的
2.MFS分布式文件系统,比NFS分布式文件系统更加强大,其他分布式式文件系统还有HDFS分布式文件系统和fastDFS分布式文件系统等
3.MySQL环境,此案列环境限于本人机器设备配置问题,只配置了mysql主从复制,如果是真实环境中,还可以配置双击热备,借助amoeba环境实现读写分离,负载均衡等功能,大型网络环境还可以配置MySQL集群
采用四层模式实现,主要分为前端反向代理层、Web层、数据库缓存层和数据库层。前端反向代理层层采用主备模式,Web层采用群集模式,数据库缓存层采用主备模式,数据库层采用主从模式。
为了更接近生产环境,采用两台实体机部署此次环境,将前端反向代理层、数据库缓存层、数据库层部署在实体机上,只将Web层部署在KVM虚拟机当中。同时将每一层都做了高可用架构,保证业务的稳定性。
拓扑架构如图所示:实现是正常情况数据流向,虚线是异常情况下的数据流向
[root@localhost ~]#rpm -ivh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
[root@localhost ~]#yum install -y keepalived nginx
[root@localhost ~]#vi /etc/keepalived/keepalived.conf
! Configuration File for keepalived
vrrp_script nginx {
interval 2
}
global_defs {
notification_email {
}
global_defs {
notification_email {
acassen@firewall.loc
failover@firewall.loc
}
notification_email_from Alexandre.Cassen@firewall.loc
smtp_server 192.168.200.1
smtp_connect_timeout 30
}
vrrp_instance VI_1 {
state MASTER
interface ens33
virtual_router_id 51
priority 100
! Configuration File for keepalived
vrrp_script nginx { #添加
script "/opt/shell/nginx.sh"
interval 2
}
global_defs {
notification_email {
acassen@firewall.loc
failover@firewall.loc
sysadmin@firewall.loc
}
notification_email_from Alexandre.Cassen@firewall.loc
smtp_server 192.168.200.1
smtp_connect_timeout 30
router_id NGINX_HA
}
state MASTER
sysadmin@firewall.loc
}
notification_email_from Alexandre.Cassen@firewall.loc
smtp_server 192.168.200.1
smtp_connect_timeout 30
router_id NGINX_HA
}
vrrp_instance VI_1 {
state MASTER #主服务器
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.126.188 #内网虚拟IP
192.168.100.188 #外网虚拟IP
}
}
注:在配置文件里面载入了一个nginx.sh脚本,脚本的含义是如果keepalived已启动,那么每2秒检查并启动一次nginx服务
[root@localhost ~]# mkdir /opt/shell
[root@localhost ~]# cd /opt/shell/
[root@localhost shell]# vim 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
[root@localhost shell]# chmod +x nginx.sh #脚本增加可执行权限
[root@localhost ~]# vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
vrrp_script nginx { #nginx触发脚本
script "/opt/shell/nginx.sh"
interval 2
}
global_defs {
notification_email {
acassen@firewall.loc
failover@firewall.loc
sysadmin@firewall.loc
}
notification_email_from Alexandre.Cassen@firewall.loc
smtp_server 192.168.200.1
smtp_connect_timeout 30
router_id NGINX_HB # 名称
}
vrrp_instance VI_1 {
state BACKUP #从服务器
interface ens33
virtual_router_id 52 #id号
priority 90 #优先级
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
track_script { #添加
nginx
}
virtual_ipaddress {
192.168.126.188 #虚拟IP
192.168.100.188
}
}
另外一台也一样,这里不再赘述
[root@localhost PV]# vim /etc/nginx/nginx.conf
#gzip on;
upstream tomcat_pool {
server 192.168.126.193:8080; #两台节点服务器的IP
server 192.168.126.194:8080;
ip_hash; #会话稳固功能,否则无法通过vip地址登陆
}
server {
listen 80;
server_name 192.168.126.188; #虚拟出的IP
location / {
proxy_pass http://tomcat_pool; proxy_set_header X-Real-IP $remote_addr;
}
}
[root@localhost PV]# 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
[root@localhost PV]# systemctl stop firewalld.service
[root@localhost PV]# setenforce 0
[root@localhost PV]# systemctl start keepalived.service
关闭主机的Keepalived服务,查看虚拟IP是否可以自动漂移到备机上。
主服务器:
[root@localhost PV]# ip addr
inet 192.168.126.138/24 brd 192.168.126.255 scope global dynamic ens33
valid_lft 1697sec preferred_lft 1697sec
inet 192.168.126.188/32 scope global ens33
valid_lft forever preferred_lft forever
[root@localhost PV]# systemctl stop keepalived.service
link/ether 00:0c:29:15:68:5d brd ff:ff:ff:ff:ff:ff
inet 192.168.126.138/24 brd 192.168.126.255 scope global dynamic ens33
valid_lft 1625sec preferred_lft 1625sec
inet6 fe80::7f06:3732:bb38:c9e1/64 scope link
valid_lft forever preferred_lft forever
备份服务器:
[root@localhost ~]# ip addr
link/ether 00:0c:29:5b:03:86 brd ff:ff:ff:ff:ff:ff
inet 192.168.126.192/24 brd 192.168.126.255 scope global dynamic ens33
valid_lft 1180sec preferred_lft 1180sec
inet 192.168.126.188/32 scope global ens33
valid_lft forever preferred_lft forever
inet 192.168.100.188/32 scope global ens33 #虚拟IP自动漂移成功
注意:再次将主服务器的keepalived服务开启后,虚拟IP又会漂移到主服务器上。
[root@localhost PV]# tar zxvf jdk-8u144-linux-x64.tar.gz -C /opt/
[root@localhost PV]# tar zxvf apache-tomcat-8.5.23.tar.gz -C /opt/
[root@localhost PV]# cd /opt/
[root@localhost opt]# ls
apache-tomcat-8.5.16 apache-tomcat-8.5.23 jdk1.8.0_144 jdk1.8.0_91 rh
[root@localhost opt]# mv jdk1.8.0_144/ /usr/local/java
[root@localhost opt]# mv apache-tomcat-8.5.23/ /usr/local/tomcat8
[root@localhost opt]# 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@localhost opt]# source /etc/profile #启动
[root@localhost opt]# ln -s /usr/local/tomcat8/bin/startup.sh /usr/bin/tomcatup[root@localhost opt]# ln -s /usr/local/tomcat8/bin/shutdown.sh /usr/bin/tomcatdown
[root@localhost opt]# systemctl stop firewalld.service
[root@localhost opt]# setenforce 0
[root@localhost opt]# tomcatup
[root@localhost opt]# netstat -ntap | grep 8080
tcp6 0 0 :::8080 :::* LISTEN 3255/java
[root@localhost opt]# cd /usr/local/tomcat8/webapps/ROOT/
[root@localhost ROOT]# mv index.jsp index.jsp.bk
[root@localhost ROOT]# vim index.jsp
<h1>Server abc01!!</h1>
停止节点服务器1的tomcat服务,再次刷新浏览器,查看是否调度成功。如图所示:
[root@localhost ROOT]# tomcatdown
[root@localhost conf]# vim server.xml
<Context path="" docBase="SLSaleSystem" reloadable="true" debug="0"></Context> #日志调试信息debug为0表示信息越少,docBase指定访问目录。
[root@localhost PV]# yum install mariadb-server mariadb -y
[root@localhost PV]# systemctl enable mariadb.service #开启自启动
[root@localhost PV]# systemctl start mariadb.service #开启服务
[root@localhost PV]# netstat -ntap | grep 3306
tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 28285/mysqld
[root@localhost PV]# mysql_secure_installation
Enter current password for root (enter for none): #Enter
Set root password? [Y/n] y 设置用户密码
New password: #abc123
Re-enter new password: #确认密码abc123
Remove anonymous users? 删除匿名用户 n
Disallow root login remotely? 不允许远程root登录吗? n
Remove test database and access to it? 删除测试数据库并访问它? n
Reload privilege tables now? 现在重新加载权限表吗? y
[root@localhost PV]# mysql -u root -p < slsaledb-2014-4-10.sql
[root@localhost PV]# mysql -u root -p
MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| slsaledb |
| test |
+--------------------+
5 rows in set (0.02 sec)
MariaDB [(none)]> GRANT all ON slsaledb.* TO ‘root‘@‘%‘ IDENTIFIED BY ‘abc123‘; #授权
MariaDB [(none)]> flush privileges; #刷新
[root@localhost PV]# tar zxvf SLSaleSystem.tar.gz -C /usr/local/tomcat8/webapps/
[root@localhost PV]# cd /usr/local/tomcat8/webapps/SLSaleSystem/WEB-INF/
classes/
[root@localhost classes]# vim jdbc.properties
driverClassName=com.mysql.jdbc.Driver
url=jdbc\:mysql\://192.168.126.188\:3306/slsaledb?useUnicode\=true&characterEncoding\=UTF-8
uname=root #用户名
password=abc123 #密码
minIdle=10
maxIdle=50
initialSize=5
maxActive=100
maxWait=100
removeAbandonedTimeout=180
removeAbandoned=true
redis简介:
redis是一个高性能的key-value数据库,和Memcached类似,但它支持的value类型更多。与Memcached一样,为了保证效率,数据都是缓存在内存中的。
区别是:redis会周期性地把更新的数据写入磁盘或者把修改操作写入追加的记录文件中,并且在此基础上实现了master-slave(主从)同步。
[root@localhost ~]# yum install -y epel-release
[root@localhost ~]#yum install redis -y
[root@localhost ~]# vim /etc/redis.conf
bind 0.0.0.0 #61行
从服务器上多如下一行配置
slaveof 192.168.126.138 6379 #主服务器的IP不是虚拟IP(266行)
[root@localhost ~]# systemctl start redis.service
[root@localhost ~]# netstat -anpt | grep 6379
tcp 0 0 0.0.0.0:6379 0.0.0.0:* LISTEN 50475/redis-server
主服务器:
[root@localhost ~]# redis-cli -h 192.168.126.138 -p 6379
192.168.126.138:6379> set name test #设置name 值是test
OK
192.168.126.138:6379> get name #获取name值
"test"
从服务器:
[root@localhost ~]# redis-cli -h 192.168.126.192 -p 6379 #登录从,获取值,成功说明主从同步成功。
192.168.126.192:6379> get name
"test"
192.168.126.192:6379>
[root@localhost classes]#cd /usr/local/tomcat8/webapps/SLSaleSystem/WEB-INF/classes/
[root@localhost classes]# vim applicationContext-mybatis.xml
<constructor-arg value="192.168.126.188"/> #47行
<constructor-arg value="6379"/> #48行
[root@localhost ~]# redis-cli -h 192.168.126.188 -p 6379
192.168.126.188:6379> info
.....//省略
evicted_keys:0
keyspace_hits:1 #keyspace_hits:1 或者 keyspace_misses:2//关注这个值,命中数和未命中数
keyspace_misses:0
pubsub_channels:0
登录商城,多次点击需要数据库参与的操作页面,再回来检查keyspace_hits或者keyspace_misses: 值变化。如果keyspace_hits:1 值变为2,说明redis在正常工作。
[root@localhost ~]# vi /etc/redis-sentinel.conf
protected-mode no #17行 开启
sentinel monitor mymaster 192.168.126.138 6379 1 #1表示1台从 注意:修改为主IP
sentinel down-after-milliseconds mymaster 3000 #故障切换时间单位是毫秒
[root@localhost ~]# service redis-sentinel start
[root@localhost ~]# netstat -anpt | grep 26379
tcp 0 0 0.0.0.0:26379 0.0.0.0:* LISTEN 57151/redis-sentine
tcp6 0 0 :::26379 :::* LISTEN 57151/redis-sentine
[root@localhost ~]# redis-cli -h 192.168.126.138 -p 26379 infoSentinel #查看集群信息
# 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.126.138:6379,slaves=1,sentinels=1
主服务器:
root@localhost ~]# systemctl stop redis.service #关闭主服务器的redis服务
[root@localhost ~]# redis-cli -h 192.168.126.138 -p 26379 info Sentinel # Sentinel #发现主切换成从服务器的IP
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.126.192:6379,slaves=1,sentinels=1
在主服务器上:
[root@localhost ~]# redis-cli -h 192.168.126.192 -p 6379
192.168.126.192:6379> set name2 test2
OK
192.168.126.192:6379> get name2
"test2"
192.168.126.192:6379>
在从服务器上查看:
[root@localhost ~]# redis-cli -h 192.168.126.192 -p 6379
192.168.126.192:6379> get name2
"test2"
启动主服务器的redis服务:
[root@localhost ~]# systemctl start redis.service
[root@localhost ~]# redis-cli -h 192.168.126.138 -p 6379
192.168.126.138:6379> get name2
"test2" #同步成功
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
binlog-ignore-db=mysql,information_schema #日志
character_set_server=utf8 #字符集
log_bin=mysql_bin #二进制日志
server_id=1 #从服务器的id号不能相同
log_slave_updates=true
sync_binlog=1
[root@localhost ~]# systemctl restart mariadb.service
[root@localhost ~]# netstat -ntap | grep 3306
tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 65321/mysqld
[root@localhost ~]# mysql -u root -p
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.126.%‘ identified by ‘123456‘; #授权
MariaDB [(none)]> flush privileges;
[root@localhost ~]# mysql -u root -p
MariaDB [(none)]> change master to master_host=‘192.168.126.138‘,master_user=‘rep‘,master_password=‘123456‘,master_log_file=‘mysql_bin.000001‘,master_log_pos=245;
MariaDB [(none)]> start slave;
Ma
riaDB [(none)]> show slave status\G;
.....//省略
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
在Mysql主服务器上创建一个库,看从服务器上是否能够同步主服务器的新数据库。
主服务器:
MariaDB [(none)]> flush privileges;
MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| school |
| slsaledb |
| test |
+--------------------+
6 rows in set (0.28 sec)
从服务器:
MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| school | #
| slsaledb |
| test |
+--------------------+
6 rows in set (0.19 sec)
一步一步操作,你也可以成功!
标签:love rip jdbc coding cat ima 默认 php _id
原文地址:http://blog.51cto.com/13687553/2152773