linux下使用redis代理twmproxy(1)(3)
配置文件参数:
- listen: The listening address and port (name:port or ip:port) for this server pool.
nutcracker服务器池监听端口和地址;
- hash: The name of the hash function
hash函数,支持md5,crc16,crc32,finv1a_32等十多种;
- hash_tag: A two character string that specifies the part of the key used for hashing. Eg "{}" or "$$". Hash tagenable mapping different keys to the same server as long as the part of the key within the tag is the same.
启用hash tags 意味着你将使用key的一部分来计算hash值,当hash tages存在的时候 ,我们使用在标签内的key的一部分来构建一致性hash,其他情况,我们使用全部的key去构建。hash tags能够让你将不同的key(只要在标签内的部分相同)映射到相同的服务器上。
例如,服务池beta的配置如下,指定了两个hash_tag字符“{}”,这意味着keys”user:{user1}:ids” 和 “user:{user1}:tweets” 将会映射到同一台服务器上,因为我们使用的是’user1′来计算hash,对于key “user:user1:ids”将会是用整个字符串来计算hash,所以可能会映射到其他服务器上。
- timeout: The timeout value in msec that we wait for to establish a connection to the server or receive a response from a server. By default, we wait indefinitely.
为nutcracker的每一个服务池配置timeout 要比仅仅依靠客户端超时要好的多。
比如 :timeout: 400
仅仅依靠客户端超时设置并不能达到理想的超时效果,反而起到了相反的作用,因为客户端的超时设置在这里变成了客户端对代理的超时,但代理对服务端的链接是一直保持的,客户端重试请求对于服务端是没有效果的。默认情况下,任何发送给服务端的请求,nutcracker都会无限期的等待,当timeout被设置后,如果在timeout的时间过后还没有从服务端得到回应,这时会将超时错误信息SERVER_ERROR Connection time out发送给客户端。
- backlog: The TCP backlog argument. Defaults to 512.
- preconnect: A boolean value that controls if nutcracker should preconnect to all the servers in this pool on process start. Defaults to false.
- redis: A boolean value that controls if a server pool speaks redis or memcached protocol. Defaults to false.
- server_connections: The maximum number of connections that can be opened to each server. By default, we open at most 1 server connection.
twemproxy的设计意图是通过少量的服务端的链接来响应更多的客户端的链接,但是需要注意的是当twemproxy配置了 server_connections: > 1时,情况就不一定是这样的。
为了说明这一点,假设在twemproxy 配置为server_connections: 2的场景下,一个客户端发出了以set foo 0 0 3\r\nbar\r\n写)为开始,然后第二个命令是get foo\r\n (读)的piplined请求,期望的结果是读取foo的时候能够返回bar,然而,由于配置了两个服务端链接,读写请求可能被发送到不同的链接上,也意味着他们的执行顺序要看哪一个先到达服务端,总结一下,如果客户端期望得到的是我最后写的内容,需要将twemproxy配置为 server_connections:1或者客户端只发起同步的请求。
- auto_eject_hosts: A boolean value that controls if server should be ejected temporarily when it fails consecutively server_failure_limit times. See liveness recommendations for information. Defaults to false.
- server_retry_timeout: The timeout value in msec to wait for before retrying on a temporarily ejected server, when auto_eject_host is set to true. Defaults to 30000 msec.
- server_failure_limit: The number of consecutive failures on a server that would lead to it being temporarily ejected when auto_eject_host is set to true. Defaults to 2.
- servers: A list of server address, port and weight (name:port:weight or ip:port:weight) for this server pool.
twemproxy上代理的服务实例可以通过两种字符串格式指定‘host:port:weight’ 或者 ‘host:port:weight name’.
或者
在前面的配置中,keys是直接由‘host:port:weight’三重映射而来,而在后者keys是由节点名映射而来,节点名和主机的地址和端口对应,后者的方法可以使我们更自由地在不打乱hash环的情况下重置节点后端实例。在auto_eject_hosts设置为false的情况下,达到理想的配置。 了解详细issue 25
需要注意的是当使用节点名来构建一致性hash环的时候,twemproxy将会忽略’host:port:weight name’这种格式的字符串中权重的值。
测试使用:
(1) 配置一份配置文件:
(2) 按照servers的配置分别在对应服务器上启动redis-server;
(3) 启动nutcracker
日志显示redis pool代理4 servers.
评论暂时关闭