Extjs4 RowEditing Update 按鈕的背景色改變
引言
一個專案搭建
問題1:客戶端到底要將請求傳送給哪臺伺服器?
問題2:如果所有客戶端都將請求傳送給了伺服器1,怎麼辦?
問題3:客戶端可能會請求動態資源 或者 靜態資源,如果只請求靜態資源,那麼還讓伺服器處理是否佔用資源了?
伺服器搭建集群后
為什麼要學習Nginx?它有效的解決了上面3個問題。
問題1:Nginx會接受使用者的請求,再做轉發,所以客戶端只需要向Nginx發起請求。
問題2:Nginx通過演算法指定將請求傳送到哪個伺服器上,比如:輪循演算法 就是按順序1 2 1 2。
問題3:Nginx可以實現動靜分離,動態資源請求交給伺服器,靜態資源自身就可以處理。
伺服器搭建叢集,並用Nginx做反向代理
Nginx介紹
Nginx是一款輕量級的Web 伺服器/反向代理伺服器及電子郵件(IMAP/POP3)代理伺服器,在BSD-like 協議下發行。其特點是佔有記憶體少,併發能力強,事實上nginx的併發能力在同類型的網頁伺服器中表現較好,中國大陸使用nginx網站使用者有:百度、京東、新浪、網易、騰訊、淘寶等。
特點:
- 能夠支援 50,000 個併發連線數的響應。(tomcat預設支援150個併發量)
- 穩定性極強,可以 24小時不間斷的工作。
- 佔用記憶體小
- Nginx提供了非常豐富的配置例項,學習成本低。
二、Nginx的安裝
# 建立維護 nginx服務的目錄 [root@zxh opt]# mkdir docker_nginx [root@zxh opt]# cd docker_nginx/ [root@zxh docker_nginx]# vim docker-compose.yml # 編寫 docker-compose.yml 編排檔案 version: "3" services: nginx: restart: always image: daocloud.io/library/nginx:latest container_name: nginx ports: - 80:80 [root@zxh docker_nginx]# docker-compose up -d # 訪問測試
cat /etc/nginx/conf.d
user nginx; worker_processes 1; error_log /var/log/nginx/error.log warn; pid /var/run/nginx.pid; # 以上統稱為全域性塊 # worker_processes 的數值越大,Nginx的併發能力就越強,需要根據伺服器的效能進行配置,預設即可 # error_log 代表Nginx的錯誤日誌存在的位置 # pid 儲存著nginx的程序id # events塊,需要根據伺服器的效能進行配置,預設即可 # worker_connections 的數值越大,Nginx併發能力越強 events { worker_connections 1024; } # http塊 http { # include代表引入一個外部的檔案 -> mime.types中放著大量的媒體型別 include /etc/nginx/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 /var/log/nginx/access.log main; sendfile on; #tcp_nopush on; keepalive_timeout 65; #gzip on; # include代表引入一個外部的檔案 -> *.conf 是配置檔案,之後的配置主要是對給目錄下的檔案進行修改 include /etc/nginx/conf.d/*.conf; }
include /etc/nginx/conf.d/*.conf 引入的配置檔案
root@c7206ced66dc:/etc/nginx/conf.d# ls default.conf root@c7206ced66dc:/etc/nginx/conf.d# cat default.conf server { listen 80; # Nginx監聽的埠 listen [::]:80; server_name localhost; # Nginx接收請求的ip,只要訪問到該地址就會被nginx處理 # location塊 location / { root /usr/share/nginx/html; # 接收到請求後,去該目錄下查詢靜態資源 index index.html index.htm; # 預設去上述的路徑中找 index.html 或 index.html } error_page 500 502 503 504 /50x.html; location = /50x.html { root /usr/share/nginx/html; } }
修改docker-compose檔案
之後需要修改的就是這個檔案,所以做好掛載
# 先停掉服務 docker-compose down # 修改配置檔案為 [root@zxh docker_nginx]# vim docker-compose.yml version: "3" services: nginx: restart: always image: daocloud.io/library/nginx:latest container_name: nginx ports: - 80:80 volumes: - /opt/docker_nginx/conf.d:/etc/nginx/conf.d # 重新構建 [root@zxh conf.d]# docker-compose build # 再次啟動 [root@zxh conf.d]# docker-compose up -d # 進入掛載目錄,編寫配置檔案 [root@zxh conf.d]# pwd /opt/docker_nginx/conf.d [root@zxh conf.d]# vim default.conf ################## server{ listen 80; server_name localhost; location / { root /usr/share/nginx/html; index index.html index.htm; } } ################## # 這樣就掛在好了配置檔案,方便之後修改
正向代理和反向代理的介紹
正向代理
正向代理伺服器是由客戶端設立的
客戶端要了解代理伺服器和目標伺服器都是誰
幫助我們實現突破訪問許可權,提高訪問的速度,對目標伺服器隱藏客戶端的 IP 地址
反向代理
反向代理伺服器是配置服務端的。
客戶端不知道訪問的到底是那一臺伺服器。
可以實現負載均衡,能夠隱藏伺服器真正的 IP 地址。
舉例:通過Nginx訪問tomcat
1)建立目標伺服器,我們就建立一個tomcat容器
[root@zxh opt]# mkdir docker_mysql_tomcat [root@zxh opt]# cd docker_mysql_tomcat/ [root@zxh docker_mysql_tomcat]# vim docker-compose.yml [root@zxh docker_mysql_tomcat]# cat docker-compose.yml version: '3' services: mysql: # 服務的名稱 restart: always # 代表只要docker啟動,那麼這個容器就跟著一起啟動 image: daocloud.io/library/mysql:5.7.4 # 指定映象路徑 container_name: mysql # 指定容器的名稱 ports: - 3306:3306 # 指定埠號的對映 environment: MYSQL_ROOT_PASSWORD: 123456 # 指定MySQL的ROOT使用者登入密碼 TZ: Asia/Shanghai # 指定時區 volumes: - /opt/docker_mysql_tomcat/mysql_data:/var/lib/mysql # 對映資料卷 tomcat: restart: always image: daocloud.io/library/tomcat:8.5.15-jre8 container_name: tomcat ports: - 8080:8080 environment: TZ: Asia/Shanghai volumes: - /opt/docker_mysql_tomcat/tomcat_webapps:/usr/local/tomcat/webapps - /opt/docker_mysql_tomcat/tomcat_logs:/usr/local/tomcat/logs # 後臺執行 [root@zxh docker_mysql_tomcat]# docker-compose up -d # 編寫首頁 [root@zxh docker_mysql_tomcat]# cd tomcat_webapps/ [root@zxh tomcat_webapps]# mkdir ROOT [root@zxh tomcat_webapps]# cd ROOT/ [root@zxh ROOT]# vim index.html [root@zxh ROOT]# cat index.html <h1>hello,lanmu!!<h1>
2)配置Nginx的反向代理
# 配置Nginx的反向代理,監聽localhost:80,當接收到請求後,將其轉發到 http://47.95.37.172:8080 [root@zxh docker_nginx]# cd conf.d/ [root@zxh conf.d]# vim default.conf [root@zxh conf.d]# cat default.conf server{ listen 80; server_name localhost; location / { proxy_pass http://47.69.37.172:8080; # 配置請求轉發 } #location / { # root /usr/share/nginx/html; # index index.html index.htm; #} } # 重啟 nginx 容器 [root@zxh conf.d]# cd .. [root@zxh docker_nginx]# docker-compose restart Restarting nginx ... done
關於Nginx的location路徑對映
# 1. 等號'='匹配 location = / { # 精準匹配,主機名後面不能帶任何的字串 } # 舉例:訪問www.baidu.com時,nginx可以匹配,因為它是根目錄 /,而訪問www.baidu.com/hello時,則不能匹配,因為它的路徑為 /hello,所有的匹配方式都是如此。 # 2. 通用匹配 location /xxx { # 匹配所有以/xxx開頭的路徑 } # 3. 正則匹配 location ~ /xxx { # 匹配所有以/xxx開頭的路徑 } # 4. 匹配開頭的路徑 location ^~ /images { # 匹配所有以/images開頭的路徑 } # 5. 匹配結尾的路徑 location ~* \.(gif|jpg|png)$ { # 匹配以gif、jpg、png結尾的路徑 } # 6. 匹配所有路徑,也就是所有路徑都會攔截 location / { }
優先順序從大到小(location / 是匹配所有的路徑,但會繼續往優先順序高的去匹配):
推薦按照優先順序書寫
例項
1)我們將之前在docker中寫過的SpringBoot+redis實現訪問自增的專案也啟動,方便做多條路徑對映
2)啟動後有兩個頁面,一個是首頁,一個自增的功能
3)現在我們用Nginx做路徑對映,修改default.conf
配置檔案
server{ listen 80; server_name localhost; location = /msg/send/index { proxy_pass http://47.95.37.172:8081/look; } location /msg/send { proxy_pass http://47.95.37.172:8081/; # 這裡URL的結尾需要有 /,否則會404 } location ^~ /msg { proxy_pass http://47.95.37.172:8080/; # 這裡URL的結尾需要有 /,否則會404 } location / { root /usr/share/nginx/html; index index.html index.htm; } }
4)重啟容器
[root@zxh docker_nginx]# docker-compose restart Restarting nginx ... done
Nginx為我們提供了三種負載均衡的策略:
輪詢(預設的方式)
將客戶端發起請求,平均的分配給每一臺伺服器,也就是每臺伺服器處理一次請求,但是也不是0101非常有規律的,這不用去糾結。
權重(用於伺服器效能不一致)
會將客戶端的請求,根據伺服器的權重值不同,分配不同的數量。
ip_hash
基於發起請求的客戶端的IP地址不同,他始終會將請求傳送到指定的伺服器上。
輪詢
- 這裡使用 8080 和 8081 埠進行模擬多臺伺服器的情況。
# 1、修改nginx配置檔案 [root@zxh docker_nginx]# vim conf.d/default.conf # 2、配置完成重新啟動容器 [root@zxh docker_nginx]# docker-compose restart Restarting nginx ... done [root@zxh docker_nginx]# cat conf.d/default.conf upstream my-server { server 47.95.37.172:8080; server 47.95.37.172:8081; } server{ listen 80; server_name localhost; location / { proxy_pass http://my-server/; } } # 格式: =================default.conf ========================= # 自定義名稱不要出現下劃線 # upstream 自定義名稱 { # server ip:port; # server ip:port: # ... # } upstream my-server { server 47.95.37.172:8080; server 47.95.37.172:8081; ... } server { listen 80; server_name localhost; # 格式:proxy_pass http://服務的名稱/; location / { proxy_pass http://my-server/; # 注意路徑最後的 /,埠號之後需要加上/ } } ================default.conf ==========================
權重
[root@zxh docker_nginx]# vim conf.d/default.conf [root@zxh docker_nginx]# docker-compose restart Restarting nginx ... done [root@zxh docker_nginx]# cat conf.d/default.conf # 只需要新增 weight=權重比例 即可,權重比例只能使用數字 upstream my-server { server 47.95.37.172:8080 weight=10; server 47.95.37.172:8081 weight=90; } server{ listen 80; server_name localhost; location / { proxy_pass http://my-server/; } }
[root@zxh docker_nginx]# vim conf.d/default.conf [root@zxh docker_nginx]# docker-compose restart Restarting nginx ... done [root@zxh docker_nginx]# cat conf.d/default.conf upstream my-server { ip_hash; # 只需要新增 ip_hash; 即可 server 47.95.37.172:8080; server 47.95.37.172:8081; } server{ listen 80; server_name localhost; location / { proxy_pass http://my-server/; } }
Nginx 的併發能力公式
- worker_processes * worker_connections / (4 | 2) = Nginx最終的併發能力
worker_processes 和 worker_connections 這兩個屬性在 nginx 的配置檔案裡看到了
為什麼要除以 4或者2 呢?
如下圖所示:
- 客戶端請求動態資源要向伺服器獲取資源,那麼就需要4個連線數。
- 客戶端請求Nginx服務裡的靜態資源,只需要從自身獲取資源,那麼就需要2個連線數。
Nginx通過動靜分離,來提升Nginx的併發能力,更快的給使用者響應。
動態資源代理
# 配置如下 location / { proxy_pass 路徑; }
# 配置如下 location / { root 靜態資源所在的父目錄路徑; index 預設訪問root路徑下的什麼資源; autoindex on; # 以列表的形式展示root目錄下的所有資源 }
- 因為靜態資源代理,指的是Nginx服務自帶的靜態資源。
- 所以需要在 Nginx服務 中建立靜態資源,然後進行代理配置即可。
1、建立靜態資源
# 1、修改docker配置檔案,掛在Nginx容器的資料卷 [root@zxh docker_nginx]# ls conf.d docker-compose.yml [root@zxh docker_nginx]# vim docker-compose.yml [root@zxh docker_nginx]# cat docker-compose.yml version: "3" services: nginx: restart: always image: daocloud.io/library/nginx:latest container_name: nginx ports: - 80:80 volumes: - /opt/docker_nginx/conf.d/:/etc/nginx/conf.d - /opt/docker_nginx/img/:/data/img # 指定根目錄下的 /data/img 目錄 - /opt/docker_nginx/html/:/data/html # 指定根目錄下的 /data/html 目錄 # 2、先停止容器 [root@zxh docker_nginx]# docker-compose down Stopping nginx ... done Removing nginx ... done Removing network docker_nginx_default # 再重新啟動,這樣可以自動建立資料卷對應的目錄 [root@zxh docker_nginx]# docker-compose up -d Creating network "docker_nginx_default" with the default driver Creating nginx ... done # 3、任意圖片當作靜態資源 [root@zxh docker_nginx]# ls conf.d docker-compose.yml html img [root@zxh docker_nginx]# mv /home/zxh/file/123\ 005.GIF img/123.GIF [root@zxh docker_nginx]# ls img 123.GIF [root@zxh docker_nginx]# vim html/index.html [root@zxh docker_nginx]# ls html index.html [root@zxh docker_nginx]# cat html/index.html <h1>hello,lanmu!</h1>
配置Nginx靜態資源代理
server{ listen 80; server_name localhost; # 代理html靜態資源,當請求 /html/xx 時,自動去 Nginx 容器中的 /data/html 目錄中獲取資源,獲取不到404 location /html { root /data; # 會自動拼接為/data/html index index.html; # 請求為 /html,則預設訪問 /html/index.html } # 代理到img靜態資源,當請求 /img/xx 時,自動去 Nginx 容器中的 /data/img 目錄中獲取資源 location /img { root /data; # 會自動拼接為/data/img autoindex on; # 以目錄的形式展示所有的資源 } }
引言
為什麼要搭建叢集呢?
一個應用有多個模組,也成為多個服務,這些服務分別搭建在不同的伺服器上,這樣可以保證某個服務宕掉了,其他的服務可以照常使用。
而 Nginx 叢集亦是如此,如果只有一臺Nginx服務,那麼Nginx服務宕掉了,那麼所有服務就都訪問不到了,所以才需要搭建叢集。
也就是解決單點故障問題。
1、怎麼知道 Nginx 可用?
準備 keepalived,監聽nginx的健康情況。
2、多個 Nginx服務,到底要訪問哪個?
準備haproxy,提供一個虛擬的路徑,統一去接收使用者的請求。
搭建Nginx叢集
注意ps:所有的配置檔案最好是通過 vim 命令創建出來。
其中的 favicon.ico 是網頁的圖示,隨便上傳一張ico圖片即可。
建立映象檔案 Dockerfile
FROM nginx: 1.13.5-alpine # 指定nginx映象版本 RUN apk update && apk upgrade # apk 類似 yaml,該映象版本不是centos,所以用的是apk命,更新倉庫 RUN apk add --no-cache bash curl ipvsadm iproute2 openrc keepalived # 下載軟體,可以看到包含了 keepalived COPY entrypoint.sh /entrypoint.sh # 複製當前目錄下的一個指令碼檔案 RUN chmod +x /entrypoint.sh # 給指令碼檔案新增執行許可權 CMD ["/entrypoint.sh"] # 執行指令碼
entrypoint.sh
#!/bin/sh #/usr/sbin/keepalived -n -l -D -f /etc/keepalived/keepalived.conf --dont-fork --log-console & /usr/sbin/ keepalived -D -f /etc/keepalived/keepalived.conf nginx -g "daemon off;"
version: "3" services: # 主服務 nginx_master: build: context: ./ # 指定當前目錄下的dockerfile dockerfile: ./Dockerfile ports: - 8081:80 # 埠對映 volumes: # 關聯首頁 - ./index-master.html:/usr/share/nginx/html/index.html # 關聯頁面圖示 - ./favicon.ico:/usr/share/ngin/html/favicon.ico # 關聯keepalived配置檔案 - ./keepalived-master.conf:/etc/keepalived/keepalived.conf networks: static-network: # 將ip寫死,保證keepalived可以正確識別 ipv4_address: 172.20.128.2 cap_add: - NET_ADMIN # 從服務 nginx_slave: build: context: ./ # 指定當前目錄下的dockerfile dockerfile: ./Dockerfile ports: - 8082:80 # 埠對映 volumes: # 關聯首頁 - ./index-slave.html:/usr/share/nginx/html/index.html # 關聯頁面圖示 - ./favicon.ico:/usr/share/ngin/html/favicon.ico # 關聯keepalived配置檔案 - ./keepalived-slave.conf:/etc/keepalived/keepalived.conf networks: static-network: ipv4_address: 172.20.128.3 cap_add: - NET_ADMIN # haproxy proxy: image: haproxy:1.7-alpine ports: # 80埠對映到haproxy的埠 - 80:6301 volumes: - ./haproxy.cfg:/usr/local/etc/haproxy/haproxy.cfg networks: - static-network networks: static-networks: ipam: config: - subnet: 172.20.0.0/16
index-master.html
<h1>Master!!</h1>
index-slave.html
<h1>Slave!!</h1>
keepalived 主從配置
keepalived-master.conf
# 指定多久檢測一次nginx的健康狀態 vrrp_script cnk_nginx { script "pidof nginx" interval 2 } vrrp_instance VI_1 { state MASTER # master負責接收使用者的請求,當master宕掉後才會有slave接收 interface eth0 # 容器內部的網絡卡名稱 virtual_router_id 33 # 虛擬路由id priority 200 # 優先順序 advert_int 1 authentication { auth_type PASS auth_pass letmein } virtual_ipaddress { 172.20.128.50 # 指定maste主服務的虛擬ip地址 } track_script { chk_nginx } }
keepalived-slave.conf
# 指定多久檢測一次nginx的健康狀態 vrrp_script cnk_nginx { script "pidof nginx" interval 2 } vrrp_instance VI_1 { state BACKUP # master負責接收使用者的請求,當master宕掉後才會有slave接收 interface eth0 # 容器內部的網絡卡名稱 virtual_router_id 33 # 虛擬路由id priority 100 # 優先順序比master低 advert_int 1 authentication { auth_type PASS auth_pass letmein } virtual_ipaddress { 172.20.128.50 # 指定maste主服務的虛擬ip地址 } track_script { chk_nginx } }
haproxy.conf
g1oba1 log 127.0.0.1 local0 maxconn 4096 daemon nbproc 4 defaults log 127.0.0.1 local3 mode http option dontlognull option redispatch retries 2 maxconn 2000 balance roundrobin timeout connect 5000ms timeout client 5000ms timeout server 5000ms frontend main bind *:6301 default_backend webserver # 172.20.128.50:80 對應 keepalived-master/slave.conf 配置的虛擬路徑 # 當用戶訪問 80埠 時,會對映到haproxy,haproxy通過下面的虛擬路徑將請求交給keepalived,有keepalived 決定選擇 主/從nginx。 backend webserver server ngxin_master 172.20.128.50:80 check inter 2000 rise 2 fall 5
測試
# 1、構建映象 [root@zxh docker_nginx_cluster]# docker-compose build # 2、後臺啟動容器 [root@zxh docker_nginx_cluster]# docker-compose up -d Creating docker_nginx_cluster_nginx_slave_1 ... done Creating docker_nginx_cluster_proxy_1 ... done Creating docker_nginx_cluster_nginx_master_1 ... done
[root@zxh docker_nginx_cluster]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES da30c781d9b8 docker_nginx_cluster_nginx_slave "/entrypoint.sh" About a minute ago Up About a minute 0.0.0.0:8082->80/tcp docker_nginx_cluster_nginx_slave_1 c2a931441c08 haproxy:1.7-alpine "docker-entrypoint.s…" About a minute ago Up About a minute 0.0.0.0:80->6301/tcp docker_nginx_cluster_proxy_1 adc693524c73 docker_nginx_cluster_nginx_master "/entrypoint.sh" About a minute ago Up About a minute 0.0.0.0:8081->80/tcp docker_nginx_cluster_nginx_master_1 # 停掉master節點的nginx容器 [root@zxh docker_nginx_cluster]# docker stop adc693524c73 adc69352473
再次訪問 80埠,發現切換到了slave從節點。
# 重新啟動容器,再次訪問80埠,可以看到又切換回了 master節點 [root@zxh docker_nginx_cluster]# docker start adc693524c73 adc69352473
引用:https://www.bilibili.com/video/BV1PV411C7bc?p=14
image-20200807092543284