1. 程式人生 > 實用技巧 >Java實現隨機生成由字母、數字組合的N位隨機數

Java實現隨機生成由字母、數字組合的N位隨機數

Nginx安裝

注意:這裡以CentOS 6.8伺服器為例,以root使用者身份來安裝Nginx。

1.安裝依賴環境

yum-yinstallwgetgcc-c++ncursesncurses-develcmakemakeperlbisonopensslopenssl-develgcc*libxml2libxml2-develcurl-devellibjpeg*libpng*freetype*autoconfautomakezlib*fiex*libxml*libmcrypt*libtool-ltdl-devel*libaiolibaio-develbzrlibtool

2.安裝openssl

wgethttps://www.openssl.org/source/openssl-1.0.2s.tar.gz
tar-zxvfopenssl-1.0.2s.tar.gz
cd/usr/local/src/openssl-1.0.2s
./config--prefix=/usr/local/openssl-1.0.2s
make
makeinstall

3.安裝pcre

wgethttps://ftp.pcre.org/pub/pcre/pcre-8.43.tar.gz
tar-zxvfpcre-8.43.tar.gz
cd/usr/local/src/pcre-8.43
./configure--prefix=/usr/local/pcre-8.43
make
makeinstall

4.安裝zlib

wgethttps://sourceforge.net/projects/libpng/files/zlib/1.2.11/zlib-1.2.11.tar.gz
tar-zxvfzlib-1.2.11.tar.gz
cd/usr/local/src/zlib-1.2.11
./configure--prefix=/usr/local/zlib-1.2.11
make
make

5.安裝Nginx

wgethttp://nginx.org/download/nginx-1.17.2.tar.gz
tar-zxvfnginx-1.17.2.tar.gz
cd/usr/local/src/nginx-1.17.2
./configure--prefix=/usr/local/nginx-1.17.2--with-openssl=/usr/local/src/openssl-1.0.2s--with-pcre=/usr/local/src/pcre-8.43--with-zlib=/usr/local/src/zlib-1.2.11--with-http_ssl_module
make
makeinstall

這裡需要注意的是:安裝Nginx時,指定的是openssl、pcre和zlib的原始碼解壓目錄,安裝完成後Nginx配置檔案的完整路徑為:/usr/local/nginx-1.17.2/conf/nginx.conf。

Nginx負載均衡配置

1.負載均衡配置

http{
……
upstreamreal_server{
server192.168.103.100:2001weight=1;#輪詢伺服器和訪問權重
server192.168.103.100:2002weight=2;
}

server{
listen80;

location/{
proxy_passhttp://real_server;
}
}
}

2.失敗重試配置

upstreamreal_server{
server192.168.103.100:2001weight=1max_fails=2fail_timeout=60s;
server192.168.103.100:2002weight=2max_fails=2fail_timeout=60s;
}

意思是在fail_timeout時間內失敗了max_fails次請求後,則認為該上游伺服器不可用,然後將該服務地址踢除掉。fail_timeout時間後會再次將該伺服器加入存活列表,進行重試。

Nginx限流配置

1.配置引數

limit_req_zone指令設定引數

limit_req_zone$binary_remote_addrzone=mylimit:10mrate=10r/s;
  • limit_req_zone定義在http塊中,$binary_remote_addr表示儲存客戶端IP地址的二進位制形式。

  • Zone定義IP狀態及URL訪問頻率的共享記憶體區域。zone=keyword標識區域的名字,以及冒號後面跟區域大小。16000個IP地址的狀態資訊約1MB,所以示例中區域可以儲存160000個IP地址。

  • Rate定義最大請求速率。示例中速率不能超過每秒10個請求。

2.設定限流

location/{
limit_reqzone=mylimitburst=20nodelay;
proxy_passhttp://real_server;
}

burst排隊大小,nodelay不限制單個請求間的時間。

3.不限流白名單

geo$limit{
default1;
192.168.2.0/240;
}

map$limit$limit_key{
1$binary_remote_addr;
0"";
}

limit_req_zone$limit_keyzone=mylimit:10mrate=1r/s;

location/{
limit_reqzone=mylimitburst=1nodelay;
proxy_passhttp://real_server;
}

上述配置中,192.168.2.0/24網段的IP訪問是不限流的,其他限流。

IP後面的數字含義:

  • 24表示子網掩碼:255.255.255.0

  • 16表示子網掩碼:255.255.0.0

  • 8表示子網掩碼:255.0.0.0

Nginx快取配置

1.瀏覽器快取

靜態資源快取用expire

location~*.(jpg|jpeg|png|gif|ico|css|js)${
expires2d;
}

Response Header中添加了Expires和Cache-Control,

靜態資源包括(一般快取)

  • 普通不變的影象,如logo,圖示等

  • js、css靜態檔案

  • 可下載的內容,媒體檔案

協商快取(add_header ETag/Last-Modified value)

  • HTML檔案

  • 經常替換的圖片

  • 經常修改的js、css檔案

  • 基本不變的API介面

不需要快取

  • 使用者隱私等敏感資料

  • 經常改變的api資料介面

2.代理層快取

//快取路徑,inactive表示快取的時間,到期之後將會把快取清理
proxy_cache_path/data/cache/nginx/levels=1:2keys_zone=cache:512minactive=1dmax_size=8g;

