zoukankan      html  css  js  c++  java
  • HAProxy Windows版本的编译及其在CORS中的应用

    一.HAProxy简介

      HAProxy提供高可用性 、负载均衡以及基于TCP和HTTP应用的代理,它是免费、快速并且可靠的一种解决方案。HAProxy特别适用于那些负载特大的web站点,这些站点通常又需要会话保持或七层处理。 HAProxy运行在当前的硬件上,完全可以支持数以万计 的并发连接。并且它的运行模式使得它可以很简单安全的整合进您当前的架构中, 同时可以保护你的web服务器不被暴露到网络上。

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

      

    二.应用可行性

      出于HAProxy的众多优点,将其应用于CORS(连续运行卫星定位服务综合系统)运维工作可有效提高现有的最大负载能力、减少硬件资源的浪费、保障系统的可用性及稳定性。具体地,可以从以下几个方面阐述:

      1.基于TCP代理在基准站数据流分发流程中充当代理服务器的功能;

      2.基于TCP代理在用户接入及数据播发流程中起到负载均衡均衡器的作用;

      对于用户数量多,并发性高的CORS,使用HAProxy将对外服务端口的请求对应至控制中心内部网络的多个相同功能的应用程序(如IGate/TNC等)的服务端   HAProxy可以通过多种方式(IP、cookie、session)保持tcp链接的亲缘性,确保数据交互的正确性。通过多种负载均衡算法,内部的服务程序分担总体的服务压力,当某个程序down掉了,HAProxy能够自动将相关的tcp链接分发至其它服务程序。HAProxy实现了8种负载均衡算法: 

    • roundrobin,表示简单的轮询
    • static-rr,表示根据权重,可根据服务器性能赋权
    • leastconn,表示最少连接者先处理
    • source,表示根据请求源IP
    • uri,表示根据请求的URI
    • url_param,表示根据请求的URl参数'balance url_param' requires an URL parameter name
    • hdr(name),表示根据HTTP请求头来锁定每一次HTTP请求
    • rdp-cookie(name),表示根据据cookie(name)来锁定并哈希每一次TCP请求

      3.基于tcp代理实现内部网络的防火墙的NAT功能。

    三.HAProxy的安装与配置

      此文编写HAProxy最新稳定版本的下载地址为:http://haproxy.1wt.eu/download/1.4/src/haproxy-1.4.25.tar.gz。

      1.Linux下HAProxy的安装

    • tar zcvf haproxy-1.4.25.tar.gz 
    • cd haproxy-1.4.25
    • make TARGET=linux28 PREFIX=/usr/local/haprpxy
    • make install PREFIX=/usr/local/haproxy
    • cd /usr/local/haproxy
    • vim haproxy.cfg  编辑配置文件,后面统一阐述配置的相关细节
    • /usr/local/haproxy/sbin/haproxy -f /usr/local/haproxy/haproxy.cfg    使用刚才的配置文件启动HAProxy

      2. Windows下HAProxy的编译与安装

    • 下载cygwin安装工具,文章编写时其64位下载地址为:https://cygwin.com/setup-x86_64.exe。运行程序,程序截图如下:
    • 安装gcc,状态为Keep的是本机已安装的,如未安装请勾选Bin列的复选框
    • 安装make工具。
    • 将下载的HAProxy源码包复制到:C:cygwin64home,并解压到该目录,
    • 使用Cywin64 Terminal命令行工具进入HAProxy目录:
    • make TARGET=cygwin
    • make install   这样haproxy.exe就编译好了,将haproxy.exe和cygwin1.dll拷贝出来以供使用。
    • 运行方式与Linux环境下一致。

      3.HAProxy配置文件解析

      ########默认配置############
    defaults
    mode http #默认的模式mode { tcp|http|health },tcp是4层,http是7层,health只会返回OK
    retries 2 #两次连接失败就认为是服务器不可用,也可以通过后面设置
    option redispatch #当serverId对应的服务器挂掉后,强制定向到其他健康的服务器
    option abortonclose #当服务器负载很高的时候,自动结束掉当前队列处理比较久的链接
    maxconn 4096 #默认的最大连接数
    timeout connect 5000ms #连接超时
    timeout client 30000ms #客户端超时
    timeout server 30000ms #服务器超时
    #timeout check 2000 #=心跳检测超时
    log 127.0.0.1 local0 err #[err warning info debug]


    ########统计页面配置########
    listen admin_stats
    bind 0.0.0.0:1080 #监听端口
    mode http #http的7层模式
    option httplog #采用http日志格式
    #log 127.0.0.1 local0 err
    maxconn 10
    stats refresh 30s #统计页面自动刷新时间
    stats uri /stats #统计页面url
    stats realm XingCloud Haproxy #统计页面密码框上提示文本
    stats auth admin:admin #统计页面用户名和密码设置
    stats hide-version #隐藏统计页面上HAProxy的版本信息


    ########test1配置#################
    listen test1
    bind 0.0.0.0:90
    mode tcp
    #maxconn 4086
    #log 127.0.0.1 local0 debug
    server s1 10.18.138.201:80
    server s2 10.18.102.190:80
    server s3 10.18.102.189:80
    server s4 10.18.102.188:80
    server s5 10.18.102.187:80
    ########frontend配置##############


    ########test2配置#################
    listen test2
    bind 0.0.0.0:91
    mode tcp
    #maxconn 4086
    #log 127.0.0.1 local0 debug
    server s1 10.18.138.130:80 weight 1
    server s2 10.18.138.201:8080 weight 6
    ########frontend配置##############

      4.实例测试

      为了测试运行效果我们使用最简单的配置做一个简单的tcp代理:

    ##########全局配置#########
    global
    daemon
    nbproc 1
    pidfile /var/run/haproxy.pid


    ########默认配置############
    defaults
    mode http #默认的模式mode { tcp|http|health },tcp是4层,http是7层,health只会返回OK
    retries 2 #两次连接失败就认为是服务器不可用,也可以通过后面设置
    option redispatch #当serverId对应的服务器挂掉后,强制定向到其他健康的服务器
    option abortonclose #当服务器负载很高的时候,自动结束掉当前队列处理比较久的链接
    maxconn 4096 #默认的最大连接数
    timeout connect 5000ms #连接超时
    timeout client 30000ms #客户端超时
    timeout server 30000ms #服务器超时
    #timeout check 2000 #=心跳检测超时
    log 127.0.0.1 local0 err #[err warning info debug]


    ########统计页面配置########
    listen admin_stats
    bind 0.0.0.0:1080 #监听端口
    mode http #http的7层模式
    option httplog #采用http日志格式
    #log 127.0.0.1 local0 err
    maxconn 10
    stats refresh 30s #统计页面自动刷新时间
    stats uri /stats #统计页面url
    stats realm XingCloud Haproxy #统计页面密码框上提示文本
    stats auth admin:admin #统计页面用户名和密码设置
    stats hide-version #隐藏统计页面上HAProxy的版本信息


    ########test1配置#################
    listen test1
    bind 0.0.0.0:81
    mode tcp
    #maxconn 4086
    #log 127.0.0.1 local0 debug
    server s1 192.168.1.200:8000

    server s1 192.168.1.201:8001

    server s1 192.168.1.202:8002
    ########frontend配置##############


    ########test2配置#################
    listen test2
    bind 0.0.0.0:91
    mode tcp
    #maxconn 4086
    #log 127.0.0.1 local0 debug
    server s1 192.168.1.100:9000

    server s1 192.168.1.101:9001

    server s1 192.168.1.102:9002
    ########frontend配置##############

        

  • 相关阅读:
    poj 2763 Housewife Wind
    hdu 3966 Aragorn's Story
    poj 1655 Balancing Act 求树的重心
    有上下界的网络流问题
    URAL 1277 Cops and Thieves 最小割 无向图点带权点连通度
    ZOJ 2532 Internship 网络流求关键边
    ZOJ 2760 How Many Shortest Path 最大流+floyd求最短路
    SGU 438 The Glorious Karlutka River =) 拆点+动态流+最大流
    怎么样仿写已知网址的网页?
    5-10 公路村村通 (30分)
  • 原文地址:https://www.cnblogs.com/AllStarGIS/p/3776986.html
Copyright © 2011-2022 走看看