基於Nginx+redis的Asp.net站點搭建

劇情介紹html

在傳統的信息系統(好比小規模的ERP\MES系統),每每只是進行簡單的應用服務器和數據庫服務器的分佈式部署,以此來提升應用系統的負載能力,而伴隨着訪問的增大,應用服務器層面除了作硬件和網絡的擴容,很難應對【套路式開頭】。linux


 

固然如今開源技術不少,不就是分佈式麼,應用服務器分佈式、數據庫讀寫分離、緩存服務器、認證服務器。。。的確方法不少。那麼不買關子了,今天就應用服務器層面的負載均衡講講,能夠動手練練的技術:Nginx,固然也包括緩存技術:redisnginx

初步的設想是這樣的:經過nginx對局域網內多個相同應用服務器進行進行負載均衡,而且各個相同應用共享一個緩存服務器【表達的就是這麼簡單】。拉個效果圖:c++

開始搭建【折騰】redis

一、操做系統準備數據庫

linux一臺,固然通常爲虛擬機,這裏我安裝了centos7,配置ip地址爲:192.168.110.100,機器名就叫:centos。vim

能夠運行asp.net mvc站點windows一臺,好比windows10+iis8,配置ip地址爲:192.168.110.1,機器名無所謂。windows

配置兩臺機器的hosts:centos

windows:C:\Windows\system32\drivers\etc\hostsapi

192.168.110.100  cluster.com

 

centos: vim /etc/hosts

192.168.110.100  cluster.com 

 二、安裝Nginx

通常首先須要安裝編譯環境【反正不搞c,參照其餘文章手動安裝】,centos支持yum安裝,通常就是yum install ,固然先su root下用root用戶登陸。

保證虛擬機聯網,執行命令:yum install gcc-c++

原本覺得能夠直接安裝nginx了,沒想到還有三個依賴庫要下載安裝,套路同樣:

下載安裝包、解壓安裝包、進入配置目錄,分別執行make 和make install。

固然這不是重點,直接說明:

其中本次下載版本:pcre-8.40.tar.gz、zlib-1.2.11.tar.gz、openssl-fips-2.0.10.tar.gz、nginx-1.12.2.tar.gz

 > 安裝pcre
         獲取pcre編譯安裝包,ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.40.tar.gz,拷貝到centos
         解壓縮pcre-xx.tar.gz包。
         進入解壓縮目錄,分別執行 ./configure 、  make 、  make install

 > 安裝openssl
         獲取openssl編譯安裝包,在https://www.openssl.org/source/openssl-fips-2.0.10.tar.gz,拷貝到centos

         解壓縮openssl-xx.tar.gz包。
         進入解壓縮目錄,分別執行./config、make 、  make install

  > 安裝zlib
         獲取zlib編譯安裝包,在http://zlib.net/zlib-1.2.11.tar.gz,拷貝到centos
         解壓縮openssl-xx.tar.gz包。
         進入解壓縮目錄,分別執行 ./configure 、 make  、 make install

 > 安裝nginx
        獲取nginx,在http://nginx.org/download/nginx-1.12.2.tar.gz,拷貝到centos
         解壓縮nginx-xx.tar.gz包。
         進入解壓縮目錄,分別執行 ./configure、  make 、  make install

 

三、配置nginx

按照第2部分,安裝好nginx,固然 沒有想過其餘安裝經驗的,確定會出現不少問題,建議百度本身搞定。咱們能夠在命令行裏輸入:whereis nginx

若是正常安裝會出現nginx的按照目錄:

[root@centos bin]# whereis nginx
nginx: /usr/local/nginx
[root@centos bin]# cd /usr/local/nginx
[root@centos nginx]# ls -l
總用量 0
drwx------ 2 nobody root 6 11月 2 14:08 client_body_temp
drwxr-xr-x 2 root root 333 11月 2 20:56 conf
drwx------ 2 nobody root 6 11月 2 14:08 fastcgi_temp
drwxr-xr-x 2 root root 40 11月 2 11:05 html
drwxr-xr-x 2 root root 58 11月 2 20:58 logs
drwx------ 2 nobody root 6 11月 2 14:08 proxy_temp
drwxr-xr-x 2 root root 19 11月 2 11:05 sbin
drwx------ 2 nobody root 6 11月 2 14:08 scgi_temp
drwx------ 2 nobody root 6 11月 2 14:08 uwsgi_temp

通常配置文件在conf文件夾下,名稱叫nginx.conf,本次實驗關鍵就是配置該文件,廢話不說打開編輯:

[root@centos nginx]# vim ./conf/nginx.conf

