魏长东

weichangdong

nginx-push-stream-module 使用

安装完成之后,实现了我一直以来想实现的效果。浏览器上显示了推送的消息。不是浏览器主动请求触发的。

大体流程这样的,现在浏览器上访问一个地址,然后这个请求应该是一直没断。然后我在服务器发送一条消息,浏览器就显示了这条消息。好神奇啊,有木有~~~~

web-push.png

下面给出安装的过程

»»阅读全文

php 科学计数法还原

有时候需要用php的microtime做唯一的id,但是生成之后,显示出来就成科学记数法了,用下面代码可以还原。
function NumToStr($num){ 
    if (stripos($num,'e')===false) return $num; 
   // $num = trim(preg_replace('/[=\'"]/','',$num,1),'"');//出现科学计数法,还原成字符串 
    $result = ""; 
    while ($num > 0){ 
        $v = $num - floor($num / 10)*10; 
        $num = floor($num / 10); 
        $result   =   $v . $result; 
    }//开源代码phpfensi.com 
    return $result; 
}

redis查看使用了多少内存

# Memory
used_memory:1147092504
used_memory_human:1.07G //使用了多少
used_memory_rss:1186713600
used_memory_rss_human:1.11G
used_memory_peak:5887659864
used_memory_peak_human:5.48G
total_system_memory:8374919168
total_system_memory_human:7.80G//redis总共可用内存大小
used_memory_lua:37888
used_memory_lua_human:37.00K

Nginx 的 Echo 模块 —— echo-nginx-module

这个模块不包含在 Nginx 源码中,安装方法:

1. 首先下载模块源码:https://github.com/agentzh/echo-nginx-module/tags
2. 解压到某个路径,假设为 /path/to/echo-nginx-module
3. 使用下面命令编译并安装 Nginx

$ wget 'http://sysoev.ru/nginx/nginx-1.0.11.tar.gz'
$ tar -xzvf nginx-1.0.11.tar.gz
$ cd nginx-1.0.11/
 
# Here we assume you would install you nginx under /opt/nginx/.
$ ./configure --prefix=/opt/nginx \
--add-module=/path/to/echo-nginx-module
 
$ make -j2
$ make install

»»阅读全文

nginx+lua+redis三国演义

今天看到redis2_query  redis2_pass,然后网上一看,原来又是nginx的扩展。

以下copy 网路,自己还没有真正搞过。

redis2-nginx-module 是一个支持 Redis 2.0 协议的 Nginx upstream 模块,它可以让 Nginx 以非阻塞方式直接防问远方的 Redis 服务,同时支持 TCP 协议和 Unix Domain Socket 模式,并且可以启用强大的 Redis 连接池功能。

安装lua
# apt-get install lua5.1
# apt-get install liblua5.1-dev
# apt-get install liblua5.1-socket2
安装nginx
# apt-get install git-core
# git clone https://github.com/simpl/ngx_devel_kit.git
# git clone https://github.com/chaoslawful/lua-nginx-module.git
# git clone https://github.com/agentzh/redis2-nginx-module.git
# git clone https://github.com/agentzh/set-misc-nginx-module.git
# git clone https://github.com/agentzh/echo-nginx-module.git
# git clone https://github.com/catap/ngx_http_upstream_keepalive.git
# apt-get install libpcre3 libpcre3-dev libltdl-dev libssl-dev libjpeg62 libjpeg62-dev libpng12-0
 libpng12-dev 
libxml2-dev libcurl4-openssl-dev libmcrypt-dev autoconf 
libxslt1-dev libgd2-noxpm-dev libgeoip-dev libperl-dev -y
# wget http://nginx.org/download/nginx-1.0.8.tar.gz
# tar zxvf nginx-1.0.8.tar.gz
# cd nginx-1.0.8
# ./configure --prefix=/usr/local/nginx --with-debug --with-http_addition_module \
--with-http_dav_module --with-http_flv_module --with-http_geoip_module \
--with-http_gzip_static_module --with-http_image_filter_module --with-http_perl_module \
--with-http_random_index_module --with-http_realip_module --with-http_secure_link_module \
--with-http_stub_status_module --with-http_ssl_module --with-http_sub_module \
--with-http_xslt_module --with-ipv6 --with-sha1=/usr/include/openssl \
--with-md5=/usr/include/openssl --with-mail --with-mail_ssl_module \
--add-module=../ngx_devel_kit \
--add-module=../echo-nginx-module \
--add-module=../lua-nginx-module \
--add-module=../redis2-nginx-module \
--add-module=../ngx_http_upstream_keepalive \
--add-module=../set-misc-nginx-module
# make
# make install
安装lua-redis-parser


