ivaneeo's blog

自由的力量,自由的生活。

  BlogJava :: 首页 :: 联系 :: 聚合  :: 管理
  669 Posts :: 0 Stories :: 64 Comments :: 0 Trackbacks

#

roundrobin  Each server is used in turns, according to their weights.
                 This is the smoothest and fairest algorithm when the server's
                 processing time remains equally distributed. This algorithm
                 is dynamic, which means that server weights may be adjusted
                 on the fly for slow starts for instance. It is limited by
                 design to 4128 active servers per backend. Note that in some
                 large farms, when a server becomes up after having been down
                 for a very short time, it may sometimes take a few hundreds
                 requests for it to be re-integrated into the farm and start
                 receiving traffic. This is normal, though very rare. It is
                 indicated here in case you would have the chance to observe
                 it, so that you don't worry.

                 roundrobin:每个server根据权重依次被轮询,

这个算法是动态的,意味着
                 server的权重可以实时地被调整。对于每个haproxy的backend servers的数目
                 而言被限制在4128个活跃数目之内。


     static-rr   Each server is used in turns, according to their weights.
                 This algorithm is as similar to roundrobin except that it is
                 static, which means that changing a server's weight on the
                 fly will have no effect. On the other hand, it has no design
                 limitation on the number of servers, and when a server goes
                 up, it is always immediately reintroduced into the farm, once
                 the full map is recomputed. It also uses slightly less CPU to
                 run (around -1%).
                 静态roundrobin(static-rr):跟roundrobin类似,唯一的区别是不可以动态实时
                 server权重和backend 的server数目没有上限。

     leastconn   The server with the lowest number of connections receives the
                 connection. Round-robin is performed within groups of servers
                 of the same load to ensure that all servers will be used. Use
                 of this algorithm is recommended where very long sessions are
                 expected, such as LDAP, SQL, TSE, etc... but is not very well
                 suited for protocols using short sessions such as HTTP. This
                 algorithm is dynamic, which means that server weights may be
                 adjusted on the fly for slow starts for instance.
                 最小连接数目负载均衡策略(leastconn):round-robin适合于各个server负载相同的情况。
                 最小连接数目算法适合于长时间会话,如LDAP,SQL,TSE,但是并不适合于HTTP短连接的协议。

     source      The source IP address is hashed and divided by the total
                 weight of the running servers to designate which server will
                 receive the request. This ensures that the same client IP
                 address will always reach the same server as long as no
                 server goes down or up. If the hash result changes due to the
                 number of running servers changing, many clients will be
                 directed to a different server. This algorithm is generally
                 used in TCP mode where no cookie may be inserted. It may also
                 be used on the Internet to provide a best-effort stickiness
                 to clients which refuse session cookies. This algorithm is
                 static by default, which means that changing a server's
                 weight on the fly will have no effect, but this can be
                 changed using "hash-type".
                 源IP hash散列调度:将源ip地址进行hash,再根据hasn求模或者一致性hash定位到
                 hash表中的server上。相同的ip地址的请求被分发到同一个server上。但当server的数量变化时,
                 来自于同一client的请求可能会被分发到不同的server上。这个算法通常用在没有cookie的tcp模式下。

     uri         The left part of the URI (before the question mark) is hashed
                 and divided by the total weight of the running servers. The
                 result designates which server will receive the request. This
                 ensures that a same URI will always be directed to the same
                 server as long as no server goes up or down. This is used
                 with proxy caches and anti-virus proxies in order to maximize
                 the cache hit rate. Note that this algorithm may only be used
                 in an HTTP backend. This algorithm is static by default,
                 which means that changing a server's weight on the fly will
                 have no effect, but this can be changed using "hash-type".

                 This algorithm support two optional parameters "len" and
                 "depth", both followed by a positive integer number. These
                 options may be helpful when it is needed to balance servers
                 based on the beginning of the URI only. The "len" parameter
                 indicates that the algorithm should only consider that many
                 characters at the beginning of the URI to compute the hash.
                 Note that having "len" set to 1 rarely makes sense since most
                 URIs start with a leading "/".

                 The "depth" parameter indicates the maximum directory depth
                 to be used to compute the hash. One level is counted for each
                 slash in the request. If both parameters are specified, the
                 evaluation stops when either is reached.

     url_param   The URL parameter specified in argument will be looked up in
                 the query string of each HTTP GET request.

                 If the modifier "check_post" is used, then an HTTP POST
                 request entity will be searched for the parameter argument,
                 when it is not found in a query string after a question mark
                 ('?') in the URL. Optionally, specify a number of octets to
                 wait for before attempting to search the message body. If the
                 entity can not be searched, then round robin is used for each
                 request. For instance, if your clients always send the LB
                 parameter in the first 128 bytes, then specify that. The
                 default is 48. The entity data will not be scanned until the
                 required number of octets have arrived at the gateway, this
                 is the minimum of: (default/max_wait, Content-Length or first
                 chunk length). If Content-Length is missing or zero, it does
                 not need to wait for more data than the client promised to
                 send. When Content-Length is present and larger than
                 <max_wait>, then waiting is limited to <max_wait> and it is
                 assumed that this will be enough data to search for the
                 presence of the parameter. In the unlikely event that
                 Transfer-Encoding: chunked is used, only the first chunk is
                 scanned. Parameter values separated by a chunk boundary, may
                 be randomly balanced if at all.

                 If the parameter is found followed by an equal sign ('=') and
                 a value, then the value is hashed and divided by the total
                 weight of the running servers. The result designates which
                 server will receive the request.

                 This is used to track user identifiers in requests and ensure
                 that a same user ID will always be sent to the same server as
                 long as no server goes up or down. If no value is found or if
                 the parameter is not found, then a round robin algorithm is
                 applied. Note that this algorithm may only be used in an HTTP
                 backend. This algorithm is static by default, which means
                 that changing a server's weight on the fly will have no
                 effect, but this can be changed using "hash-type".

     hdr(name)   The HTTP header <name> will be looked up in each HTTP request.
                 Just as with the equivalent ACL 'hdr()' function, the header
                 name in parenthesis is not case sensitive. If the header is
                 absent or if it does not contain any value, the roundrobin
                 algorithm is applied instead.

                 An optional 'use_domain_only' parameter is available, for
                 reducing the hash algorithm to the main domain part with some
                 specific headers such as 'Host'. For instance, in the Host
                 value "haproxy.1wt.eu ", only "1wt" will be considered.

                 This algorithm is static by default, which means that
                 changing a server's weight on the fly will have no effect,
                 but this can be changed using "hash-type".

     rdp-cookie
     rdp-cookie(name)
                 The RDP cookie <name> (or "mstshash" if omitted) will be
                 looked up and hashed for each incoming TCP request. Just as
                 with the equivalent ACL 'req_rdp_cookie()' function, the name
                 is not case-sensitive. This mechanism is useful as a degraded
                 persistence mode, as it makes it possible to always send the
                 same user (or the same session ID) to the same server. If the
                 cookie is not found, the normal roundrobin algorithm is
                 used instead.

                 Note that for this to work, the frontend must ensure that an
                 RDP cookie is already present in the request buffer. For this
                 you must use 'tcp-request content accept' rule combined with
                 a 'req_rdp_cookie_cnt' ACL.

                 This algorithm is static by default, which means that
                 changing a server's weight on the fly will have no effect,
                 but this can be changed us
posted @ 2013-12-25 12:32 ivaneeo 阅读(508) | 评论 (0)编辑 收藏

1.安装mariadb on ubuntu
http://blog.secaserver.com/2013/07/install-mariadb-galera-cluster-ubuntu/

3 在服务器上用mysql -h 192.168.0.1 -u root -p mysql命令登录mysql数据库

然后用grant命令下放权限。

GRANT ALL PRIVILEGES ON *.* TO root@localhost IDENTIFIED BY 'root-password' WITH GRANT OPTION;

GRANT ALL PRIVILEGES ON *.* TO root@127.0.0.1 IDENTIFIED BY 'root-password' WITH GRANT OPTION;

GRANT ALL PRIVILEGES ON *.* TO root@'%' IDENTIFIED BY 'root-password' WITH GRANT OPTION;

例如:
GRANT   ALL   PRIVILEGES   ON   *.*   TO   root@'%'   identified   by   '123456'  

注意:自己根据情况修改以上命令中的 “用户”“ip地址”“密码”。 

2 安装和配置haproxy
option mysql-check [ user <username> ]   
USE mysql; INSERT INTO user (Host,User) values ('<ip_of_haproxy>','<username>'); FLUSH PRIVILEGESheck

only consists in parsing the Mysql Handshake Initialisation packet or Error packet, we don't send anything in this mode. It was reported that it can generate lockout if check is too frequent and/or if there is not enough traffic. In fact, you need in this case to check MySQL "max_connect_errors" value as if a connection is established successfully within fewer than MySQL "max_connect_errors" attempts after a previous connection was interrupted, the error count for the host is cleared to zero. If HAProxy's server get blocked, the "FLUSH HOSTS" statement is the only way to unblock it.

配置:
# this config needs haproxy-1.1.28 or haproxy-1.2.1 global
log 127.0.0.1
local0 info
#日志相关
log 127.0.0.1
local1 notice
maxconn 4096
daemon
#debug
#quiet defaults
log global mode http #option httplog option dontlognull retries 3 option redispatch maxconn 2000 contimeout 5000 clitimeout 50000 srvtimeout 50000 listen mysql bind 0.0.0.0:3333 #代理端口 mode tcp #模式 TCP option mysql-check user haproxy #mysql健康检查 root为mysql登录用户名 balance roundrobin #调度算法 server mysql1 172.20.21.1:3306 weight 1 check inter 1s rise 2 fall 2 server mysql2 172.20.21.2:3306 weight 1 check inter 1s rise 2 fall 2 server mysql3 172.20.21.3:3306 weight 1 check inter 1s rise 2 fall 2 listen stats :1936 mode http stats enable stats hide-version stats realm Haproxy\ Statistics stats uri / stats auth admin:admin
posted @ 2013-12-22 02:57 ivaneeo 阅读(1228) | 评论 (0)编辑 收藏

如何安裝RabbitMQ Cluster


建立測試環境與測試系統總是最花時間的,趁著今天重新安裝一組RabbitMQ 的測試環境,把所有的步驟和順序都整理起來吧。

安裝RabbitMQ

Our APT repository

To use our APT repository:

  1. Add the following line to your /etc/apt/sources.list:
    deb http://www.rabbitmq.com/debian/ testing main
    (Please note that the word testing in this line refers to the state of our release of RabbitMQ, not any particular Debian distribution. You can use it with Debian stable, testing or unstable, as well as with Ubuntu. We describe the release as "testing" to emphasise that we release somewhat frequently.)
  2. (optional) To avoid warnings about unsigned packages, add our public key to your trusted key list using apt-key(8):
    wget http://www.rabbitmq.com/rabbitmq-signing-key-public.asc sudo apt-key add rabbitmq-signing-key-public.asc
  3. Run apt-get update.
  4. Install packages as usual; for instance,
    sudo apt-get install rabbitmq-server


設定Cluster


最簡易的方法就是先啟動,cluster master node產生 /var/lib/rabbitmq/.erlang.cookie 這份文件,然後再把這份文件copy 到 每一個cluster node鄉對應的目錄下面去,接下來就參考RabbitMQ - create cluster這份文件,範例事先建立3台的cluster。
 
