nginx 负载均衡(url-hash)
nginx 1.7.2版本后,已经集成了url hash功能,可直接使用,不要安装三方模块
***************************
使用测试
应用1:hello-world
@RestController
public class HelloController {
@RequestMapping("/hello")
public String hello(){
return "hello world";
}
@RequestMapping("/hello2")
public String hello2(){
return "hello world12";
}
@RequestMapping("/hello3")
public String hello3(){
return "hello world13";
}
}
应用2:hello-world2
@RestController
public class HelloController {
@RequestMapping("/hello")
public String hello(){
return "hello world2";
}
@RequestMapping("/hello2")
public String hello2(){
return "hello world22";
}
@RequestMapping("/hello3")
public String hello3(){
return "hello world23";
}
}
***********************
配置文件
default.conf
server {
listen 80;
server_name localhost;
location / {
proxy_pass http://web-server;
root /usr/share/nginx/html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}
upstream web-server {
hash $request_uri;
server 192.168.57.10:8080;
server 192.168.57.11:8080;
}
***********************
创建容器
docker run -it -d --net fixed3 --ip 192.168.57.10 \
-v /usr/nginx/test/hello-world.jar:/usr/local/app.jar \
--name hello-world common
docker run -it -d --net fixed3 --ip 192.168.57.11 \
-v /usr/nginx/test/hello-world2.jar:/usr/local/app.jar \
--name hello-world common
docker run -it -d --net fixed3 --ip 192.168.57.12 \
-v /usr/nginx/nginx.conf:/etc/nginx/nginx.conf \
-v /usr/nginx/conf.d/default.conf:/etc/nginx/conf.d/default.conf --name nginx nginx
***********************
测试输出
192.168.57.12:80/hello
192.168.57.12:80/hello2
192.168.57.12:80/hello3
相同的url 连续2次测试,均输出相同的结果