青岛实训 8月14号 day28

一、haproxy负载均衡

官网网址: https://www.haproxy.com/

HAPrOxy是一个使用C语言编写的自由及开旅酒代码软性,其提供高可用性、负我均衡,以及基于TCP和HTTP的应用程座代理
HAProxy特别适用于那些负载特大的webi些站点通常又需要会话保挂或七层处理。HAPrOxy运行在当动的硬件上,完全可以支持数以万计的并发连接。并且它的运行模式使得它可以很简单安全地整合进用户当前的架构中,同时可以保护用户的web服务器不被幂露到网络上。
HAPTOXy实现了一种事件驱动,单一进程横型,此模型支持非常大的并发连接数。多进程或多线程模型受内存限制、系统调度器限制以及无处不在的锁限制,很少能处理数千并发连接。事件聚动模型因为在有更好的资源和时间管理的用户空间(User-Space)实现所有这些任务,所以没有这些问题,此模型的辩端是,在多核系统上,这些程序通常扩展性较差,这就是为什么他们必须进行优化以 使每个CPU时间片(Cycle)做更多的工作。
包括 GiHu.Bilbuckelf. stack overiew.Twiller和 Tuem在内的知名网站,及亚马逊网络服务系统都使用了HAPrOXY。

安装

[root@haproxy ~]# yum -y install ntpdate        时间同步
[root@haproxy ~]# ntpdate cn.ntp.org.cn

[root@haproxy ~]# yum -y install ntp
[root@haproxy ~]# systemctl start ntpd        开启服务
[root@haproxy ~]# systemctl enable ntpd

 安装haproxy

[root@haproxy ~]# yum -y install haproxy
配置文件的地址

[root@haproxy ~]# vim /etc/haproxy/haproxy.cfg 

 

[root@haproxy ~]# systemctl status haproxy        查看状态
● haproxy.service - HAProxy Load Balancer
   Loaded: loaded (/usr/lib/systemd/system/haproxy.service; disabled; vendor preset: disabled)
   Active: inactive (dead)
[root@haproxy ~]# systemctl start haproxy        启动服务
[root@haproxy ~]# systemctl enable haproxy        设置开机自启动
Created symlink from /etc/systemd/system/multi-user.target.wants/haproxy.service to /usr/lib/systemd/system/haproxy.service.

测试:

[root@haproxy ~]# curl 192.168.2.41
web01
[root@haproxy ~]# curl 192.168.2.41
web02


添加统计页面

[root@haproxy ~]# vim /etc/haproxy/haproxy.cfg 

 92 # 定义web管理界面
 93 listen statistics
 94         bind *:9090 #定义监听端口
 95         mode http #默认使用协议
 96         stats enable #启用stats
 97         stats uri /hadmin?stats #自定义统计页面的url
 98         stats auth admin:admin #统计页面的账号密码
 99         stats hide-version #隐藏在统计页面上的haproxy版本信息
100         stats refresh 30s #统计页面自动刷新时间
101         stats admin if TRUE #如果认证通过就做管理功能,可以管理后端服务器
102         stats realm hapadmin #统计页面密码框上提示文件,默认为haproxy\statistics


访问192.168.2.41:9090/hadmin?stats

账号和密码都是admin,出现页面

  编辑haproxy配置文件,添加权重功能

[root@haproxy ~]# vim   /etc/haproxy/haproxy.cfg
 backend web
     balance     static-rr
     server  web01 10.0.0.11:80 weight 3 check
     server  web02 10.0.0.12:80 weight 1 check
 [root@haproxy ~]# systemctl restart haproxy

二、设置mysql的haproxy

1、python代码实现mysql的读写分离

(1)安装pymysql是python管理mysql的驱动,或者成为连接器

 [root@python ~]# pip3 config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple
 [root@python ~]# pip3 install pymysql   #安装pymysql


(2)在python3的命令行界面引入pymysql

 [root@python ~]# mysql3
 >>> import pymysql


(3)创建两个connenction对象,一个指向master mysql,一个指向slave mysql

 master_conn=pymysql.connect(host="10.0.0.21",user="li",port=3306,database="test",password="li")
 slave_conn=pymysql.connect(host="10.0.0.22",user="li",port=3306,database="test",password="li")


(4)获取数据游标

 >>> master_cursor=master_conn.cursor()


(5)执行查询

 >>> select_sql="select * from student"
 >>> master_cursor.execute(select_sql)
 3
 >>> master_cursor.fetchall()
 ((1001, '孙颖莎', '云计算2班', '1002'), (1002, '大头', '云计算2班', '1003'), (1003, '小胖', '云计算2班', '1004'))

(6)执行修改

 >>> update_sql="update student set name='马龙' where id=1002"
 >>> master_cursor.execute(update_sql)
 1
 >>> master_conn.commit()


(7)执行删除

 >>> delete_sql="delete from student where name='小胖'"
 >>> master_cursor.execute(delete_sql);
 1
 >>> master_conn.commit()

(8)执行新增

 >>> insert_sql="insert into student values(1004,'张翔','云计算2班','1004')"
 >>> master_cursor.execute(insert_sql)
 1
 >>> master_conn.commit()

(9)执行查询(slave)

 >>> slave_cursor=slave_conn.cursor()
 >>> sql="select * from student"
 >>> slave_cursor.execute(sql)
 3
 >>> slave_cursor.fetchall()
 ((1001, '孙颖莎', '云计算2班', '1002'), (1002, '马龙', '云计算2班', '1003'), (1004, '张翔', '云计算2班', '1004'))

2、python脚本实现mysql的读写分离 

[root@python ~]# vim rwpython.py
 class rwsplit(object):
         def __init__ (self):
                 print("initialize")
         def master_statment(self,sql):
                 pass
         def slave_statment(self,sql):
                 pass
 ​
 ​
 if __name__ == "__main__":
         demo=rwsplit()
         sql=input("sign sql:")
         if sql[:6]=="select":
                 demo.slave_statment(sql)
         else:
                 demo.master_statment(sql)
 

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值