1. 啟動每台rabbitMQ
# rabbit1$ rabbitmq-server -detached
# rabbit2$ rabbitmq-server -detached
# rabbit3$ rabbitmq-server -detached

2. 確認每一台rabbitmq 是否是standalone狀態

# rabbit1$ rabbitmqctl cluster_status
Cluster status of node rabbit@rabbit1 ...
[{nodes,[{disc,[rabbit@rabbit1]}]},{running_nodes,[rabbit@rabbit1]}]
...done.
# rabbit2$ rabbitmqctl cluster_status
Cluster status of node rabbit@rabbit2 ...
[{nodes,[{disc,[rabbit@rabbit2]}]},{running_nodes,[rabbit@rabbit2]}]
...done.
# rabbit3$ rabbitmqctl cluster_status
Cluster status of node rabbit@rabbit3 ...
[{nodes,[{disc,[rabbit@rabbit3]}]},{running_nodes,[rabbit@rabbit3]}]
...done.

3. 依序加入cluster

rabbit2$ rabbitmqctl stop_app
Stopping node rabbit@rabbit2 ...done.
rabbit2$ rabbitmqctl reset
Resetting node rabbit@rabbit2 ...done.
rabbit2$ rabbitmqctl cluster rabbit@rabbit1
Clustering node rabbit@rabbit2 with [rabbit@rabbit1] ...done.
rabbit2$ rabbitmqctl start_app
Starting node rabbit@rabbit2 ...done. 


4. 確認cluster status是否正確加入

rabbit1$ rabbitmqctl cluster_status
Cluster status of node rabbit@rabbit1 ...
[{nodes,[{disc,[rabbit@rabbit1]},{ram,[rabbit@rabbit2]}]},
{running_nodes,[rabbit@rabbit2,rabbit@rabbit1]}]
...done.
rabbit2$ rabbitmqctl cluster_status
Cluster status of node rabbit@rabbit2 ...
[{nodes,[{disc,[rabbit@rabbit1]},{ram,[rabbit@rabbit2]}]},
{running_nodes,[rabbit@rabbit1,rabbit@rabbit2]}]
...done.


設定Web Management Plugin


參考 rabbitmq - Management Plugin,重點是要在Cluster node的每一台都要安裝這個plugin
# rabbitmq-plugins enable rabbitmq_management 
安裝好以後,要記得重啟服務
# service rabbitmq-server restart

設定好就可以連到http://server-name:55672/#/ 看看web 介面有沒有起來,預設的帳號密碼都是guest,如果是內部測試那還沒關係,如果是要連到外面,一定記得要改帳號密碼和permission。


設定帳號


最後我們可以設定一個vhost 以及這個vhost的帳號密碼和權限

#rabbitmqctl add_vhost /demo
#rabbitmqctl add_user demo mypass
#rabbitmqctl set_permissions -p /demo demo ".*" ".*" ".*"

启动HAProxy
/etc/default/haproxy
# Set ENABLED to 1 if you want the init script to start haproxy.
ENABLED=1

启动日志
/etc/rsyslog.d

添加文件haproxy.conf:
$ModLoad imudp
$UDPServerRun 514

local0.*                        -/var/log/haproxy-0.log
local1.*                        -/var/log/haproxy-1.log

HAProxy配置
# this config needs haproxy-1.1.28 or haproxy-1.2.1

global
        log 127.0.0.1   local0
        log 127.0.0.1   local1 notice
        #log loghost    local0 info
        maxconn 4096
        #chroot /usr/share/haproxy
        user haproxy
        group haproxy
        daemon
        #debug
        #quiet

defaults
        log     global
        mode    tcp
        option  tcplog
        option  dontlognull
        retries 3
        option redispatch
        maxconn 2000
        contimeout      5000
        clitimeout      50000
        srvtimeout      50000

listen rabbitmq_cluster 0.0.0.0:5678
       mode tcp
       balance roundrobin
       server   rabbit65 192.168.1.200:5672 check inter 2000 rise 2 fall 3
       server   rabbit66 192.168.1.201:5672 check inter 2000 rise 2 fall 3
listen stats :1936
    mode http
    stats enable
    stats hide-version
    stats realm Haproxy\ Statistics
    stats uri /
    stats auth admin:admin
