Springboogはnginxを統合して負荷分散を実現します(Windowsバージョン)
私はずっと前にnginxについて学びました。何が起こっているのか理解できませんでした。正確には、「ロードバランス」という4つの単語はそれが何を意味するのか理解できず、プロジェクトで使用する必要があるものに対処する順番がありません。 (実際、私はそれを処理できません...私は心が痛んでいます。)最近、私は再び関連する知識を学んでいます。私は今日それを構築し、ついに成功しました。こんにちは世界レベル。ねえ、それを記録し、将来それを使用してください!!!
ウィンドウnginxのバージョンは、私の別のブログで、ダウンロード、インストール、開始、訪問で書かれています。nginxインストールのWindowsバージョンを確認できます。開始、ここにアクセス
すると、springbootプロジェクトに統合されるため、構成ファイルにいくつかの変更が加えられます。
nginx.conf構成
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
# 服务器的集群 zjy:集群的名字 配置2个服务的ip和端口 weight=1:权重分配权重越大,分配的概率越大
upstream zjy{
server 127.0.0.1:2080 weight=1;
server 127.0.0.1:2081 weight=2;
}
server {
# 当前服务的端口
listen 80;
# 当前服务的域名
server_name localhost;
location / {
root E:/work/nginx/src/main/resources/static;
# 代理的路径
proxy_pass http://zjy;
proxy_connect_timeout 3s;
proxy_read_timeout 5s;
proxy_send_timeout 3s;
index nginx.html;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
アップストリームzjy:サーバーのクラスターzjy:クラスター
サーバーの名前127.0.0.1:2080:構成されたサービスのIPとポート
weight = 1:重みの分散が大きいほど、分散が
リッスンされる可能性が高くなります80:現在のサービスのポート
server_name localhost:現在のサービスのドメイン名
proxy_passhttp:// zjy:プロキシのパス。ドメイン名を購入しなかったため、使用できません。もう
1つは有効期限です。
nginxサービスを開始します
開始コマンド:start nginx
再始動コマンド:nginx -sリロード
停止コマンド:nginx -s stop
springbootプロジェクトをビルドする
ディレクトリ構造:
実際、NginxControllerクラスとResultBOのみが使用されます
NginxController:
package com.zjy.nginx.controller;
import com.zjy.nginx.bean.ResultBO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @PackgeName: com.zjy.nginx.controller
* @ClassName: NginxController
* @Author: zjy
* Date: 2020/6/20 17:21
* project name: nginx
* @Version:
* @Description:
*/
@Api(value = "Nginx负载均衡", tags = {
"Nginx负载均衡"})
@Slf4j
@Validated
@RestController
@RequestMapping("/nginx")
public class NginxController {
@Value("${server.port}")
private String serverPort;
@ApiOperation("Nginx负载均衡")
@GetMapping("/request")
public ResultBO request() throws Exception {
log.info("将请求打到了端口为: {} 的服务上", serverPort);
return ResultBO.success();
}
}
ResultBO:
package com.zjy.nginx.bean;
import lombok.Data;
import java.io.Serializable;
/**
* @PackgeName: com.mini.demo.yinlian.unionpay
* @ClassName: ResultBO
* @Author: zjy
* Date: 2020/4/23 12:22
* project name: mini
* @Version:
* @Description:
*/
@Data
public class ResultBO<T> implements Serializable {
private boolean succeed = true;
private int code = 0;
private String msg;
private T content;
public ResultBO(T content) {
this.content = content;
}
public ResultBO(boolean succeed, int code, String msg, T content) {
this.succeed = succeed;
this.code = code;
this.msg = msg;
this.content = content;
}
public ResultBO(boolean succeed, int code, String msg) {
this.succeed = succeed;
this.code = code;
this.msg = msg;
}
public ResultBO() {
}
public static <T> ResultBO<T> success(T content) {
return new ResultBO<T>(content);
}
public static ResultBO success() {
return new ResultBO();
}
public static ResultBO fail(int code, String msg) {
return new ResultBO(false, code, msg);
}
public static ResultBO fail(String msg) {
return new ResultBO(false, -1, msg);
}
public static ResultBO fail() {
return fail("fail");
}
}
application.properties
ポートで構成されています
server.port=2081
テスト
ポートを2080に変更してサービスを開始し
、ポートを2081に変更してサービスを再開します。
メソッドを開始するために必要な項目は1つだけです。1
。[構成の編集
]を選択します。2。[並列実行を
許可してリンクにアクセスする]をオンにします。http:// localhost / nginx / request
リクエストパスをIPに直接追加する
だけです。10回続けてリクエストしました。
結果を確認します。設定された重みが2080が1、2081が2であるため、2080サービスの結果は
2081サービスの結果です
。したがって、これは期待される結果を満たしています。
テストはOKです。
コメントを残して一緒に学ぶために大物を歓迎します!!!ありがとう!!!
===========================
元の記事、ソースとともに転載!