サーバーの数を増やし、各サーバーに要求を分散することにより、元の要求は複数のサーバーに要求を分散するのではなく、単一のサーバーに集中し、負荷を異なるサーバーに分散します。負荷分散。
実現効果:ブラウザのアドレスバーにアドレスを入力します:http : //172.16.0.1/edu/a.html、平均でポート8080および8081
1.準備
1. 8080と1つの8081の
2 つのTomcatサーバーを準備します。2. 2つのTomcatのwebappsディレクトリの下に、
8080 / a.html をテストするためのa.htmlファイルを作成します。
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
</head>
<body>
<h1>8080</h1>
</body>
</html>
8081 / a.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
</head>
<body>
<h1>8081</h1>
</body>
</html>
2. nginxを構成する
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
#access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
upstream myserver { #1.进行负载均衡服务器的列表
server 172.168.0.1:8080;
server 172.168.0.1:8081;
}
server {
listen 80;
server_name 172.168.0.1;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
proxy_pass http://myserver; # 加入映射的规则
index index.html index.htm;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
server {
listen 9001;
server_name 172.168.0.1;
location ~ /edu/ {
proxy_pass http://127.0.0.1:8080;
}
location ~ /vod/ {
proxy_pass http://127.0.0.1:8081;
}
}
# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}
3.検証
http://111.229.251.86/edu/a.htmlに頻繁にアクセスすると、返される結果は8080と8081の両方です
4.負荷分散ルール
インターネット情報の爆発的な増加に伴い、負荷分散(負荷分散)はもはや奇妙なトピックではなくなりました。名前が示すように、負荷分散は、サービスの可用性を確保するだけでなく、ユーザーに優れたエクスペリエンスを提供するのに十分な速さの応答を保証するために、負荷をさまざまなサービスユニットにプッシュすることです。アクセスとデータトラフィックの急速な増加により、さまざまなロードバランシング製品が生み出されました。多くのプロフェッショナルロードバランシングハードウェアは優れた機能を提供していますが、高価です。これにより、ロードソフトウェアが一般的になります。Linuxにはnginx、LVS、Haproxyなどのサービスがあり、負荷分散サービスを向上させることができ、nginxは次の配布モード(戦略)を提供します。
4.1ポーリング(デフォルト)
各リクエストは、時系列順に異なるバックエンドサーバーに1つずつ割り当てられます。バックエンドサーバーがダウンしている場合は、自動的に削除できます。
4.2重量
重みは重みを表し、デフォルトでは1です。重みが大きいほど、割り当てられるクライアントが多くなります。
ポーリング確率を指定します。重みはアクセス確率に比例し、バックエンドサーバーの不均一なパフォーマンスに使用されます。
upstream server_pool{
server 172.168.0.1:8080 weight=10;
server 172.168.0.1:8081 weight=5;
}
4.3 ip_hash
各リクエストはアクセスIPのハッシュ結果に従って割り当てられるため、各ビジターはバックエンドサーバーに固定的にアクセスし、セッションの問題を解決できます。たとえば
upstream server_pool{
ip_hash;
server 172.168.0.1:8080;
server 172.168.0.1:8081;
}
4.4公正な方法
リクエストはバックエンドサーバの応答時間に応じて振り分けられ、短い応答時間を優先します。
upstream server_pool{
server 192.168.5.21:8080;
server 192.168.5.21:8081;
fair;
}
Webサイトにアクセスして、8080および8081への負荷分散を行います。最初に8080にアクセスし、次に8081にアクセスします。応答時間が短い場合は、最初にアクセスします。