SpringCloud微服務筆記-Nginx實現網關反向代理


背景

當前在SpringCloud微服務架構下,網關作為服務的入口尤為重要,一旦網關發生單點故障會導致整個服務集群癱瘓,為了保證網關的高可用可以通過Nginx的反向代理功能實現網關的高可用。

項目源碼:https://github.com/taoweidong/Micro-service-learning/tree/SpringCloud-branch

項目架構圖

在這里插入圖片描述

  • Nginx作為反向代理服務器,代理后端網關服務,通過Nginx自帶的負載均衡算法進行轉發
  • Zull網關部署集群時,如果一台服務器發生故障,就會轉發到另外一台機器上,服務正常訪問,保證網關的高可用

具體部署

修改本地Host文件

(C:\Windows\System32\drivers\etc)編輯下面這個文件,修改里面ip對應的地址,因為要使用域名的不同來實現反向代理.
在這里插入圖片描述在這里插入圖片描述

Nginx配置

下載

Nginx下載地址(Windows和Linux的配置一樣):http://nginx.org/en/download.html

配置

解壓后,找到配置文件\nginx-1.12.2\conf\nginx.conf
在這里插入圖片描述詳細配置

#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;
	
	#配置上游服務器網關端口集群
	upstream  backServer{
		# weight 權重:誰的的權重多,訪問到哪個服務的幾率就大
	    server 127.0.0.1:8040 weight=1;
	    server 127.0.0.1:8041 weight=1;
	}

    server {
		# 注意:如果使用域名進行反向代理的話,Nginx的端口必須是80
        listen       80;
		# 入口地址-對應域名地址
        server_name  www.taowd123.com;  

        location /ms {
            ### 指定上游服務器負載均衡服務器
		    proxy_pass http://backServer/;
            index  index.html index.htm;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        # 錯誤頁面
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
      
    }
    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

}

測試

  • 啟動注冊中心服務:http://127.0.0.1:8761/
  • 啟動兩個網關,端口分別為:8041,8040
  • 啟動服務提供者,端口為:9000 已經在Zuul中配置
  • 啟動Nginx服務
    在這里插入圖片描述
  • 訪問注冊中心檢查服務
    在這里插入圖片描述
  • 使用網關端口直接訪問正常
    在這里插入圖片描述
  • 使用host中配置的域名直接訪問,測試反向代理功能
    在這里插入圖片描述訪問多次,檢查網關后台輸出結果
    在這里插入圖片描述

參考

https://blog.csdn.net/kxj19980524/article/details/87868108

歡迎訪問個人博客: http://www.taoweidong.com/


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM