linux下使用redis代理twmproxy
twemproxy,又名nutracker,是一個(gè)快速,輕量級(jí)的代理程序,支持memcache 和redis協(xié)議。它主要用來(lái)降低后端緩存服務(wù)器的連接數(shù)。
目前的***版本為0.3.0.git地址:https://github.com/yuyunliuhen/twemproxy。
編譯:
- $get clone https://github.com/yuyunliuhen/twemproxy
- $cd twemproxy & ./configure --enable-debug=log & make
- $src/nutcracker --help
這樣就已經(jīng)編譯成功了,這里從源代碼目錄啟動(dòng),也可以通過(guò)make install 安裝。
命令行選項(xiàng):
- -t, --test-conf : test configuration for syntax errors and exit
測(cè)試配置文件是否正確,默認(rèn)為conf/nutcracker.yml,詳細(xì)內(nèi)容是這樣的:
#p#
配置文件各項(xiàng)參數(shù)的說(shuō)明下面會(huì)進(jìn)行詳細(xì)的介紹;
- -d, --daemonize : run as a daemon
后臺(tái)啟動(dòng)進(jìn)程
- -D, --describe-stats : print stats description and exit
- -v, --verbosity=N : set logging level (default: 5, min: 0, max: 11)
默認(rèn)情況下debug日志是不可用的,不過(guò)在生產(chǎn)環(huán)境中打開(kāi)debug日志并將日志級(jí)別設(shè)置為verbosity,LOG_INFO(-v 6 或者 –verbosity=6);
默認(rèn)日志級(jí)別:
日志級(jí)別-v 6 或者 –verbosity=6:
在這個(gè)日志級(jí)別里,nutcracker 日志記錄了每個(gè)客戶端和服務(wù)端的生存周期和一些像節(jié)點(diǎn)從hash環(huán)上淘汰等重要信息.
打開(kāi)debug日志的方法是在編譯的時(shí)候加上--enable-debug=log選項(xiàng).
- -o, --output=S : set logging file (default: stderr)
- -c, --conf-file=S : set configuration file (default: conf/nutcracker.yml)
如果不指定此選項(xiàng),默認(rèn)為conf/nutcracker.yml,我們也可以設(shè)定自己的配置文件;
- -s, --stats-port=N : set stats monitoring port (default: 22222)
- -a, --stats-addr=S : set stats monitoring ip (default: 0.0.0.0)
指定啟動(dòng)地址,默認(rèn)為0.0.0.0;
- -i, --stats-interval=N : set stats aggregation interval in msec (default: 30000 msec)
- -p, --pid-file=S : set pid file (default: off)
- -m, --mbuf-size=N : set size of mbuf chunk in bytes (default: 16384 bytes)
#p#
配置文件參數(shù):
- listen: The listening address and port (name:port or ip:port) for this server pool.
nutcracker服務(wù)器池監(jiān)聽(tīng)端口和地址;
- hash: The name of the hash function
hash函數(shù),支持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的一部分來(lái)計(jì)算hash值,當(dāng)hash tages存在的時(shí)候 ,我們使用在標(biāo)簽內(nèi)的key的一部分來(lái)構(gòu)建一致性hash,其他情況,我們使用全部的key去構(gòu)建。hash tags能夠讓你將不同的key(只要在標(biāo)簽內(nèi)的部分相同)映射到相同的服務(wù)器上。
例如,服務(wù)池beta的配置如下,指定了兩個(gè)hash_tag字符“{}”,這意味著keys”user:{user1}:ids” 和 “user:{user1}:tweets” 將會(huì)映射到同一臺(tái)服務(wù)器上,因?yàn)槲覀兪褂玫氖?rsquo;user1′來(lái)計(jì)算hash,對(duì)于key “user:user1:ids”將會(huì)是用整個(gè)字符串來(lái)計(jì)算hash,所以可能會(huì)映射到其他服務(wù)器上。
- 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的每一個(gè)服務(wù)池配置timeout 要比僅僅依靠客戶端超時(shí)要好的多。
比如 :timeout: 400
僅僅依靠客戶端超時(shí)設(shè)置并不能達(dá)到理想的超時(shí)效果,反而起到了相反的作用,因?yàn)榭蛻舳说某瑫r(shí)設(shè)置在這里變成了客戶端對(duì)代理的超時(shí),但代理對(duì)服務(wù)端的鏈接是一直保持的,客戶端重試請(qǐng)求對(duì)于服務(wù)端是沒(méi)有效果的。默認(rèn)情況下,任何發(fā)送給服務(wù)端的請(qǐng)求,nutcracker都會(huì)無(wú)限期的等待,當(dāng)timeout被設(shè)置后,如果在timeout的時(shí)間過(guò)后還沒(méi)有從服務(wù)端得到回應(yīng),這時(shí)會(huì)將超時(shí)錯(cuò)誤信息SERVER_ERROR Connection time out發(fā)送給客戶端。
- 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的設(shè)計(jì)意圖是通過(guò)少量的服務(wù)端的鏈接來(lái)響應(yīng)更多的客戶端的鏈接,但是需要注意的是當(dāng)twemproxy配置了 server_connections: > 1時(shí),情況就不一定是這樣的。
為了說(shuō)明這一點(diǎn),假設(shè)在twemproxy 配置為server_connections: 2的場(chǎng)景下,一個(gè)客戶端發(fā)出了以set foo 0 0 3\r\nbar\r\n(寫(xiě))為開(kāi)始,然后第二個(gè)命令是get foo\r\n (讀)的piplined請(qǐng)求,期望的結(jié)果是讀取foo的時(shí)候能夠返回bar,然而,由于配置了兩個(gè)服務(wù)端鏈接,讀寫(xiě)請(qǐng)求可能被發(fā)送到不同的鏈接上,也意味著他們的執(zhí)行順序要看哪一個(gè)先到達(dá)服務(wù)端,總結(jié)一下,如果客戶端期望得到的是我***寫(xiě)的內(nèi)容,需要將twemproxy配置為 server_connections:1或者客戶端只發(fā)起同步的請(qǐng)求。
- 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上代理的服務(wù)實(shí)例可以通過(guò)兩種字符串格式指定‘host:port:weight’ 或者 ‘host:port:weight name’.
或者
在前面的配置中,keys是直接由‘host:port:weight’三重映射而來(lái),而在后者keys是由節(jié)點(diǎn)名映射而來(lái),節(jié)點(diǎn)名和主機(jī)的地址和端口對(duì)應(yīng),后者的方法可以使我們更自由地在不打亂hash環(huán)的情況下重置節(jié)點(diǎn)后端實(shí)例。在auto_eject_hosts設(shè)置為false的情況下,達(dá)到理想的配置。 了解詳細(xì)issue 25
需要注意的是當(dāng)使用節(jié)點(diǎn)名來(lái)構(gòu)建一致性hash環(huán)的時(shí)候,twemproxy將會(huì)忽略’host:port:weight name’這種格式的字符串中權(quán)重的值。
測(cè)試使用:
(1) 配置一份配置文件:
(2) 按照servers的配置分別在對(duì)應(yīng)服務(wù)器上啟動(dòng)redis-server;
(3) 啟動(dòng)nutcracker
日志顯示redis pool代理4 servers.
#p#
(4) 連接nutcracker,并設(shè)置獲取鍵值
各個(gè)redis服務(wù)器的監(jiān)視情況:
nutcracker日志:
以上只是展示了一主機(jī)多實(shí)例的情況,如果是多主機(jī)多實(shí)例,只需要更改對(duì)應(yīng)IP就行了。
參考:twemproxy/recommendation.md(翻譯)twemproxy的一些生產(chǎn)環(huán)境使用經(jīng)驗(yàn)