nginx 负载均衡(url-hash)


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次测试,均输出相同的结果

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值