# git clone https://github.com/agentzh/lua-redis-parser.git
# export LUA_INCLUDE_DIR=/usr/include/lua5.1
# make CC=gcc
# make install CC=gcc

安装json
# wget http://files.luaforge.net/releases/json/json/0.9.50/json4lua-0.9.50.zip
# unzip json4lua-0.9.50.zip
# cp json4lua-0.9.50/json/json.lua /usr/share/lua/5.1/
安装redis-lua

# git clone https://github.com/nrk/redis-lua.git
# cp redis-lua/src/redis.lua /usr/share/lua/5.1/

»»阅读全文

lua html模板(win)

set $template_location "/html";   
	set $template_root "D:/openresty/html/html";
	

	
	location /html_html {  
		default_type 'text/html';  
		content_by_lua_file "html/html/test_template_1.lua";   
	}  
	
	location /html_html2 {  
		default_type 'text/html';  
		content_by_lua_file "html/html/test_template_2.lua";   
	} 
	
		
	location /html_html3 {  
		default_type 'text/html';  
		charset utf-8;
		#ngx.header['Content-Type']="text/html;charset=UTF-8"
		content_by_lua_file "html/html/test_template_3.lua";   
	} 

»»阅读全文

Nginx Push Stream Module

https://www.nginx.com/resources/wiki/modules/push_stream/

有时间研究下。

elastic search扫描模式

function makeLangAction()
    {
        $es = self::connectEs();
        $params = array(  
            "search_type" => "scan",
            "scroll" => "30s",
            "size" => 50,
            "index" => "360weather",  
            "body" => array(  
                "query" => array(  
                    "match_all" => array()  
                )  
            )  
        );  
          
        $docs = $es->search($params);
        $scroll_id = $docs['_scroll_id'];
        $redis = new RedisModel('write');
        echo "all num ".$docs['hits']['total']."\n";
        $num = 0;
        $all_tz = [];
        while (true) {   
            $response = $es->scroll(  
                array(  
                    "scroll_id" => $scroll_id,
                    "scroll" => "30s",
                )  
            );  
            if (count($response['hits']['hits']) > 0) {  
                //loop $response['hits']['hits'] data
                $scroll_id = $response['_scroll_id'];
                //$num += count($response['hits']['hits']); 
                foreach ($response['hits']['hits'] as $tmp) {
                    $tt = explode('_', $tmp['_id']);
                    $loc_id = $tt[0];
                    $data = $tmp['_source'];
                    $lang = $data['lang'];
                    $lang_info = json_encode([
                        'area' => $data['country_code'],
                        'province' => $data['province'],
                        'name' => $data['city'],
                        'tz' => $data['tz'],
                    ]);
                    $tz_num = Functions::computerTz($data['tz']);
                    /*
                    if (isset($all_tz[$data['tz']])) {
                        
                    } else {
                        $all_tz[$data['tz']] = $tz_num;
                    }
                    */
                    $redis->hset(self::REDIS_CITY_ID_WEATHER.$loc_id,self::REDIS_CITY_LANG.$lang,$lang_info);
                    $redis->hset(self::REDIS_CITY_ID_WEATHER.$loc_id,'tz',$tz_num);
                    $num++;
                } 
            } else {   
                break;  
            }  
        }
        
        echo 'tz num '.count($all_tz)."\n";
        print_R($all_tz);
        
        echo "set num ". $num ."\n";  
    }

git 分支

最近天气项目,测试和正式环境使用两套代码(主要是测试环境测试人员需要保留方便测试的接口,数据传输没有加密)。所以用git创建一个新的branch,通过jenkins配置,
 

测试项目用test的branch,online项目用master。用到的命令大概有
查看本地、远程、所有分支 git branch、git branch -r、git branch -a
创建分支 git branch test
删除本地、远程分支   git branch -d test,git branch -d -r test 删除的时候要是有报错,就加-D
重命名 git branch -m old_name new_name
推送到分支 git push origin test  
切换分之 git checkout test