posted @ 2013-12-16 19:11 ivaneeo 阅读(546) | 评论 (0)编辑 收藏

 Linux下高并发socket最大连接数所受的限制问题

  1、修改用户进程可打开文件数限制

  在Linux平台上,无论编写客户端程序还是服务端程序,在进行高并发TCP连接处理时,最高的并发数量都要受到系统对用户单一进程同时可打开文件数量的限制(这是因为系统为每个TCP连接都要创建一个socket句柄,每个socket句柄同时也是一个文件句柄)。可使用ulimit命令查看系统允许当前用户进程打开的文件数限制:

  [speng@as4 ~]$ ulimit -n

  1024

  这表示当前用户的每个进程最多允许同时打开1024个文件,这1024个文件中还得除去每个进程必然打开的标准输入,标准输出,标准错误,服务器监听 socket,进程间通讯的unix域socket等文件,那么剩下的可用于客户端socket连接的文件数就只有大概1024-10=1014个左右。也就是说缺省情况下,基于Linux的通讯程序最多允许同时1014个TCP并发连接。

  对于想支持更高数量的TCP并发连接的通讯处理程序,就必须修改Linux对当前用户的进程同时打开的文件数量的软限制(soft limit)和硬限制(hardlimit)。其中软限制是指Linux在当前系统能够承受的范围内进一步限制用户同时打开的文件数;硬限制则是根据系统硬件资源状况(主要是系统内存)计算出来的系统最多可同时打开的文件数量。通常软限制小于或等于硬限制。

  修改上述限制的最简单的办法就是使用ulimit命令:

  [speng@as4 ~]$ ulimit -n

  上述命令中,在中指定要设置的单一进程允许打开的最大文件数。如果系统回显类似于"Operation notpermitted"之类的话,说明上述限制修改失败,实际上是因为在中指定的数值超过了Linux系统对该用户打开文件数的软限制或硬限制。因此,就需要修改Linux系统对用户的关于打开文件数的软限制和硬限制。

  第一步,修改/etc/security/limits.conf文件,在文件中添加如下行:

  speng soft nofile 10240

  speng hard nofile 10240

  其中speng指定了要修改哪个用户的打开文件数限制,可用'*'号表示修改所有用户的限制;

  soft或hard指定要修改软限制还是硬限制;10240则指定了想要修改的新的限制值,即最大打开文件数(请注意软限制值要小于或等于硬限制)。修改完后保存文件。

  第二步,修改/etc/pam.d/login文件,在文件中添加如下行:

  session required /lib/security/pam_limits.so 这是告诉Linux在用户完成系统登录后,应该调用pam_limits.so模块来设置系统对该用户可使用的各种资源数量的最大限制(包括用户可打开的最大文件数限制),而pam_limits.so模块就会从/etc/security/limits.conf文件中读取配置来设置这些限制值。修改完后保存此文件。

  第三步,查看Linux系统级的最大打开文件数限制,使用如下命令:

  [speng@as4 ~]$ cat /proc/sys/fs/file-max

  12158

  这表明这台Linux系统最多允许同时打开(即包含所有用户打开文件数总和)12158个文件,是Linux系统级硬限制,所有用户级的打开文件数限制都不应超过这个数值。通常这个系统级硬限制是Linux系统在启动时根据系统硬件资源状况计算出来的最佳的最大同时打开文件数限制,如果没有特殊需要,不应该修改此限制,除非想为用户级打开文件数限制设置超过此限制的值。

  修改此硬限制的方法是修改/etc/rc.local脚本,在脚本中添加如下行:

  echo 22158 > /proc/sys/fs/file-max

  这是让Linux在启动完成后强行将系统级打开文件数硬限制设置为22158.修改完后保存此文件。

  完成上述步骤后重启系统,一般情况下就可以将Linux系统对指定用户的单一进程允许同时打开的最大文件数限制设为指定的数值。如果重启后用 ulimit-n命令查看用户可打开文件数限制仍然低于上述步骤中设置的最大值,这可能是因为在用户登录脚本/etc/profile中使用ulimit -n命令已经将用户可同时打开的文件数做了限制。由于通过ulimit-n修改系统对用户可同时打开文件的最大数限制时,新修改的值只能小于或等于上次 ulimit-n设置的值,因此想用此命令增大这个限制值是不可能的。

  所以,如果有上述问题存在,就只能去打开/etc/profile脚本文件,在文件中查找是否使用了ulimit-n限制了用户可同时打开的最大文件数量,如果找到,则删除这行命令,或者将其设置的值改为合适的值,然后保存文件,用户退出并重新登录系统即可。 通过上述步骤,就为支持高并发TCP连接处理的通讯处理程序解除关于打开文件数量方面的系统限制。

  2、修改网络内核对TCP连接的有关限制

  在Linux上编写支持高并发TCP连接的客户端通讯处理程序时,有时会发现尽管已经解除了系统对用户同时打开文件数的限制,但仍会出现并发TCP连接数增加到一定数量时,再也无法成功建立新的TCP连接的现象。出现这种现在的原因有多种。

  第一种原因可能是因为Linux网络内核对本地端口号范围有限制。此时,进一步分析为什么无法建立TCP连接,会发现问题出在connect()调用返回失败,查看系统错误提示消息是"Can't assign requestedaddress".同时,如果在此时用tcpdump工具监视网络,会发现根本没有TCP连接时客户端发SYN包的网络流量。这些情况说明问题在于本地Linux系统内核中有限制。

  其实,问题的根本原因在于Linux内核的TCP/IP协议实现模块对系统中所有的客户端TCP连接对应的本地端口号的范围进行了限制(例如,内核限制本地端口号的范围为1024~32768之间)。当系统中某一时刻同时存在太多的TCP客户端连接时,由于每个TCP客户端连接都要占用一个唯一的本地端口号(此端口号在系统的本地端口号范围限制中),如果现有的TCP客户端连接已将所有的本地端口号占满,则此时就无法为新的TCP客户端连接分配一个本地端口号了,因此系统会在这种情况下在connect()调用中返回失败,并将错误提示消息设为"Can't assignrequested address".

  有关这些控制逻辑可以查看Linux内核源代码,以linux2.6内核为例,可以查看tcp_ipv4.c文件中如下函数:

  static int tcp_v4_hash_connect(struct sock *sk)

  请注意上述函数中对变量sysctl_local_port_range的访问控制。变量sysctl_local_port_range的初始化则是在tcp.c文件中的如下函数中设置:

  void __init tcp_init(void)

  内核编译时默认设置的本地端口号范围可能太小,因此需要修改此本地端口范围限制。

  第一步,修改/etc/sysctl.conf文件,在文件中添加如下行:

  net.ipv4.ip_local_port_range = 1024 65000

  这表明将系统对本地端口范围限制设置为1024~65000之间。请注意,本地端口范围的最小值必须大于或等于1024;而端口范围的最大值则应小于或等于65535.修改完后保存此文件。

  第二步,执行sysctl命令:

  [speng@as4 ~]$ sysctl -p

  如果系统没有错误提示,就表明新的本地端口范围设置成功。如果按上述端口范围进行设置,则理论上单独一个进程最多可以同时建立60000多个TCP客户端连接。

  第二种无法建立TCP连接的原因可能是因为Linux网络内核的IP_TABLE防火墙对最大跟踪的TCP连接数有限制。此时程序会表现为在 connect()调用中阻塞,如同死机,如果用tcpdump工具监视网络,也会发现根本没有TCP连接时客户端发SYN包的网络流量。由于 IP_TABLE防火墙在内核中会对每个TCP连接的状态进行跟踪,跟踪信息将会放在位于内核内存中的conntrackdatabase中,这个数据库的大小有限,当系统中存在过多的TCP连接时,数据库容量不足,IP_TABLE无法为新的TCP连接建立跟踪信息,于是表现为在connect()调用中阻塞。此时就必须修改内核对最大跟踪的TCP连接数的限制,方法同修改内核对本地端口号范围的限制是类似的:

  第一步,修改/etc/sysctl.conf文件,在文件中添加如下行:

  net.ipv4.ip_conntrack_max = 10240

  这表明将系统对最大跟踪的TCP连接数限制设置为10240.请注意,此限制值要尽量小,以节省对内核内存的占用。

  第二步,执行sysctl命令:

  [speng@as4 ~]$ sysctl -p

  如果系统没有错误提示,就表明系统对新的最大跟踪的TCP连接数限制修改成功。如果按上述参数进行设置,则理论上单独一个进程最多可以同时建立10000多个TCP客户端连接。

  3、使用支持高并发网络I/O的编程技术在Linux上编写高并发TCP连接应用程序时,必须使用合适的网络I/O技术和I/O事件分派机制。可用的I/O技术有同步I/O,非阻塞式同步I/O(也称反应式I/O),以及异步I/O.在高TCP并发的情形下,如果使用同步I/O,这会严重阻塞程序的运转,除非为每个TCP连接的I/O创建一个线程。

  但是,过多的线程又会因系统对线程的调度造成巨大开销。因此,在高TCP并发的情形下使用同步 I/O是不可取的,这时可以考虑使用非阻塞式同步I/O或异步I/O.非阻塞式同步I/O的技术包括使用select(),poll(),epoll等机制。异步I/O的技术就是使用AIO.

  从I/O事件分派机制来看,使用select()是不合适的,因为它所支持的并发连接数有限(通常在1024个以内)。如果考虑性能,poll()也是不合适的,尽管它可以支持的较高的TCP并发数,但是由于其采用"轮询"机制,当并发数较高时,其运行效率相当低,并可能存在I/O事件分派不均,导致部分TCP连接上的I/O出现"饥饿"现象。而如果使用epoll或AIO,则没有上述问题(早期Linux内核的AIO技术实现是通过在内核中为每个 I/O请求创建一个线程来实现的,这种实现机制在高并发TCP连接的情形下使用其实也有严重的性能问题。但在最新的Linux内核中,AIO的实现已经得到改进)。

  综上所述,在开发支持高并发TCP连接的Linux应用程序时,应尽量使用epoll或AIO技术来实现并发的TCP连接上的I/O控制,这将为提升程序对高并发TCP连接的支持提供有效的I/O保证。

  内核参数sysctl.conf的优化

  /etc/sysctl.conf 是用来控制linux网络的配置文件,对于依赖网络的程序(如web服务器和cache服务器)非常重要,RHEL默认提供的最好调整。

  推荐配置(把原/etc/sysctl.conf内容清掉,把下面内容复制进去):

  net.ipv4.ip_local_port_range = 1024 65536

  net.core.rmem_max=16777216

  net.core.wmem_max=16777216

  net.ipv4.tcp_rmem=4096 87380 16777216

  net.ipv4.tcp_wmem=4096 65536 16777216

  net.ipv4.tcp_fin_timeout = 10

  net.ipv4.tcp_tw_recycle = 1

  net.ipv4.tcp_timestamps = 0

  net.ipv4.tcp_window_scaling = 0

  net.ipv4.tcp_sack = 0

  net.core.netdev_max_backlog = 30000

  net.ipv4.tcp_no_metrics_save=1

  net.core.somaxconn = 262144

  net.ipv4.tcp_syncookies = 0

  net.ipv4.tcp_max_orphans = 262144

  net.ipv4.tcp_max_syn_backlog = 262144

  net.ipv4.tcp_synack_retries = 2

  net.ipv4.tcp_syn_retries = 2

  这个配置参考于cache服务器varnish的推荐配置和SunOne 服务器系统优化的推荐配置。

  varnish调优推荐配置的地址为:http://varnish.projects.linpro.no/wiki/Performance

  不过varnish推荐的配置是有问题的,实际运行表明"net.ipv4.tcp_fin_timeout = 3"的配置会导致页面经常打不开;并且当网友使用的是IE6浏览器时,访问网站一段时间后,所有网页都会

  打不开,重启浏览器后正常。可能是国外的网速快吧,我们国情决定需要调整"net.ipv4.tcp_fin_timeout = 10",在10s的情况下,一切正常(实际运行结论)。

  修改完毕后,执行:

  /sbin/sysctl -p /etc/sysctl.conf

  /sbin/sysctl -w net.ipv4.route.flush=1

  命令生效。为了保险起见,也可以reboot系统。

  调整文件数:

  linux系统优化完网络必须调高系统允许打开的文件数才能支持大的并发,默认1024是远远不够的。

  执行命令:

  Shell代码

  echo ulimit -HSn 65536 》 /etc/rc.local

  echo ulimit -HSn 65536 》/root/.bash_profile

  ulimit -HSn 65536

