haproxy简单负载均衡搭建

YzhilongY 2014-05-13

最近对负载均衡进行搭建具体方法如下:

  • haproxy 修改部分(haproxy-cfg.cfg)
global
     daemon
     maxconn 4500 
defaults
        mode http
        timeout connect 5000ms
        timeout client 50000ms
        timeout server 180000ms
        stats enable
        stats uri /stats
        stats auth admin:linkage!^*
frontend http-in
        bind *:9090
        default_backend dynamic_servers
backend dynamic_servers
        mode http
        option httpclose
        option forwardfor header X-Forwarded-For
        appsession JSESSIONID len 52 timeout 30m
        balance roundrobin
        server server_9091 218.9.70.6:9091 maxconn 250
        server server_9092 218.9.70.6:9092 maxconn 250
        server server_9093 218.9.70.6:9093 maxconn 250
        server server_9094 218.9.70.6:9094 maxconn 250
        server server_9095 218.9.70.6:9095 maxconn 250

 使用haproxy做端口转发,只需要修改tomcat的 到不同的端口(保证端口不被占用)

在haproxy映射即可。

启动方法 ./haproxy -f haproxy-cfg.cfg

haproxy 有编译好的版本,也可以自行编译参考方法

http://wenku.baidu.com/view/0a7a28154431b90d6c85c760.html 

tomcat示例

<!-- Note:  A "Server" is not itself a "Container", so you may not
     define subcomponents such as "Valves" at this level.
     Documentation at /docs/config/server.html
-->
<Server port="9005" shutdown="SHUTDOWN">
 
  <!--APR library loader. Documentation at /docs/apr.html -->
  <Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" />
  <!--Initialize Jasper prior to webapps are loaded. Documentation at /docs/jasper-howto.html -->
  <Listener className="org.apache.catalina.core.JasperListener" />
  <!-- JMX Support for the Tomcat server. Documentation at /docs/non-existent.html -->
  <Listener className="org.apache.catalina.mbeans.ServerLifecycleListener" />
  <Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" />
 
  <!-- Global JNDI resources
       Documentation at /docs/jndi-resources-howto.html
  -->
  <GlobalNamingResources>
    <!-- Editable user database that can also be used by
         UserDatabaseRealm to authenticate users
    -->
    <Resource name="UserDatabase" auth="Container"
              type="org.apache.catalina.UserDatabase"
              description="User database that can be updated and saved"
              factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
              pathname="conf/tomcat-users.xml" />
  </GlobalNamingResources>
 
  <!-- A "Service" is a collection of one or more "Connectors" that share
       a single "Container" Note:  A "Service" is not itself a "Container", 
       so you may not define subcomponents such as "Valves" at this level.
       Documentation at /docs/config/service.html
   -->
  <Service name="Catalina">
    <!--The connectors can use a shared executor, you can define one or more named thread pools-->
    <!--
    <Executor name="tomcatThreadPool" namePrefix="catalina-exec-" 
        maxThreads="150" minSpareThreads="4"/>
    -->
    
    
    <!-- A "Connector" represents an endpoint by which requests are received
         and responses are returned. Documentation at :
         Java HTTP Connector: /docs/config/http.html (blocking & non-blocking)
         Java AJP  Connector: /docs/config/ajp.html
         APR (HTTP/AJP) Connector: /docs/apr.html
         Define a non-SSL HTTP/1.1 Connector on port 8080
    -->
    <Connector port="9091" protocol="HTTP/1.1" 
               connectionTimeout="100000" 
               redirectPort="8443" URIEncoding="gbk"/>
<!-- A "Connector" using the shared thread pool-->
    <!--
    
<Connector executor="tomcatThreadPool"
               port="8081" protocol="HTTP/1.1" 
               connectionTimeout="20000" 
               redirectPort="8443" />
    -->           
    <!-- Define a SSL HTTP/1.1 Connector on port 8443
         This connector uses the JSSE configuration, when using APR, the 
         connector should be using the OpenSSL style configuration
         described in the APR documentation -->
    <!--
    <Connector port="8443" protocol="HTTP/1.1" SSLEnabled="true"
               maxThreads="150" scheme="https" secure="true"
               clientAuth="false" sslProtocol="TLS" />
    -->
.....

相关推荐