0%
需要进行负载均衡的配置
以下是经过测试的 Nginx 负载均衡的配置方法
conf 文件修改
1.修改nginx.conf
1 2 3 4 5 6 7 8 9
| cd /usr/local/nginx/conf vi nginx.conf
# 在 http 配置最后增加以下内容 http { ... #包含server配置文件 include /usr/local/nginx/conf/server/*.conf; }
|
2.创建负载均衡配置文件
1 2 3
| cd /usr/local/nginx/conf mkdir server vi ubt_data_collection.conf
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
|
upstream ubt_data_collection {
server cdh8:8001 weight=5; server cdh8:8002 weight=5; }
server {
listen 8003;
server_name localhost;
location /{
proxy_pass http://ubt_data_collection; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } }
|
重新加载配置文件
1 2
| cd /usr/local/nginx/sbin ./nginx -s reload
|
测试用 SpringBoot 代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| package com.offlineClass;
import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController;
@RestController @SpringBootApplication public class NginxtestApplication { @Value("${server.port:}") private String port;
public static void main(String[] args) { SpringApplication.run(NginxtestApplication.class, args); }
@GetMapping("") public String hello() { System.out.println("call me " + port); return "i am " + port; } }
|
启动命令
1 2
| java -jar nginx_test.jar --server.port=8001 java -jar nginx_test.jar --server.port=8002
|
请求地址