posted @ 2013-11-15 20:00 ivaneeo 阅读(503) | 评论 (0)编辑 收藏

如何设置呢,官方是这样的:

第一步:配置/etc/security/limits.conf

sudo vim /etc/security/limits.conf 文件尾追加  * hard nofile 40960 * soft nofile 40960
4096可以自己设置,四列参数的设置见英文,简单讲一下:

第一列,可以是用户,也可以是组,要用@group这样的语法,也可以是通配符如*%

第二列,两个值:hard,硬限制,soft,软件限制,一般来说soft要比hard小,hard是底线,决对不能超过,超过soft报警,直到hard数

第三列,见列表,打开文件数是nofile

第四列,数量,这个也不能设置太大


第二步:/etc/pam.d/su(官方)或/etc/pam.d/common-session(网络)

sudo vim /etc/pam.d/su 将 pam_limits.so 这一行注释去掉  重起系统
sudo vim /etc/pam.d/common-session 加上以下一行 session required pam_limits.so 

打开/etc/pam.d/su,发现是包含/etc/pam.d/common-session这个文件的,所以修改哪个文件都应该是可以的

我的修改是在/etc/pam.d/common-session文件中进行的。


官方只到第二步,就重启系统了,没有第三步,好象不行,感觉是不是全是第三步的作用?!

