欢迎各位兄弟 发布技术文章

这里的技术是共享的

You are here

4)ttserver+nginx构建高并发高可用性应用

2012年7月20日
 

ttserver+nginx构建高并发高可用性应用

ttserver一款兼容memcached协议,也可以通过HTTP协议进行数据交换,支持故障转移,高可用性,高并发的分布式key-value持久存储系统。key-value分布式存储系统的特点是查询快,存储数量大,高并发,非常适合通过主键进行查询的操作。

下面的案例是将图片以二进制的方式存入到ttserver中,并通过http方式读取图片。

一.配置nginx

nginx_upstream_check_module模块地址: https://github.com/yaoweibin/nginx_upstream_check_module

nginx需要添加nginx_upstream_check_module模块,用于对后端服务器的健康情况检测,如果后端服务器不可用,则把这台服务器移除负载均衡轮循集群,所有的请求不往这台服务器上转发,待这台服务器恢复正常后,再把这台加入到负载均衡集群。这是LB的基本功能。

# vi nginx.conf

user www-data;

worker_processes 8;

worker_cpu_affinity 00000001 00000010 00000100 00001000 00010000 00100000 01000000 10000000;

error_log /var/log/nginx/error.log crit;

pid /var/run/nginx.pid;

worker_rlimit_nofile 65535;

events {

use epoll;

worker_connections 65535;

}

http {

include /etc/nginx/mime.types;

default_type application/octet-stream;

server_tokens off;

access_log off;

sendfile on;

tcp_nopush on;

keepalive_timeout 0;

tcp_nodelay on;

client_max_body_size 200m;

gzip on;

gzip_min_length 1k;

gzip_buffers 4 16k;

gzip_http_version 1.1;

gzip_comp_level 5;

gzip_disable “MSIE [1-6]\.(?!.*SV1)”;

gzip_types text/plain application/x-javascript text/css application/xml text/javascript;

include /etc/nginx/conf.d/*.conf;

include /etc/nginx/sites-enabled/*;

}

# vi default

server {

listen 80;

server_name 192.168.1.213;

memcached_connect_timeout 1s;

location / {

root /www/web/tmp;

error_page 404 = @fallback;

}

location ~ ^/ttlsa/ttlsa_([0-9a-zA-Z]\.+)$ {

set $memcached_key $1; //memcached键值

add_header X-ttserver-key $memcached_key; //添加一个header信息

memcached_pass ttserver;

memcached_next_upstream error timeout; //当发生错误或超时时,将请求转发到upstream下一个服务器

default_type text/html;

error_page 404 = @fallback;

}

location @fallback {

rewrite ^ http://www.ttlsa.com redirect;

}

}

# vi upstream.conf

upstream ttserver {

server 192.168.1.60:1978;

server 192.168.1.60:1979;

check interval=3000 rise=2 fall=2 timeout=1000; //interval检测周期3s一次,fall宕机标记2次失败后标记不可用

}

二.配置ttserver

需要将ttserver配置成主主结构。

ttserver的介绍,安装,配置参见: http://www.ttlsa.com/html/1220.html

三.测试

1.上传界面

# vi upload.php

<html>

<head>

<meta http-equiv=”Content-Type” content=”text/html; charset=utf-8″/>

<title>Upload Files</title>

</head>

<body>

<h2>Select files to upload</h2>

<form enctype=”multipart/form-data” action=”/store.php” method=”post”>

<input type=”file” name=”file”><br>

<input type=”submit” name=”submit” value=”Upload”>

</form>

</body>

</html>

2.存入ttserver

# vi store.php

[codesyntax lang=”php”]

<?php

/*

###################################

### author: www.ttlsa.com ###

### QQ群: 39514058 ###

### E-mail: service@ttlsa.com ###

############################

普通分类: