Nginx实现负载均衡


这里主要测试如图:

windows nginx 检查配置 windows查看nginx运行状态_spring

下载:http://nginx.org/en/download.html

windows nginx 检查配置 windows查看nginx运行状态_windows nginx 检查配置_02

下载后解压,解压后如下:

windows nginx 检查配置 windows查看nginx运行状态_nginx_03


一、Nginx基本使用 

1、启动

  • (1)直接双击nginx.exe,双击后一个黑色的弹窗一闪而过
  • (2)打开cmd命令窗口,切换到nginx解压目录下,输入命令 nginx.exe 或者 start nginx

2、检查nginx是否启动成功

  • (1)直接在浏览器地址栏输入网址 http://localhost:80,回车,出现以下页面说明启动成功

windows nginx 检查配置 windows查看nginx运行状态_nginx_04

  • (2)可以在cmd命令窗口输入命令 tasklist /fi "imagename eq nginx.exe"

windows nginx 检查配置 windows查看nginx运行状态_windows nginx 检查配置_05

3、检查端口 

检查80端口是否被占用的命令是: netstat -ano | findstr 0.0.0.0:80 或 netstat -ano | findstr "80",如上图。

4、端口修改

nginx的配置文件是conf目录下的nginx.conf,默认配置的nginx监听的端口为80,如果80端口被占用可以修改为未被占用的端口即可

windows nginx 检查配置 windows查看nginx运行状态_tomcat_06

如果修改了nginx的配置文件nginx.conf 时,不需要关闭nginx后重新启动nginx,只需要执行命令 nginx -s reload

5、关闭

注意:如果使用cmd命令窗口启动nginx,关闭cmd窗口是不能结束nginx进程的,可使用两种方法关闭nginx

  • (1)输入nginx命令  nginx -s stop(快速停止nginx)  或  nginx -s quit(完整有序的停止nginx)
  • (2)使用 taskkill   taskkill /f /t /im nginx.exe

二、使用nginx代理服务器做负载均衡

 

1、nginx配置 

可以修改nginx的配置文件nginx.conf 达到访问nginx代理服务器时跳转到指定服务器的目的,即通过proxy_pass

可以配置多个目标服务器,当一台服务器出现故障时,nginx能将请求自动转向另一台服务器,例如配置如下: 

windows nginx 检查配置 windows查看nginx运行状态_windows nginx 检查配置_07

注意:nginx配置文件中配置upstream时用了“_”字符,如上用的 power_server,直接报错了。改成powerserver,即不使用“_字符就好了。 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

windows nginx 检查配置 windows查看nginx运行状态_nginx_08

2、代码实现

windows nginx 检查配置 windows查看nginx运行状态_tomcat_09

所有模块中pom.xml添加依赖 

<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
</dependencies>

2.1 客户端user

AppUserClient
package com;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class AppUserClient {
    public static void main(String[] args) {
        SpringApplication.run(AppUserClient.class);
    }
}
UserController :注意使用nginx代理的端口80
package com.yyy.controller;

import com.yyy.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.Map;

@RestController
public class UserController {
    @Autowired
    RestTemplate restTemplate;


    @RequestMapping("/getUser.do")
    public R getUser(){
        Map<String,Object> map = new HashMap<>();
        map.put("key","user");
        return R.success("返回成功",map);
    }


    @RequestMapping("/getPower.do")
    public R getPower(){
        return R.success("操作成功",restTemplate.getForObject("http://localhost:80/getPower.do",Object.class));
    }
}
AppConfig
package com.yyy.config;

import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;

@ComponentScan("com") //可以不写
@Configuration
public class AppConfig {

    @Bean
    public RestTemplate restTemplate(){
        return  new RestTemplate();
    }


    @Bean
    public TomcatServletWebServerFactory tomcat(){
        TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory();
        tomcat.setPort(5000);
        return  tomcat;
    }
}
 R
package com.yyy.util;

import java.util.HashMap;

public class R extends HashMap{
    public static  String SUCCESS_CODE="200";
    public static String ERROR_CODE="500";
    public static String DATA_KEY = "data";
    public static String MSG_KEY = "msg";

    private R(){

    }

    public R set(String key, Object object){
        super.put(key,object);
        return  this;
    }

    private  static R ok(){
        return new R();
    }

    public static R success(){

        return R.ok().set("code", R.SUCCESS_CODE).set(R.MSG_KEY,"操作成功");
    }

    public static R success(String msg){

        return R.ok().set("code", R.SUCCESS_CODE).set(R.MSG_KEY,msg);
    }

    public static R success(String msg, Object object){

        return R.ok().set("code", R.SUCCESS_CODE).set(R.MSG_KEY,msg).set(R.DATA_KEY,object);
    }

    public R data(Object obj){
        return this.set("data",obj);
    }

    public static R error(){
        return R.ok().set(R.MSG_KEY,"操作失败").set("code", R.ERROR_CODE);
    }

    public static R error(String msg){
        return R.ok().set(R.MSG_KEY,msg).set("code", R.ERROR_CODE);
    }

    public static R error(String msg, Object object){
        return R.ok().set(R.MSG_KEY,msg).set(R.DATA_KEY,object).set("code", R.ERROR_CODE);
    }

}

2.2 服务端power集群

power,power1,power2配置一样,只需要修改端口,为了方便查看,对应的输出信息也修改对应的。

AppPowerServer 
package com;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;


@SpringBootApplication
public class AppPowerServer {

    public static void main(String[] args) {
        SpringApplication.run(AppPowerServer.class);
    }
}
PowerController
package com.yyy.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
public class PowerController {

    @RequestMapping("/getPower.do")
    public Object getPower(){
        Map<String,Object> map = new HashMap<>();
        map.put("key","power");
        return map;
    }

}
AppConfig
package com.yyy.config;

import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@ComponentScan("com")
@Configuration
public class AppConfig {
    @Bean
    public TomcatServletWebServerFactory tomcat(){
        TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory();
        tomcat.setPort(8001);
        return  tomcat;
    }
}

2.3 启动多次刷新查看

windows nginx 检查配置 windows查看nginx运行状态_nginx_10

windows nginx 检查配置 windows查看nginx运行状态_windows nginx 检查配置_11

windows nginx 检查配置 windows查看nginx运行状态_tomcat_12