第三步:配置/etc/profile

最后一行加上

ulimit -SHn 40960

重启,ulimit -n 验证,显示40960就没问题了


注意以上三步均是要使用root权限进行修改。

posted @ 2013-11-15 19:58 ivaneeo 阅读(3557) | 评论 (0)编辑 收藏

/etc/sysctl.conf

net.core.rmem_default = 25696000
net.core.rmem_max = 25696000
net.core.wmem_default = 25696000
net.core.wmem_max = 25696000
net.ipv4.tcp_timestamps = 0
net.ipv4.tcp_sack =1
net.ipv4.tcp_window_scaling = 1

hotrod方式:
nohup ./bin/startServer.sh -r hotrod -l 172.20.21.1 -c ./etc/config-samples/distributed-udp.xml -p 11222 &
posted @ 2013-11-14 17:56 ivaneeo 阅读(433) | 评论 (0)编辑 收藏

From a convenience perspective, I want to authenticate as infrequently as possible. However, security requirements suggest that I should be authenticated for all sorts of services. This means that Single Sign On and forwardable authentication credentials would be useful.

Within an individual organisation at least, it is useful and fairly straightforward to have centralised control for authentication services. More and more authorisation and applications services are able to use centralised authentication services such as Kerberos.

This document will demonstrate how to configure a machine running OpenSSH server to use GSSAPI so that users can log in if they have authorised kerberos tickets. This is not the place for extensive explanations about tickets or how to set up the Key Distribution Center(KDC) in the first place or how to build or install the necessary software on various different unixlike systems. Likely, your distribution's package management system can provide you with what you need.

Kerberos

All destination machines should have /etc/krb5.conf modified to allow forwardable tickets:

[libdefaults]     default_realm = ALLGOODBITS.ORG     forwardable = TRUE [realms]     ALLGOODBITS.ORG = {                     kdc = kerberos.allgoodbits.org                     } 

Using kadmin, create a principal for a user:

kadmin> ank <username>@<REALM> 

Here the process differs depending upon whether you're using MIT Kerberos (probably) or Heimdal.

MIT

Create a principal for the host:

kadmin> ank -randkey host/<FQDN>@<REALM> 

Extract the key for the host principal to a keytab file and locate it correctly on the ssh server:

kadmin> ktadd -k /tmp/<FQDN>.keytab host/<FQDN> 

Heimdal

Create a principal for the host:

kadmin> ank -r host/<FQDN>@<REALM> 

Extract the key for the host principal to a keytab file and locate it correctly on the ssh server:

kadmin> ext -k /tmp/<FQDN>.keytab host/<FQDN>@<REALM> 

SSH

Then we need to take the keytab file into which you extracted the key for the host principal and copy it to the location on the ssh server where sshd will look for it, probably /etc/krb5.keytab.

We need to configure sshd_config(5). The important options start with GSSAPI, not to be confused with the Kerberos options which are merely for KDC-validated password authentication; the GSSAPI method allows authentication and login based upon existing tickets. In other words, the "Kerberos" method requires you to enter your password (again), GSSAPI will allow login based on upon the tickets you already have.

sshd_config:

GSSAPIAuthentication yes GSSAPICleanupCredentials yes PermitRootLogin without-password 

ssh_config:

GSSAPIAuthentication yes GSSAPIDelegateCredentials yes 

PAM

Linux Pluggable Authentication Modules(PAM) provide a common framework for authentication/authorisation for applications.

/etc/pam.d/common-account:

account sufficient      pam_krb5.so     use_first_pass 

/etc/pam.d/common-auth:

auth    sufficient      pam_krb5.so     use_first_pass 

/etc/pam.d/common-password:

password        sufficient      pam_krb5.so 

/etc/pam.d/common-session:

session optional      pam_krb5.so 

This is sufficient to allow OpenAFS based home directories because although AFS uses Kerberosv4, MIT Kerberos does 5 to 4 ticket conversion on the fly.

Troubleshooting

  • As with anything concerned with kerberos, make sure you have NTP and DNS working properly before you even start.
  • ssh -v can give you a lot of valuable information.
  • read your logs.
posted @ 2013-10-12 18:12 ivaneeo 阅读(309) | 评论 (0)编辑 收藏

nohup ./bin/startServer.sh -r hotrod -l 172.20.21.3 -c ./etc/config-samples/distributed-udp.xml -p 11222 &
posted @ 2013-10-08 10:49 ivaneeo 阅读(435) | 评论 (0)编辑 收藏

https://github.com/sksamuel/elasticsearch-river-hazelcast
posted @ 2013-10-08 00:57 ivaneeo 阅读(399) | 评论 (0)编辑 收藏

