8.14haproxy

一、haproxy
官网https://www.haproxy.com/
自由及开放源代码软件

HAProxy是一个使用C语言编写的自由及开放源代码软件,其提供高可用性、负我均衡,以及基TCP和HTTP的应用程序代理。

HAProxy特别适用于那些负载特大的veb站点,这些站点通常又需要会活保或七层处理。HAProxy运行在当前的硬件上,完全可以支持数以万计的并发连接。并且它的运行模式一与它可以很简单安全的整合进用户当前的架构中,同时可以保护用户的web服务器不被暴露到网上。

HAProxy实现了一种事件驱动,单一进程模型,此模型支持非常大的并发连接数。多进程或多线程模型受内存限制、系统调度器限制以及无处不在的锁限制,很少能处理数千并发连接。事件驱动模型因为在有更好的资源和时间管理的用户空间(User-Space)实现所有这些任务,所以没有这些问题。此模型的弊端是,在多核系统上,这些程序通常扩展性较差。这就是为什么他们必须进行优化以使每个CPU时间片(Cycle)做更多的工作。

包括GitHub、Bitbucket[3]、Stack Overflow[4]、Reddit、Tumblr、Twitter5和Tuenti[7]在内的知名网站,及亚马逊网络服务系统都使用了HAProxy。

1、安装
[root@haproxy ~]# yum -y install ntpdate

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

2、安装haproxy
[root@haproxy ~]# yum -y install haproxy

3、配置文件地址
[root@haproxy ~]# rpm -ql haproxy
[root@haproxy ~]# vim /etc/haproxy/haproxy.cfg 

  

4、启动服务
[root@haproxy ~]# systemctl start haproxy.service 
[root@haproxy ~]# systemctl enable haproxy.service 

5、web服务器
[root@web01 ~]# echo "web01" > /usr/share/nginx/html/index.html

[root@web02 ~]# echo "web02" > /usr/share/nginx/html/index.html

6、访问测试
[root@haproxy ~]# curl 192.168.8.149
web02
[root@haproxy ~]# curl 192.168.8.149
web01

7、DNS服务
[root@dns ~]# vim /var/named/aaa.com.zone

 [root@dns ~]# systemctl restart named

[root@haproxy ~]# echo "nameserver 192.168.8.143" > /etc/resolv.conf

[root@haproxy ~]# curl hp.aaa.com
web01
[root@haproxy ~]# curl hp.aaa.com
web02

8、添加统计页面
[root@haproxy ~]# vim /etc/haproxy/haproxy.cfg 

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

  9、加权轮询
[root@haproxy ~]# vim /etc/haproxy/haproxy.cfg 

  

[root@haproxy ~]# systemctl restart haproxy.service 

  二、使用haproxy对mysql做代理
[root@haproxy ~]# vim /etc/haproxy/haproxy.cfg 

[root@haproxy ~]# systemctl restart haproxy.service 

三、mysql读写分离
[root@python ~]# pip3 config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple 

[root@python ~]# pip3 install -i https://pypi.tuna.tsinghua.edu.cn/simple/ some-package

[root@python ~]# pip3 install pymysql 

[root@python ~]# python3

>>> import pymysql
>>>master_conn=pymysql.connect(host="192.168.8.148",user="slave",password="slave_123",database="test",port=3306) 
>>>slave_conn=pymysql.connect(host="192.168.8.146",user="slave",password="slave123",database="test",port=3306)
>>> updatesql="update user set password='000' where username='aaa'"
>>> master_cursor.execute(updatesql)
>>> master_conn.commit()
>>> delete_sql="delete from user where username='aaa' "
>>> master_cursor. execute(delete_sql)
1
>>> master conn. commit ()
# 执行查询slave
>>># 执行查询 获得获得slave 游标
>>> slave_cursor=slave_conn.cursor()
>>> sql
'select * from user'
>>> slave_cursor.execute(sql)
3
>>> slave_cursor.fetchall()
((2, 'bbb', 'bbbb'), (3, 'ccc', 'cccc'),(1004, 'ddddd', 'ddddddd'))
 

class rwsplit(object):        

        def __init__ (self):                

                print("initialized")

if __name__ == "__main__":         demo=rwsplit()

class rwsplit(object):        

         def __init__ (self):                

                print("initialized")        

        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)

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值