location/{
location~\.(htm|html)?${
proxy_cachecache;
proxy_cache_key$uri$is_args$args;//以此變數值做HASH,作為KEY
//HTTP響應首部可以看到X-Cache欄位,內容可以有HIT,MISS,EXPIRES等等
add_headerX-Cache$upstream_cache_status;
proxy_cache_valid20010m;
proxy_cache_validany1m;
proxy_passhttp://real_server;
proxy_redirectoff;
}
location~.*\.(gif|jpg|jpeg|bmp|png|ico|txt|js|css)${
root/data/webapps/edc;
expires3d;
add_headerStaticNginx-Proxy;
}
}

在本地磁碟建立一個檔案目錄,根據設定,將請求的資源以K-V形式快取在此目錄當中,KEY需要自己定義(這裡用的是url的hash值),同時可以根據需要指定某內容的快取時長,比如狀態碼為200快取10分鐘,狀態碼為301,302的快取5分鐘,其他所有內容快取1分鐘等等。
可以通過purger的功能清理快取。

AB測試/個性化需求時應禁用掉瀏覽器快取。

Nginx黑名單

1.一般配置

location/{
deny192.168.1.1;
deny192.168.1.0/24;
allow10.1.1.0/16;
allow2001:0db8::/32;
denyall;
}

2. Lua+Redis動態黑名單(OpenResty)

安裝執行

yuminstallyum-utils
yum-config-manager--add-repohttps://openresty.org/package/centos/openresty.repo
yuminstallopenresty
yuminstallopenresty-resty
檢視
yum--disablerepo="*"--enablerepo="openresty"listavailable
執行
serviceopenrestystart

配置(/usr/local/openresty/nginx/conf/nginx.conf)

lua_shared_dictip_blacklist1m;

server{
listen80;

location/{
access_by_lua_filelua/ip_blacklist.lua;
proxy_passhttp://real_server;
}
}

lua指令碼(ip_blacklist.lua)

localredis_host="192.168.1.132"
localredis_port=6379
localredis_pwd=123456
localredis_db=2

--connectiontimeoutforredisinms.
localredis_connection_timeout=100

--asetkeyforblacklistentries
localredis_key="ip_blacklist"

--cachelookupsforthismanyseconds
localcache_ttl=60

--endconfiguration

localip=ngx.var.remote_addr
localip_blacklist=ngx.shared.ip_blacklist
locallast_update_time=ip_blacklist:get("last_update_time");

--updateip_blacklistfromRediseverycache_ttlseconds:
iflast_update_time==nilorlast_update_time<(ngx.now()-cache_ttl)then

localredis=require"resty.redis";
localred=redis:new();

red:set_timeout(redis_connect_timeout);

localok,err=red:connect(redis_host,redis_port);
ifnotokthen
ngx.log(ngx.ERR,"Redisconnectionerrorwhileconnect:"..err);
else
localok,err=red:auth(redis_pwd)
ifnotokthen
ngx.log(ngx.ERR,"Redispassworderrorwhileauth:"..err);
else
localnew_ip_blacklist,err=red:smembers(redis_key);
iferrthen
ngx.log(ngx.ERR,"Redisreaderrorwhileretrievingip_blacklist:"..err);
else
ngx.log(ngx.ERR,"Getdatasuccess:"..new_ip_blacklist)
--replacethelocallystoredip_blacklistwiththeupdatedvalues:
ip_blacklist:flush_all();
forindex,banned_ipinipairs(new_ip_blacklist)do
ip_blacklist:set(banned_ip,true);
end
--updatetime
ip_blacklist:set("last_update_time",ngx.now());
end
end
end
end

ifip_blacklist:get(ip)then
ngx.log(ngx.ERR,"BannedIPdetectedandrefusedaccess:"..ip);
returnngx.exit(ngx.HTTP_FORBIDDEN);
end

Nginx灰度釋出

1.根據Cookie實現灰度釋出

根據Cookie查詢version值,如果該version值為v1轉發到host1,為v2轉發到host2,都不匹配的情況下轉發到預設配置。

upstreamhost1{
server192.168.2.46:2001weight=1;#輪詢伺服器和訪問權重
server192.168.2.46:2002weight=2;
}

upstreamhost2{
server192.168.1.155:1111max_fails=1fail_timeout=60;
}

upstreamdefault{
server192.168.1.153:1111max_fails=1fail_timeout=60;
}

map$COOKIE_version$group{
~*v1$host1;
~*v2$host2;
defaultdefault;
}

lua_shared_dictip_blacklist1m;

server{
listen80;

#set$group"default";
#if($http_cookie~*"version=v1"){
#set$grouphost1;
#}
#if($http_cookie~*"version=v2"){
#set$grouphost2;
#}

location/{
access_by_lua_filelua/ip_blacklist.lua;
proxy_passhttp://$group;
}
}

2.根據來路IP實現灰度釋出

server{
……………
set$groupdefault;
if($remote_addr~"192.168.119.1"){
set$grouphost1;
}
if($remote_addr~"192.168.119.2"){
set$grouphost2;
}

3.更細粒度灰度釋出

參考:

https://github.com/sunshinelyz/ABTestingGateway

好了,咱們今天就聊到這兒吧!別忘了給個在看和轉發,讓更多的人看到,一起學習一起進步!!

相關推薦