ElasticSearch是一个基于Lucene构建的开源,分布式,RESTful搜索引擎。设计用于云计算中,能够达到实时搜索,稳定,可靠,快速,安装使用方便。支持通过HTTP使用JSON进行数据索引。 

  我们建立一个网站或应用程序,并要添加搜索功能,令我们受打击的是:搜索工作是很难的。我们希望我们的搜索解决方案要快,我们希望 有一个零配置和一个完全免费的搜索模式,我们希望能够简单地使用JSON通过HTTP的索引数据,我们希望我们的搜索服务器始终可用,我们希望能够一台开 始并扩展到数百,我们要实时搜索,我们要简单的多租户,我们希望建立一个云的解决方案。Elasticsearch旨在解决所有这些问题和更多的。

安装

  以windows操作系统和ES0.19.7版本为例:

 

  ①下载elasticsearch-0.19.7.zip

 

  ②直接解压至某目录,设置该目录为ES_HOME环境变量

 

  ③安装JDK,并设置JAVA_HOME环境变量

 

  ④在windows下,运行 %ES_HOME%\bin\elasticsearch.bat即可运行

分布式搜索elasticsearch单机与服务器环境搭建

      先到http://www.elasticsearch.org/download/下 载最新版的elasticsearch运行包,本文写时最新的是0.19.1,作者是个很勤快的人,es的更新很频繁,bug修复得很快。下载完解开有三 个包:bin是运行的脚本,config是设置文件,lib是放依赖的包。如果你要装插件的话就要多新建一个plugins的文件夹,把插件放到这个文件 夹中。

1.单机环境:

单机版的elasticsearch运行很简单,linux下直接 bin/elasticsearch就运行了,windows运行bin/elasticsearch.bat。如果是在局域网中运行elasticsearch集群也是很简单的,只要cluster.name设置一致,并且机器在同一网段下,启动的es会自动发现对方,组成集群。

2.服务器环境:

如果是在服务器上就可以使用elasticsearch-servicewrapper这个es插件,它支持通过参数,指定是在后台或前台运行es,并且支持启动,停止,重启es服务(默认es脚本只能通过ctrl+c关闭es)。使用方法是到https://github.com/elasticsearch/elasticsearch-servicewrapper下载service文件夹,放到es的bin目录下。下面是命令集合:
bin/service/elasticsearch +
console 在前台运行es
start 在后台运行es
stop 停止es
install 使es作为服务在服务器启动时自动启动
remove 取消启动时自动启动

在service目录下有个elasticsearch.conf配置文件,主要是设置一些java运行环境参数,其中比较重要的是下面的

参数:

#es的home路径,不用用默认值就可以
set.default.ES_HOME=<Path to ElasticSearch Home>

#分配给es的最小内存
set.default.ES_MIN_MEM=256

#分配给es的最大内存
set.default.ES_MAX_MEM=1024


# 启动等待超时时间(以秒为单位)
wrapper.startup.timeout=300

# 关闭等待超时时间(以秒为单位)

wrapper.shutdown.timeout=300

# ping超时时间(以秒为单位)

wrapper.ping.timeout=300

安装插件

  以head插件为例:

 

  联网时,直接运行%ES_HOME%\bin\plugin -install mobz/elasticsearch-head

 

  不联网时,下载elasticsearch-head的zipball的master包,把内容解压到%ES_HOME%\plugin\head\_site目录下,[该插件为site类型插件]

 

  安装完成,重启服务,在浏览器打开 http://localhost:9200/_plugin/head/ 即可

ES概念

  cluster

 

  代表一个集群,集群中有多个节点,其中有一个为主节点,这个主节点是可以通过选举产生的,主从节点是对于集群内部来说 的。es的一个概念就是去中心化,字面上理解就是无中心节点,这是对于集群外部来说的,因为从外部来看es集群,在逻辑上是个整体,你与任何一个节点的通 信和与整个es集群通信是等价的。

 

  shards

 

  代表索引分片,es可以把一个完整的索引分成多个分片,这样的好处是可以把一个大的索引拆分成多个,分布到不同的节点上。构成分布式搜索。分片的数量只能在索引创建前指定,并且索引创建后不能更改。

 

  replicas

 

  代表索引副本,es可以设置多个索引的副本,副本的作用一是提高系统的容错性,当个某个节点某个分片损坏或丢失时可以从副本中恢复。二是提高es的查询效率,es会自动对搜索请求进行负载均衡。

 

  recovery

 

  代表数据恢复或叫数据重新分布,es在有节点加入或退出时会根据机器的负载对索引分片进行重新分配,挂掉的节点重新启动时也会进行数据恢复。

 

  river

 

  代表es的一个数据源,也是其它存储方式(如:数据库)同步数据到es的一个方法。它是以插件方式存在的一个es服 务,通过读取river中的数据并把它索引到es中,官方的river有couchDB的,RabbitMQ的,Twitter的,Wikipedia 的。

 

  gateway

 

  代表es索引的持久化存储方式,es默认是先把索引存放到内存中,当内存满了时再持久化到硬盘。当这个es集群关闭再 重新启动时就会从gateway中读取索引数据。es支持多种类型的gateway,有本地文件系统(默认),分布式文件系统,Hadoop的HDFS和 amazon的s3云存储服务。

 

  discovery.zen

 

  代表es的自动发现节点机制,es是一个基于p2p的系统,它先通过广播寻找存在的节点,再通过多播协议来进行节点之间的通信,同时也支持点对点的交互。

 

  Transport

 

  代表es内部节点或集群与客户端的交互方式,默认内部是使用tcp协议进行交互,同时它支持http协议(json格式)、thrift、servlet、memcached、zeroMQ等的传输协议(通过插件方式集成)。

分布式搜索elasticsearch中文分词集成

elasticsearch官方只提供smartcn这个中文分词插件,效果不是很好,好在国内有medcl大神(国内最早研究es的人之一)写的两个中文分词插件,一个是ik的,一个是mmseg的,下面分别介绍下两者的用法,其实都差不多的,先安装插件,命令行:
安装ik插件:

plugin -install medcl/elasticsearch-analysis-ik/1.1.0  