咱們修改如下點【192.168.110.1:9001和9002站點是 底下第5點部署的應用站點,這裏提早說明。那爲啥同一個ip呢,本地演示就丟一臺IIS上了,端口不一樣就能夠】:

 1 http {
 2 
 3     #....省略一些編碼
 4     #咱們須要負載均衡的內部應用地址以及端口,其中weight爲權重,這裏就50% 各佔一半了
 5     upstream    cluster.com{
 6        server 192.168.110.1:9001 weight=1;
 7        server 192.168.110.1:9002 weight=1;
 8     }
 9 
10     server {
11         listen       80;
#須要負載的站點,這裏就是本機設置的hosts站點
12 server_name cluster.com; 13 14 #charset koi8-r; 15 16 #access_log logs/host.access.log main; 17 18 location / { 19 root html; 20 index index.html index.htm; 21 proxy_pass http://cluster.com; 22 #設置主機頭和客戶端真實地址,以便服務器獲取客戶端真實IP 23 proxy_set_header X-Forwarded-Host $host; 24 proxy_set_header X-Forwarded-Server $host; 25 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 26 proxy_set_header X-Real-IP $remote_addr; 27 } 28 29 #...省略一些編碼 30 }

 配置就是這麼簡單,在啓動nginx以前建議關閉下centos的防火牆:

systemctl stop iptalbes.service

開啓nginx,在nginx目錄下執行,順便檢測下是否成功:

 

 [root@centos nginx]# ./sbin/nginx

 [root@centos nginx]# ps -ef | grep nginx
 root 1485 1 0 08:51 ? 00:00:00 nginx: master process ./sbin/nginx
 nobody 1486 1485 0 08:51 ? 00:00:00 nginx: worker process
 root 1663 1471 0 09:28 pts/0 00:00:00 grep --color=auto nginx

 這個時候咱們在windows電腦上 瀏覽器訪問下 cluster.com 應該就能夠看到 9001或者9002站點了:

是否是看不到到底訪問了那臺電腦,別急,咱們先部署好redis再來寫幾個mvc請求服務就能夠。

 

四、安裝配置redis

和按照nginx同樣的步驟,先下載,這裏我下載了比較舊的版本redis-3.0.6.tar.gz,https://redis.io/download,固然你也能夠下載最新的。

三板斧:解壓文件、進入解壓目錄,執行make

而後進入子目錄Src,能夠看到一些執行文件【主要:redis-server\redis-cli】和一個redis.conf。咱們最好在/usr/local下去建個redis目錄,而後裏面把幾個主要的拷貝進去。

固然這裏有個文件配置須要修改下:redis.cong下的是否後臺開啓:

vim redis.conf
#而後修改
daemonize yes
#同時註釋掉bind 端,保證局域網均可以訪問

 開啓redis:

 ./redis-server ./redis.conf
1 [root@centos redis]# redis-cli
2 127.0.0.1:6379> set test "helloword"
3 OK
4 127.0.0.1:6379> get test
5 "helloword"
6 127.0.0.1:6379> 

 就是這麼簡單,測試也成功了,這裏再次強調,關閉centos的防火牆。

五、編寫併發布asp.net mvc 站點

經過上面的一步步安裝,終於進入正題了,咱們構建一個簡單的asp.net mvc站點,這裏就不廢話了,而後把站點部署到本地iis,部署兩個站點,端口後分別爲9001和9002。

這裏須要說明的是,windows的防火牆若是開啓了,那麼請在防火牆裏面開啓對端口9001和9002的進出站限制。

同時爲了開發鏈接redis進行緩存服務,還須要安裝api包,功能在這裏:

輸入:Install-Package StackExchange.Redis

在HomeController裏添加如下服務:

 1 public class HomeController : Controller
 2     {
 3         public ActionResult Index()
 4         {
 5             return View();
 6         }
 7         /// <summary>
 8         /// 獲取服務請求地址
 9         /// </summary>
10         /// <returns></returns>
11         public JsonResult GetServerInfo()
12         {
13             var server = HttpContext.Request.Url.Host + " " + HttpContext.Request.Url.Port;
14             return Json(server, JsonRequestBehavior.AllowGet);
15         }
16         /// <summary>
17         /// 設置緩存
18         /// </summary>
19         /// <param name="key"></param>
20         /// <param name="value"></param>
21         /// <returns></returns>
22         public JsonResult SetRedisValue(string key, string value)
23         {
24             RedisClient client = new RedisClient("192.168.110.100", 6379);
25 
26             client.SetValue(key,value);
27 
28             var server = HttpContext.Request.Url.Host + " " + HttpContext.Request.Url.Port;
29 
30             return Json($"訪問服務器:{server},設置緩存鍵{key}的值爲{value}", JsonRequestBehavior.AllowGet);
31         }
32         /// <summary>
33         /// 讀取緩存
34         /// </summary>
35         /// <param name="key"></param>
36         /// <returns></returns>
37         public JsonResult GetRedisValue(string key)
38         {
39             RedisClient client = new RedisClient("192.168.110.100", 6379);
40             var v =client.GetValue(key);
41 
42             var server = HttpContext.Request.Url.Host + " " + HttpContext.Request.Url.Port;
43 
44             return Json($"訪問服務器:{server},獲取緩存鍵{key}的值爲{v}", JsonRequestBehavior.AllowGet);
45         }
View Code

六、測試運行

 終於開始[項目驗收]

第1步,首先訪問地址:http://cluster.com/Home/GetServerInfo,而且不斷的F5刷新頁面,能夠發現,會變化的出現"cluster.com 9002"、"cluster.com 9001"說明負載均衡成功了,9001和9002端口應用不斷替換被用戶訪問。

第2步,寫緩存http://cluster.com/Home/SetRedisValue?key=test&value=helloword  ,能夠發現,會提示你:"訪問服務器:cluster.com 9001,設置緩存鍵test的值爲helloword"

第3步,請求緩存http://cluster.com/Home/GetRedisValue?key=test,能夠發現,若是屢次刷新,會變化出現:

"訪問服務器:cluster.com 9002,獲取緩存鍵test的值爲helloword"
"訪問服務器:cluster.com 9001,獲取緩存鍵test的值爲helloword"
說明,咱們的試驗成功了!

 

七、後感

回顧整個過程,其實真的只是小試驗,僅此記錄下學習的過程而已。這裏固然尚未進行會話狀態的同步、數據庫層面的分佈式。

相關文章
相關標籤/搜索