下载ik相关配置词典文件到config目录

  1. cd config  
  2. wget http://github.com/downloads/medcl/elasticsearch-analysis-ik/ik.zip --no-check-certificate  
  3. unzip ik.zip  
  4. rm ik.zip  

安装mmseg插件:

  1. bin/plugin -install medcl/elasticsearch-analysis-mmseg/1.1.0  

下载相关配置词典文件到config目录

  1. cd config  
  2. wget http://github.com/downloads/medcl/elasticsearch-analysis-mmseg/mmseg.zip --no-check-certificate  
  3. unzip mmseg.zip  
  4. rm mmseg.zip  

分词配置

ik分词配置,在elasticsearch.yml文件中加上

  1. index:  
  2.   analysis:                     
  3.     analyzer:        
  4.       ik:  
  5.           alias: [ik_analyzer]  
  6.           type: org.elasticsearch.index.analysis.IkAnalyzerProvider  

  1. index.analysis.analyzer.ik.type : “ik”  

这两句的意义相同
mmseg分词配置,也是在在elasticsearch.yml文件中

  1. index:  
  2.   analysis:  
  3.     analyzer:  
  4.       mmseg:  
  5.           alias: [news_analyzer, mmseg_analyzer]  
  6.           type: org.elasticsearch.index.analysis.MMsegAnalyzerProvider  

  1. index.analysis.analyzer.default.type : "mmseg"  

mmseg分词还有些更加个性化的参数设置如下

  1. index:  
  2.   analysis:  
  3.     tokenizer:  
  4.       mmseg_maxword:  
  5.           type: mmseg  
  6.           seg_type: "max_word"  
  7.       mmseg_complex:  
  8.           type: mmseg  
  9.           seg_type: "complex"  
  10.       mmseg_simple:  
  11.           type: mmseg  
  12.           seg_type: "simple"  

这样配置完后插件安装完成,启动es就会加载插件。

定义mapping

在添加索引的mapping时就可以这样定义分词器

  1. {  
  2.    "page":{  
  3.       "properties":{  
  4.          "title":{  
  5.             "type":"string",  
  6.             "indexAnalyzer":"ik",  
  7.             "searchAnalyzer":"ik"  
  8.          },  
  9.          "content":{  
  10.             "type":"string",  
  11.             "indexAnalyzer":"ik",  
  12.             "searchAnalyzer":"ik"  
  13.          }  
  14.       }  
  15.    }  
  16. }  

indexAnalyzer为索引时使用的分词器,searchAnalyzer为搜索时使用的分词器。

java mapping代码如下:

  1. XContentBuilder content = XContentFactory.jsonBuilder().startObject()  
  2.         .startObject("page")  
  3.           .startObject("properties")         
  4.             .startObject("title")  
  5.               .field("type", "string")             
  6.               .field("indexAnalyzer", "ik")  
  7.               .field("searchAnalyzer", "ik")  
  8.             .endObject()   
  9.             .startObject("code")  
  10.               .field("type", "string")           
  11.               .field("indexAnalyzer", "ik")  
  12.               .field("searchAnalyzer", "ik")  
  13.             .endObject()       
  14.           .endObject()  
  15.          .endObject()  
  16.        .endObject()  

定义完后操作索引就会以指定的分词器来进行分词。

 附:

ik分词插件项目地址:https://github.com/medcl/elasticsearch-analysis-ik

mmseg分词插件项目地址:https://github.com/medcl/elasticsearch-analysis-mmseg

如果觉得配置麻烦,也可以下载个配置好的es版本,地址如下:https://github.com/medcl/elasticsearch-rtf

 

elasticsearch的基本用法


最大的特点: 
1. 数据库的 database, 就是  index 
2. 数据库的 table,  就是 tag 
3. 不要使用browser, 使用curl来进行客户端操作.  否则会出现 java heap ooxx... 

curl:  -X 后面跟 RESTful :  GET, POST ... 
-d 后面跟数据。 (d = data to send) 

1. create:  

指定 ID 来建立新记录。 (貌似PUT, POST都可以) 
$ curl -XPOST localhost:9200/films/md/2 -d ' 
{ "name":"hei yi ren", "tag": "good"}' 

使用自动生成的 ID 建立新纪录: 
$ curl -XPOST localhost:9200/films/md -d ' 
{ "name":"ma da jia si jia3", "tag": "good"}' 

2. 查询: 
2.1 查询所有的 index, type: 
$ curl localhost:9200/_search?pretty=true 

2.2 查询某个index下所有的type: 
$ curl localhost:9200/films/_search 

2.3 查询某个index 下, 某个 type下所有的记录: 
$ curl localhost:9200/films/md/_search?pretty=true 

2.4 带有参数的查询:  
$ curl localhost:9200/films/md/_search?q=tag:good 
{"took":7,"timed_out":false,"_shards":{"total":5,"successful":5,"failed":0},"hits":{"total":2,"max_score":1.0,"hits":[{"_index":"film","_type":"md","_id":"2","_score":1.0, "_source" : 
{ "name":"hei yi ren", "tag": "good"}},{"_index":"film","_type":"md","_id":"1","_score":0.30685282, "_source" : 
{ "name":"ma da jia si jia", "tag": "good"}}]}} 

2.5 使用JSON参数的查询: (注意 query 和 term 关键字) 
$ curl localhost:9200/film/_search -d ' 
{"query" : { "term": { "tag":"bad"}}}' 

3. update  
$ curl -XPUT localhost:9200/films/md/1 -d { ...(data)... } 

4. 删除。 删除所有的: 
$ curl -XDELETE localhost:9200/films
posted @ 2013-10-04 02:09 ivaneeo 阅读(16733) | 评论 (0)编辑 收藏

仅列出标题
共67页: First 上一页 6 7 8 9 10 11 12 13 14 下一页 Last