Redis安裝與簡介
參考:http://www.reibang.com/p/c4bba1df95c5
數(shù)據(jù)操作(哈希,列表,集合等)
Mongodb
簡介:
-----MongoDB是由C++語言編寫的惠啄,是一個(gè)基于分布式文件存儲(chǔ)的開源數(shù)據(jù)庫皇耗。數(shù)據(jù)以二進(jìn)制JSON(javascript Object Notation)格式存儲(chǔ)在BSON文件中。它是屬于NoSQL的一種赞哗。
---NoSQL(not only SQL)泛指非關(guān)系性數(shù)據(jù)庫雷则。NoSQL用于超大規(guī)模數(shù)據(jù)的存儲(chǔ)。
-----這些類型的數(shù)據(jù)庫存儲(chǔ)不需要固定的格式懈玻,無需多余操作就可以橫向擴(kuò)展巧婶。沒有聲明性查詢語言乾颁,非結(jié)構(gòu)化和不可預(yù)知的數(shù)據(jù)。多種存儲(chǔ)方式艺栈,鍵值對(duì)英岭,列存儲(chǔ),文檔存儲(chǔ)湿右,圖形數(shù)據(jù)庫诅妹。最終一致性,而非ACID屬性毅人。CAP定理吭狡,CAP原則又稱CAP定理,指的是在一個(gè)分布式系統(tǒng)中丈莺,一致性(consistency),可用性划煮,分區(qū)容錯(cuò)性。這三個(gè)要素最多只能同時(shí)實(shí)現(xiàn)兩點(diǎn)缔俄,不可能三者都有弛秋。高性能,高可用性和伸縮性俐载。
----mongoDB的主要特點(diǎn)
1.面向文檔蟹略。數(shù)據(jù)格式更靈活,易于使用和擴(kuò)展遏佣。
2.分布式數(shù)據(jù)庫挖炬,易于橫向擴(kuò)展,能夠應(yīng)對(duì)大規(guī)模數(shù)據(jù)
3.功能豐富状婶,支持索引意敛,聚合,文件存儲(chǔ)太抓,地理位置存儲(chǔ)
4.性能桌越空闲,能對(duì)文檔進(jìn)行動(dòng)態(tài)填充,也能預(yù)分配數(shù)據(jù)以利用額外的空間獲取穩(wěn)定的性能走敌。
安裝mongoDB
-
windows下安裝
11.安裝版4.0.XX版對(duì)win7有用碴倾。
image.png
https://www.mongodb.com/try/download/community---》https://www.mongodb.com/download-center/community/releases/archive,下載了免安裝版掉丽,并且是3.4.7版跌榔,因?yàn)樽钚掳姹荆蟀惭b系統(tǒng)為win10.
解壓后捶障,在根目錄下建兩個(gè)文件夾僧须,data, logs
首先:進(jìn)入bin目錄,打開comd輸入:*
mongod.exe --dbpath C:\mongodb-win32-x86_64-windows-3.4.7\data --logpath C:\mongodb-win32-x86_64-3.4.7\logs\mongodb.log
啟動(dòng)成功后data目錄下會(huì)自動(dòng)生成MongoDB數(shù)據(jù)庫的一些信息项炼,logs目錄下存的則是日志文件担平,文件內(nèi)容是啟動(dòng)信息
- liunx下安裝Mongodb
-----本人虛擬機(jī)安裝了ubuntu18.x.lts示绊,centos 7
ubuntu方式一:通過apt安裝
<1>添加簽名到APT.
wget -qO - https://www.mongodb.org/static/pgp/server-4.2.asc |sudo apt-key add -
<2>創(chuàng)建list文件并寫入命令
echo "deb [ arch=amd64 ] https://repo.mongodb.org/apt/ubuntu bionic/mongodb-org/4.2 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-4.2.list
當(dāng)我的Ubuntu install某個(gè)東西時(shí),給出下面提示
E: Could not get lock /var/lib/dpkg/lock-frontend - open (11: Resource temporarly unavailable)
E: Unable to acquire the dpkg frontend lock (/var/lib/dpkg/lock-frontend), is an other process using it?
刪除鎖定文件暂论,行了
sudo rm /var/lib/dpkg/lock-frontend
sudo rm /var/lib/dpkg/lock
<3>更新軟件源列表
sudo apt-get update
<4>安裝mongodb,默認(rèn)安裝穩(wěn)定版本
sudo apt-get install -y mongodb-org
<5>啟動(dòng)面褐,停止,重啟取胎,狀態(tài)查看展哭。
sudo service mongod start/stop/restart/status
<6>驗(yàn)證服務(wù)是否啟動(dòng)成功(啟動(dòng)mongodb shell)
mongo
>db //顯示處在什么庫中
test
>show dbs //查看當(dāng)前有什么系統(tǒng)數(shù)據(jù)庫
----Centos7中安裝
《1》。下載
wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel70-4.0.28.tgz
《2》闻蛀。解壓匪傍,并反解壓的根目錄重命名,以便操作
tar -zxvf mongodb-linux-x86_64-rhel70-4.0.28.tgz
mv mongodb-linux-x86_64-rhel70-4.0.28 mongodb
《3》觉痛。在解壓的并重命的根目錄下役衡,建兩個(gè)文件夾
[root@localhost mongodb]cd mongodb
[root@localhost mongodb]mkdir db
[root@localhost mongodb]# mkdir logs
[root@localhost mongodb]# cd bin
《4》。進(jìn)入bin目錄中秧饮,建一個(gè)mongo.conf文件映挂,且編輯,寫上如下內(nèi)容
touch mongo.conf
vi mongo.conf
-------------------------------------------------------------------------------------------------------------------
dbpath=/root/mongodb/db
logpath=/root/mongodb/logs/mongodb.log
port=27017
fork=true
fork=true表示以守護(hù)線程的方式啟動(dòng)mongodb,即允許在后臺(tái)運(yùn)行
《5》盗尸。啟動(dòng)和關(guān)閉mongodb
啟動(dòng)
cd bin
./mongod -f mongo.conf --bind_ip_all
./mongo
db.version() //可查看版本號(hào)
關(guān)閉
use admin;
db.shutdownServer();
exit
-----默認(rèn)情況下,啟動(dòng)后連接的是test庫帽撑,而關(guān)閉的命令要在admin庫中執(zhí)行欲鹏,因此半閉它需要首先切抽換到admin庫醋虏,然后執(zhí)行關(guān)閉命令。
《6》。安全管理
**---默認(rèn)情況下累颂,啟動(dòng)的mongodb沒有登錄密碼,在生產(chǎn)環(huán)境中這是不允許的琳状,mongodb中每個(gè)庫都有獨(dú)立的密碼绵跷,在哪一個(gè)庫中創(chuàng)建用戶就要在哪個(gè)庫中驗(yàn)證密碼。要配置密碼笙僚,首先要?jiǎng)?chuàng)建一個(gè)用戶芳肌,例如在amdin庫中創(chuàng)建一個(gè)用戶代碼如下:它的功能是,sang對(duì)test庫有讀寫權(quán)限肋层。用戶創(chuàng)成功后亿笤,要重啟服務(wù)。
use admin;
db.createUser({user:"sang",pwd:"123",roles:{[role:"readWrite",db:"test"}]})
./mongod -f mongo.conf --bind_ip_all
《7》栋猖。驗(yàn)證用戶是否成功,返回1表示成功净薛。
./mongo
db.auth("sang","123")
《8》。如果遠(yuǎn)程方問不了蒲拉,極大可能是系統(tǒng)中的防火墻沒有關(guān)閉肃拜。我們要關(guān)閉之痴腌。
systemctl stop firewalld.service
systemctl disable firewalld.service //重統(tǒng)重啟自動(dòng)關(guān)閉防火墻
ubuntu方式二,curl命令下載燃领,解壓.這個(gè)安裝現(xiàn)在還處于失敗中
<1>下載,沒有curl命令衷掷,則要sudo apt-get install -y curl安裝之。
sudo curl -O https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-ubuntu1804-4.2.0.tgz
//下載
<2>解壓縮
tar zxvf mongodb-linux-x86_64-ubuntu1804-4.2.0.tgz
<3>把解壓的文件移動(dòng)到/usr/local/下柿菩,再移動(dòng)一下改名戚嗅,不移它處。
sudo mv mongodb-linux-x86_64-ubuntu1804-4.2.0 /usr/local/
cd /usr/local
sudo mv mongodb-linux-x86_64-ubuntu1804-4.2.0 mongodb
<4>bin目錄加到環(huán)境變量中枢舶。以便任何地方都可用到它里面的命令懦胞。
export PATH=/usr/local/mongodb/bin:PATH
<5>創(chuàng)建/data/db數(shù)據(jù)目錄,并賦讀寫權(quán)限凉泄,再啟動(dòng)mongodb服務(wù)
rootx@ubuntu:~$ sudo mkdir -p /data/db
[sudo] password for rootx: xiong
rootx@ubuntu:~$ sudo chmod 755 /data/*
rootx@ubuntu:~$ sudo mongod
<6>驗(yàn)證是事啟動(dòng)成功躏尉,再打開一個(gè)終端,輸入
rootx@ubuntu:~$ mongo
eMonitoring()
---
> db
test
> show dbs
admin 0.000GB
config 0.000GB
local 0.000GB
>
redis集群
-----首先上面的4.0版的redis配置集群要安裝rvm工具后众,現(xiàn)在centos中胀糜,rvm版本過底,要下載高版本(2.3以上)需要翻qiang蒂誉。但5.0版本的redis可以直接用redis-cli(客戶端工)創(chuàng)建教藻。
部署主要步驟:
- 部署單機(jī)版redis
首先:下載redis.5.0x版本,復(fù)制鏈地址到CENTOS 8終端上右锨,再解壓括堤,復(fù)制到/usr/local,并用mv重命名解壓的根目錄以便好用,進(jìn)入根目錄绍移,輸入make,make install悄窃。
image.png
wget https://download.redis.io/releases/redis-5.0.14.tar.gz
tax -zxvf redis-5.0.14.tar.gz
//安裝所需tcl rpm包
rpm -qa|grep tcl //查看安裝否tcl,如果有,不用蹂窖,我這里有轧抗,如果沒有則
rpm -ivh tcl-8.5.13-4.el7.x86_64.rpm --nodeps --force
mv redis-5.0.14 /usr/local/
cd /usr/local
mv redis-5.0.14 redis
yum -y install gcc gcc-c++
make
make install
然后:配置mogod.conf
daemonizeyes
#bind 127.0.0.1
requirepass 123@456
protected-mode no
-----------------------------------------------------------------------------------------------------------------------------------------------
第1行配置表示允許Redis在后臺(tái)運(yùn)行
第2行配配置表示允許連接該Redis實(shí)例的地址,默認(rèn)情況下只允許本地連接瞬测,將默認(rèn)地址注釋掉横媚,外網(wǎng)就可以訪問它了。
第3行配置表示登錄該Redis的所需的密碼
第4行配置涣楷,表示由于有第3行的密碼登錄分唾,就可以不用保護(hù)了。
再后:關(guān)閉防火嗇狮斗,進(jìn)入bin目錄下绽乔,啟動(dòng)服務(wù)和客戶端,關(guān)閉服務(wù)
systemctl stop firewalld.service
systemctl disable firewalld.service
cd /usr/local/redis/bin
redis-server redis.conf //啟動(dòng)redis服務(wù)
redis-cli -a 123@456 //客戶端連接
redis-cli -p 6379 -a 123@456 shutdown //關(guān)閉服務(wù)
- 將已部署好的單機(jī)版redis復(fù)制多個(gè)副本(6個(gè))
首先: 創(chuàng)建集群文件夾redis-cluster碳褒,并復(fù)制已創(chuàng)建的redis 6個(gè)副本.執(zhí)行如下命令創(chuàng)建6個(gè)節(jié)點(diǎn)所需部署路徑文件夾及數(shù)據(jù)文件夾折砸,及啟動(dòng)文件目錄(bin)
[root@centos01 redis-cluster]# mkdir -p /usr/local/redis-cluster/6371/data/
[root@centos01 redis-cluster]# mkdir -p /usr/local/redis-cluster/6372/data/
[root@centos01 redis-cluster]# mkdir -p /usr/local/redis-cluster/6373/data/
[root@centos01 redis-cluster]# mkdir -p /usr/local/redis-cluster/6374/data/
[root@centos01 redis-cluster]# mkdir -p /usr/local/redis-cluster/6375/data/
[root@centos01 redis-cluster]# mkdir -p /usr/local/redis-cluster/6376/data/
[root@centos01 redis-cluster]# mkdir -p /usr/local/redis-cluster/bin/
*然后:進(jìn)入源碼編譯目錄/usr/local/redis/src/ 將腳本mkreleasehdr.sh ...等復(fù)制到集群bin目錄/usr/local/redis-cluster/bin下看疗,也可以直接將整個(gè)src目錄復(fù)制到bin目錄
cp /usr/local/redis/src/mkreleasehdr.sh redis-benchmark redis-check-aof redis-cli redis-server redis-trib.rb /usr/local/redis-cluster/bin/
cp /usr/local/redis/src/* /usr/local/redis-cluster/bin/
再后:執(zhí)行如下語句分別將已安裝成功的redis復(fù)制到6個(gè)節(jié)點(diǎn)文件夾
[root@centos01 redis-cluster]# cp -r /usr/local/redis-5.0.13/* /usr/local/redis-cluster/6371/
[root@centos01 redis-cluster]# cp -r /usr/local/redis-5.0.13/* /usr/local/redis-cluster/6372/
[root@centos01 redis-cluster]# cp -r /usr/local/redis-5.0.13/* /usr/local/redis-cluster/6373/
[root@centos01 redis-cluster]# cp -r /usr/local/redis-5.0.13/* /usr/local/redis-cluster/6374/
[root@centos01 redis-cluster]# cp -r /usr/local/redis-5.0.13/* /usr/local/redis-cluster/6375/
[root@centos01 redis-cluster]# cp -r /usr/local/redis-5.0.13/* /usr/local/redis-cluster/6376/
再然后: 修改每一個(gè)節(jié)點(diǎn)下redis.conf配置文件。如下所示睦授,6371是當(dāng)前節(jié)點(diǎn)的端口號(hào)两芳,請(qǐng)根據(jù)節(jié)點(diǎn)進(jìn)行更改,再驗(yàn)證各節(jié)點(diǎn)---啟動(dòng)各節(jié)點(diǎn)服務(wù)。進(jìn)入任意節(jié)點(diǎn)下的src目錄 登陸如下表示正常
port 6371 #端口
pidfile /var/run/redis_6371.pid
dir /usr/local/redis-cluster/6371/data/ #數(shù)據(jù)存放目錄
cluster-config-file nodes-6371.conf #節(jié)點(diǎn)信息
#bind 127.0.0.1 # ip限制 不限制就注釋掉
daemonize yes #后臺(tái)啟動(dòng)模式
requirepass 123@456 #redis登陸密碼去枷,不要密碼請(qǐng)注釋
appendonly yes
appendfsync always
cluster-enabled yes 開啟集群
-------------------------------------------------驗(yàn)證各節(jié)點(diǎn)的成功啟動(dòng)否--------------
/usr/local/redis-cluster/bin/redis-server /usr/local/redis-cluster/6371/redis.conf
/usr/local/redis-cluster/bin/redis-server /usr/local/redis- /usr/local/redis-cluster/bin/redis-server /usr/local/redis-cluster/6373/redis.conf
/usr/local/redis-cluster/bin/redis-server /usr/local/redis-cluster/6374/redis.conf
/usr/local/redis-cluster/bin/redis-server /usr/local/redis-cluster/6375/redis.conf
/usr/local/redis-cluster/bin/redis-server /usr/local/redis-cluster/6376/redis.conf
-------------------------------------------------------任意節(jié)點(diǎn)src下-------------------
./redis-cli -p 6371 -a 123456
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
127.0.0.1:6371>
————————————————
版權(quán)聲明:本文為CSDN博主「夜灬狼丶」的原創(chuàng)文章怖辆,遵循CC 4.0 BY-SA版權(quán)協(xié)議,轉(zhuǎn)載請(qǐng)附上原文出處鏈接及本聲明删顶。
原文鏈接:https://blog.csdn.net/u010976445/article/details/119331006
---------------------------------------------停止對(duì)應(yīng)端口號(hào)的服務(wù)--------------------
redis-cli -p 6371 -a 123@456 shutdown
- 創(chuàng)建集群
----redis5.0之前的版本創(chuàng)建集群使用redis-trib.rb命令竖螃,5.0之后的版本直接使用redis-cli命令,也不用ruby命令
# /usr/local/redis-cluster/bin/redis-cli --cluster create 192.168.19.128:6371 192.168.19.128:6372 192.168.19.128:6373 192.168.19.128:6374 192.168.19.128:6375 192.168.19.128:6376 --cluster-replicas 1 -a 123@456
//--cluster-replicas 1 :表示主從復(fù)制比例為 1:1,即一個(gè)主節(jié)點(diǎn)對(duì)應(yīng)一個(gè)從節(jié)點(diǎn)逗余,我們有6個(gè)節(jié)點(diǎn)特咆,即默認(rèn)前3個(gè)為主,后3個(gè)為從
//執(zhí)行后會(huì)有提示需要輸入”yes“, 之后打印如下信息標(biāo)識(shí)創(chuàng)建完成
- 集群驗(yàn)證
----集群規(guī)劃(測試環(huán)境ip192.168.19.128录粱,一臺(tái)部署6個(gè)節(jié)點(diǎn)腻格,正式環(huán)境和測試環(huán)境一樣的部署方式,無非把節(jié)點(diǎn)分配到其他服務(wù)器)
image.png
首先:任意節(jié)點(diǎn)創(chuàng)建key 任意節(jié)點(diǎn)登陸redi創(chuàng)建一個(gè)名字為name的key啥繁,并設(shè)置值為text
/usr/local/redis-cluster/bin/redis-cli -c -h 192.168.19.128 -p 6371 -a 123@456
192.168.19.128:6371> set name text
-> Redirected to slot [5798] located at 192.168.19.128:6372
OK
192.168.19.128:6372>
然后:登陸其他節(jié)點(diǎn)(非4.1中登陸的節(jié)點(diǎn))菜职,查看數(shù)據(jù),可以看見如下圖所示输虱,登陸節(jié)點(diǎn)5(6375)能正常查看到4.1步驟在節(jié)點(diǎn)1(6371)創(chuàng)建的名為name值為text的key些楣,至此標(biāo)識(shí)集群創(chuàng)建成功。
# /usr/local/redis-cluster/bin/redis-cli -c -h 192.168.19.128 -p 6375 -a 123456
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
192.168.19.128:6375> get name
-> Redirected to slot [5798] located at 192.168.19.128:6372
"text"
redis集群補(bǔ)充優(yōu)化上面的做法
-------不用復(fù)制所有根目錄文件到節(jié)點(diǎn)根目錄中宪睹,只需redis.conf文件。
首先下載redis-6.2.10.tar.gz蚕钦,再wincrp上傳到Centos 7上亭病。
如果提示無gcc,首先需要安裝gcc
查看gcc版本嘶居,Centos版本默認(rèn)安裝的是4.8.5版本的gcc罪帖,版本過低,升級(jí)完成后查看gcc版本
gcc -v
yum install -y centos-release-scl
yum install -y devtoolset-9-gcc devtoolset-9-gcc-c++ devtoolset-9-binutils
yum install -y systemd-devel
echo "source /opt/rh/devtoolset-9/enable" >> /etc/profile
source /etc/profile
解壓安裝tar -zxvf redis,下面進(jìn)行Redis的編譯安裝, 因Redis版本原因需要安裝或者更新tcl版本大于8.5
wget http://downloads.sourceforge.net/tcl/tcl8.6.1-src.tar.gz
tar -xzvf tcl8.6.1-src.tar.gz -C /usr/local/
cd /usr/local/tcl8.6.1/unix/
./configure
make
make install
下面進(jìn)入redis目錄執(zhí)行編譯redis need systemd-devel
cd /usr/local/redis-6.2.10/src/
make BUILD_WITH_SYSTEMD=yes USE_SYSTEMD=yes
make install
安裝完成后查看redis版本,如果只啟動(dòng)6379端口號(hào)的主服務(wù)器,則需redis-server ../redis-6.2.10/redis.conf.配置文件見上面的描述
redis-cli --version
redis-server --version
cd /usr/local
redis-server ../redis-6.2.10/redis.conf
集群創(chuàng)建
1.創(chuàng)建六個(gè)節(jié)點(diǎn)(cd /usr/local/redis-cluster)
[root@xsw redis-cluster]# mkdir -p 6371/data
[root@xsw redis-cluster]# mkdir -p 6372/data
[root@xsw redis-cluster]# mkdir -p 6373/data
[root@xsw redis-cluster]# mkdir -p 6374/data
[root@xsw redis-cluster]# mkdir -p 6375/data
[root@xsw redis-cluster]# mkdir -p 6376/data
[root@xsw redis-cluster]# mkdir -p bin
2.把安裝redis后產(chǎn)生的bin(6.x版本中是src)目錄拷貝放到 /usr/local/redis-cluster/bin下
cd /usr/local/redis-6.2.10
cp src/* /usr/local/redis-cluster/bin/
3.在各節(jié)點(diǎn)之中建redis.conf文件,下面是示例邮屁。
bind 0.0.0.0
protected-mode no
port 6373
daemonize yes
pidfile /var/run/redis_6373.pid
logfile /usr/local/redis-cluster/6373/redis_6373.log
dbfilename dump_6373.rdb
dir /usr/local/redis-cluster/6373/
masterauth 123456
requirepass 123456
cluster-enabled yes
cluster-config-file nodes-6373.conf
4.編寫啟動(dòng)腳本(隨便在什么地方建整袁,但運(yùn)行時(shí),要到這兒來佑吝。本人放在redis-cluster目錄下,執(zhí)行后坐昙,會(huì)啟動(dòng)六個(gè)節(jié)點(diǎn)。
vi start.sh
---------------------------------------------------------------------------------------------------
#!/bin/bash
/usr/local/redis-cluster/bin/redis-server /usr/local/redis-cluster/6371/redis.conf
/usr/local/redis-cluster/bin/redis-server /usr/local/redis-cluster/6372/redis.conf
/usr/local/redis-cluster/bin/redis-server /usr/local/redis-cluster/6373/redis.conf
/usr/local/redis-cluster/bin/redis-server /usr/local/redis-cluster/6374/redis.conf
/usr/local/redis-cluster/bin/redis-server /usr/local/redis-cluster/6375/redis.conf
/usr/local/redis-cluster/bin/redis-server /usr/local/redis-cluster/6376/redis.conf
---------------------------------------------運(yùn)行sh文件----------------------------------
bash start.sh
5.查看進(jìn)行
ps -ef | grep redis
6.創(chuàng)建集群
redis-cli -a 123456 --cluster create 10.170.11.61:6371 10.170.11.61:6372 10.170.11.61:6373 10.170.11.61:6374 10.170.11.61:6375 10.170.11.61:6376 --cluster-replicas 1
- 測試成功否
-----分別登錄6371芋忿,6376
redis-cli -c -p 6371 -a 123456
>set name xiong
redis-cli -c -p 6376 -a 123456
get name
8.刪除集群節(jié)點(diǎn)
-------redis集群里的節(jié)點(diǎn)支持動(dòng)態(tài)刪除炸客,但是一般情況下不會(huì)這么做疾棵,只有在節(jié)點(diǎn)軟硬件升級(jí)的時(shí)候才會(huì)主動(dòng)讓節(jié)點(diǎn)下線。刪除節(jié)點(diǎn)的方式就是redis-cli客戶端連接到服務(wù)器痹仙,然后執(zhí)行cluster forget node-id就可以了是尔,如果是刪除一個(gè)從節(jié)點(diǎn)的話,集群仍然是可用狀態(tài)开仰,如果是刪除一個(gè)主節(jié)點(diǎn)的話拟枚,集群的槽位不足,就會(huì)變成不可用狀態(tài)众弓。
首先:查看節(jié)點(diǎn)信息,再停止所有節(jié)點(diǎn)服務(wù)
stop.sh
#!/bin/bash
/usr/local/redis-cluster/bin/redis-cli -c -p 6371 -a 123@456 shutdown
/usr/local/redis-cluster/bin/redis-cli -c -p 6372 -a 123@456 shutdown
/usr/local/redis-cluster/bin/redis-cli -c -p 6373 -a 123@456 shutdown
/usr/local/redis-cluster/bin/redis-cli -c -p 6374 -a 123@456 shutdown
/usr/local/redis-cluster/bin/redis-cli -c -p 6375 -a 123@456 shutdown
/usr/local/redis-cluster/bin/redis-cli -c -p 6376 -a 123@456 shutdown
127.0.0.1:7000> cluster info
127.0.0.1:7000> cluster nodes
--------------------------------------------------------------------------------------------------
930daea84150b5fabd32a95592781b27ceab1b71 192.168.39.153:7001 master - 0 1479044139420 2 connected 5461-10922
8a6707d5b9269b6260315b47f300c1ab599733b7 192.168.39.153:7005 slave bdb62bb6ffce71588961f513c74b0d5a1a7145ea 0 1479044141441 6 connected
bdb62bb6ffce71588961f513c74b0d5a1a7145ea 192.168.39.153:7002 master - 0 1479044139925 3 connected 10923-16383
81c884ebfc919ad293f02d797aff1033025ac27e 192.168.39.153:7004 slave 930daea84150b5fabd32a95592781b27ceab1b71 0 1479044140937 2 connected
099cfc6fbb785449a8bf5369a53d21a9e127fa42 192.168.39.153:7000 myself,slave a8081e97862d9cf76c72d364f9a173187376f215 0 0 1 connected
a8081e97862d9cf76c72d364f9a173187376f215 192.168.39.153:7003 master - 0 1479044140430 7 connected 0-5460
然后:我們刪除從節(jié)點(diǎn)192.168.39.153:7004
------我們看看redis源代碼恩溅,看看forget刪除節(jié)點(diǎn)是怎么實(shí)現(xiàn)的,在redis/cluster.c文件里田轧,客戶端傳入的forget參數(shù)會(huì)進(jìn)
127.0.0.1:7000> cluster forget 81c884ebfc919ad293f02d797aff1033025ac27e
9.重新搭建集群
首先:關(guān)閉所有節(jié)點(diǎn)
----使用kill命令殺掉相應(yīng)的進(jìn)程即可暴匠。
ps -ef | grep redis
kill -i pid
云服務(wù)器redis集群問題
http://www.reibang.com/p/9d6a55a42a8b
主要是 公網(wǎng)不能訪問到私網(wǎng)中的redis數(shù)據(jù)。
-----連接客戶端我們用的是Lettuce傻粘,這里發(fā)現(xiàn)指定的公網(wǎng)ip竟然變成私網(wǎng)ip了每窖,客戶端獲取的IP地址信息是從Redis集群獲取的,所以我們得讓集群返回給我們公網(wǎng)ip弦悉。
-----手動(dòng)指定了公網(wǎng)ip后窒典,Redis集群中的節(jié)點(diǎn)會(huì)通過公網(wǎng)IP進(jìn)行通信,也就是外網(wǎng)訪問稽莉。因此相關(guān)的總線端口(+10000)瀑志,如下面的16371-16376等總線端口必須在云服務(wù)器中的安全組中放開,不然集群會(huì)處于fail狀態(tài)污秆。
- 修改配置文件
# 配置文件進(jìn)行了精簡劈猪,完整配置可自行和官方提供的完整conf文件進(jìn)行對(duì)照。端口號(hào)自行對(duì)應(yīng)修改
#后臺(tái)啟動(dòng)的意思
daemonize yes
#端口號(hào)
port 6371
# IP綁定良拼,redis不建議對(duì)公網(wǎng)開放战得,這里綁定了服務(wù)器私網(wǎng)IP及環(huán)回地址
bind 172.17.0.13 127.0.0.1
# redis數(shù)據(jù)文件存放的目錄
dir /redis/workingDir
# 日志文件
logfile "/redis/logs/cluster-node-6381.log"
# 開啟AOF
appendonly yes
# 開啟集群
cluster-enabled yes
# 集群持久化配置文件,內(nèi)容包含其它節(jié)點(diǎn)的狀態(tài)庸推,持久化變量等常侦,會(huì)自動(dòng)生成在上面配置的dir目錄下
cluster-config-file cluster-node-6381.conf
# 集群節(jié)點(diǎn)不可用的最大時(shí)間(毫秒),如果主節(jié)點(diǎn)在指定時(shí)間內(nèi)不可達(dá)贬媒,那么會(huì)進(jìn)行故障轉(zhuǎn)移
cluster-node-timeout 5000
# 云服務(wù)器上部署需指定公網(wǎng)ip
cluster-announce-ip 122.51.151.130
# Redis總線端口聋亡,用于與其它節(jié)點(diǎn)通信
cluster-announce-bus-port 16381
----------------------------------------------------------------實(shí)測成功的配置--------------------------------------
protected-mode no
bind 0.0.0.0 10.170.11.61 #(10.170.11.61青云云主機(jī)內(nèi)網(wǎng)IP--2023.3--2023.6)
port 6371
daemonize yes
pidfile /usr/local/redis-cluster/6371/redis.pid
logfile "/usr/local/redis-cluster/6371/redis.log"
dbfilename dump6371.rdb
dir /usr/local/redis-cluster/6371/data
appendonly yes
appendfilename "appendonly_6371.aof"
cluster-enabled yes
cluster-config-file /usr/local/redis-cluster/6371/nodes.conf
cluster-node-timeout 15000
cluster-replica-validity-factor 10
cluster-migration-barrier 1
cluster-require-full-coverage no
requirepass 123456
- 重新啟動(dòng)Redis服務(wù)并創(chuàng)建集群,節(jié)點(diǎn)ip要用公網(wǎng)ip,不是內(nèi)網(wǎng)IP
redis-cli --cluster create 139.198.11.194:6371 139.198.11.194:6372
139.198.11.194:6373 139.198.11.194:6374 139.198.11.194:6375
139.198.11.194:6376 --cluster-replicas 1 -a 123456
Centos7 jdk安裝
- 確定系統(tǒng)上是否安裝jdk
#1.檢查當(dāng)前機(jī)器是否有自帶的JDK
rpm -qa |grep java
rpm -qa |grep jdk
rpm -qa |grep gcj
#2.如果沒有 則跳至安裝步驟际乘,有的話 進(jìn)行卸載
rpm -qa | grep java | xargs rpm -e --nodeps
#3.檢測卸載是否成功
java -version
#出現(xiàn)一下提示則說明沒有安裝JDK或者已經(jīng)卸載成功
-bash: java: command not found
- 安裝
1.下載坡倔,提示:將下載好的壓縮包通過FTP工具上傳到服務(wù)器的/usr/local/java目錄中,上傳的過程就不描述了,有很多種上傳方式的致讥,例如使用ftp工具仅仆、Xshell的文件上傳工具、cmd命令下使用SCP命令上傳垢袱、寶塔面板上使用上傳文件功能墓拜、WinSCP工具等等,這里選tar.gz.
Oracle官網(wǎng)下載:Java Downloads | Oracle
image.png
2.解壓
tar -zxvf jdk-8u333-linux-x64.tar.gz
3.編輯配置文件,加bin到環(huán)境變量中
vim /etc/profile
export JAVA_HOME=/usr/local/java/jdk1.8.0_333
export JRE_HOME=$JAVA_HOME/jre
export PATH=$JAVA_HOME/bin:$JRE_HOME/bin:$PATH
export CLASSPATH=$CLASSPATH:.:$JAVA_HOME/lib:$JRE_HOME/lib
#按esc退出按wq!保存退出
#刷新配置
source /etc/profile
Redis應(yīng)用之Session共享
----正常情況下请契,Httpsession是通過Servlet容器創(chuàng)建并管理的咳榜,創(chuàng)建成功之后都是保存在內(nèi)存中,如果開發(fā)者需要對(duì)項(xiàng)目進(jìn)行橫向擴(kuò)展搭建集群爽锥,那么可以利用一些硬件或者軟件工具來做負(fù)載均衡涌韩,此時(shí),來自同一用戶的HTTP請(qǐng)求就有可能被分發(fā)到不同的實(shí)例上氯夷,如何保存各個(gè)實(shí)例之間的session的同步就成為一個(gè)必須解決的問題臣樱。使用Redis 解決共享問題的原理非常簡單,就是把原本存儲(chǔ)在不同服務(wù)器上的session拿出來放在一個(gè)獨(dú)立的服務(wù)器上腮考。
----當(dāng)一個(gè)請(qǐng)求到達(dá)Nginx服務(wù)器后雇毫,首先進(jìn)行請(qǐng)求分發(fā),假設(shè)請(qǐng)求被real server處理了踩蔚,real server 1在處理請(qǐng)求時(shí)棚放,無論是存儲(chǔ)在session還是讀取session,都去操作session服務(wù)器而不是操作自身內(nèi)存中的session馅闽,其它real server在處理請(qǐng)求時(shí)也是如此飘蚯,這樣就實(shí)現(xiàn)了session共享。
舉例:IDEA創(chuàng)建一個(gè)springboot項(xiàng)目'session'
- 導(dǎo)包
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.session</groupId>
<artifactId>spring-session-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
- application.properties
spring.redis.database=0
spring.redis.host=redis服務(wù)的ip
spring.redis.port=6379
spring.redis.password=123456
spring.redis.jedis.pool.max-active=8
spring.redis.jedis.pool.max-idle=8
spring.redis.jedis.pool.max-wait=-1ms
spring.redis.jedis.pool.min-idle=0
- 創(chuàng)建一個(gè)控制類來測試
這里創(chuàng)建了兩個(gè)方法福也,一個(gè)save接口用來向session中存儲(chǔ)數(shù)據(jù)局骤,還有一個(gè)get是用來從session中獲取數(shù)據(jù),這里注入了項(xiàng)目啟動(dòng)端口server.port,主要是為了區(qū)分到底是哪個(gè)服務(wù)器提供服務(wù)暴凑。另外庄涡,雖然還是操作的Httpsession,但實(shí)際上Httpsession容器已經(jīng)被透明替換,真正的session存儲(chǔ)在Redis服務(wù)器上搬设。
@RestController
public class HelloController {
@Value("${server.port}")
String port;
@PostMapping("/save")
public String saveName(String name, HttpSession session) {
session.setAttribute("name", name);
return port;
}
@GetMapping("/get")
public String getName(HttpSession session) {
return port + ":"
+ session.getAttribute("name").toString();
}
}
- 項(xiàng)目配好后,打包撕捍,上傳到Centos服務(wù)器上(Redis,nginx都在這上面拿穴,已配好jdk,Redis,Nginx,tomcat),后,再執(zhí)行如下代碼忧风。
注意:nohup表示不掛斷程序運(yùn)行默色,最終端口關(guān)閉后,程序仍可在運(yùn)行狮腿,&表示讓程序在后臺(tái)運(yùn)行腿宰,接下來就要運(yùn)行負(fù)截均衡了呕诉,redis了。分別開兩個(gè)窗口運(yùn)行之吃度,不要關(guān) 閉窗口
nohup java -jar session0.0.1-SNAPSHOT.jar --server.port=8084 &
nohup java -jar session0.0.1-SNAPSHOT.jar --server.port=8085 &
5.nginx的配置/etc/nginx/conf.d/proxy.conf,重啟nignx -s reload,8088是代理服務(wù)器甩挫,常用的8080端口服務(wù)器做了文件服務(wù)器。
upstream sessionshare-server {
server 139.198.11.194:8084 weight=1;
server 139.198.11.194:8085 weight=1;
}
server {
listen 8088;
server_name localhost;
location / {
proxy_pass http://sessionshare-server;
proxy_redirect default;
}
}
運(yùn)行后椿每,可以分別登錄ip+端口號(hào)測試伊者,當(dāng)然了瀏覽器不好測試,沒有post請(qǐng)求间护,我們用了postman工具來測試
nginx搭建
簡介
Nginx(engine x)是一個(gè)高性能的HTTP反向代理服務(wù)器亦渗,也是一個(gè)IMAP/POP3/SMTP服務(wù)器,可以作為一個(gè)WEB服務(wù)器進(jìn)行網(wǎng)站發(fā)布汁尺,也可以用為反向代理服務(wù)器進(jìn)行負(fù)載均衡的實(shí)現(xiàn)---如與Tomcat(web服務(wù)器)一起實(shí)現(xiàn)負(fù)載均衡法精。
特點(diǎn)
占用內(nèi)存少,并發(fā)能力強(qiáng)痴突。
- 安裝nginx方式一(通過yum安裝--這是我喜歡的方式)
1.把 nginx 的源加入 yum 中
直接通過 yum install nginx 肯定是不行的,因?yàn)閥um沒有nginx搂蜓,所以首先把 nginx 的源加入 yum 中。
rpm -ivh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
2.查看nginx信息
yum info nginx
3.使用yum安裝ngnix
yum install nginx
4.啟動(dòng)與重啟nginx,并查看安裝目錄分別在那里
#systemctl start/restart nginx.service
//重啟服務(wù)還可以
#/usr/sbin/nginx -s reload
#whereis nginx
nginx: /usr/sbin/nginx /usr/lib64/nginx /etc/nginx /usr/share/nginx /usr/share/man/man8/nginx.8.gz
5.查看nginx版本,測試成功否
nginx -v
curl -i localhost
6.nginx配置文件位置在/etc/nginx/conf.d/default.conf可以更改端口號(hào)
- 安裝nginx方式二(源碼安裝)
----下載:http://nginx.org/en/linux_packages.html
1.源代碼安裝(要make makeinstall)
image.png
需要編譯
./configure -----make----- makeinstall
wget https://nginx.org/download/nginx-1.14.0.tar.gz
tar -zxvf nginx-1.14.0.tar.gz
cd nginx-1.14.0
./configure
make
make install
如果遇到安裝nginx報(bào)錯(cuò):make: *** 沒有規(guī)則可以創(chuàng)建“default”需要的目標(biāo)“build”苞也。 停止洛勉。再重新配置如迟,編譯
yum -y install gcc openssl openssl-devel pcre-devel zlib zlib-devel
./configure
make
make install
安裝成功后,自動(dòng)安裝到/usr/local中此再,進(jìn)sbin目錄下。執(zhí)行nginx的啟動(dòng)文件
whereis nginx #可查看安裝到哪里
cd /usr/local/nginx/sbin
./nginx
測試:nginx安裝成功否输拇,默認(rèn)端口號(hào):80
ip回車 或 localhost回車贤斜,
啟動(dòng)策吠,重啟猴抹,停止nginx服務(wù)器
cd /usr/local/nginx/sbin
./nginx
./nginx -s stop
./ nginx reload
查看端口號(hào)占用情況蟀给,如果沒有netstat命令,則yum install net-tools
netstat -tnlp | grep 80 #因?yàn)閚ginx默認(rèn)端口號(hào)是80择克,這里我們查一下他有沒有被其它應(yīng)用占用
2.配成http服務(wù)器即虛擬主機(jī)(/usr/local/nginx/conf/nginx.conf)
-------Centos中安裝nginx,它安裝的文件全在一個(gè)目錄下道偷,不同于ubuntu勺鸦,分布在不同的文件夾中换途,這一點(diǎn)centos要方便多了刽射。
nginx.conf
#user nobody; 工作進(jìn)程的數(shù)量誓禁,可以根據(jù)cpu的核心總數(shù)來設(shè)置
worker_processes 1;
#error_log logs/error.log;
error_log /usr/local/nginx/error.log warn;
#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;
#是否調(diào)用sendfile函數(shù)來輸出文件
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
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;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
}
1.創(chuàng)建虛擬主機(jī)目錄(/tmp/nginxhttp/mus和sms)
cd /tmp
mkdir nginxhttp
cd nginxhttp
mkdir mus
mkdir sms
pwd #查看當(dāng)前目錄絕對(duì)路徑
2.實(shí)現(xiàn)虛擬主機(jī)的三種方式
基于不同的IP----最常用
基于不同的域名----要購買域名
基于不同的端口號(hào)--一般用于測試用辫继,實(shí)際生產(chǎn)中不可能這樣用
-----本人在這是基于域名的創(chuàng)建虛擬主機(jī),當(dāng)然了不是買的炮车,是本地虛擬的
首先:修改配置文件,這里我從安裝目錄的conf目錄找到nginx.conf瘦穆。再打開修改,這里我們只要在http{}中加兩個(gè)server即可难审。注:一定要養(yǎng)成備份的習(xí)慣。
cd /usr/local/ngixn/conf #nginx安裝自動(dòng)部署到該位置派昧,不是我做的
-----------------------------------------------------------------------------------------------
server {
listen 80;
server_name www.mus.com; #www.mus.com和www.sms.com 都是自設(shè)域名
location / {
root /tmp/nginxhttp/mus; # root /tmp/nginxhttp/sms;
index index.html index.htm;
}
}
server {
listen 80;
server_name www.sms.com; #www.mus.com和www.sms.com 都是自設(shè)域名
location / {
root /tmp/nginxhttp/sms; # root /tmp/nginxhttp/sms;
index index.html index.htm;
}
}
- 重啟nginx服務(wù)器,并把對(duì)應(yīng)的vmware nat ip地址復(fù)制好秆吵,可以用ip addr查詢之纳寂,放進(jìn) c:/windows/system32/drivers/etc/hosts文件中毙芜,如下所示腋粥。
cd /usr/local/nginx/sbin
./nginx -s reload
---------------------------------------------------------------windows中hosts------------------------------------------------------------
192.168.72.135 www.ums.com
192.168.72.135 www.sms.com
4.測試:https://www.ums.com/http://www.sms.com回車
Nginx作為文件服務(wù)器下載功能
- 上傳通過ftp或sftp(Winscp工具)
- 下載功能實(shí)現(xiàn)
首先:,創(chuàng)建文件目錄 (/home/xiong/www/image/)
cd /home/xiong
mkdir www
cd www
mkdir image
然后: 配置nginx.conf文件(/usr/local/nginx/conf/nginx.conf,安裝nginx時(shí)自動(dòng)放在這的)(yum安裝方式/etc/nginx/conf.d/default.conf,但nginx/nginx.conf有一個(gè)/etc/nginx/conf.d/*.conf;會(huì)把conf.d中的所有conf文件包含 進(jìn)來)展辞,此時(shí)只在主服務(wù)器上加如下代碼即可罗珍。
//原來的代碼
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
}
}
-------------------------------------------------------------------修改后的代碼---------------------------------------------------------------------
server {
listen 80;
server_name localhost;
location / {
root html;
index index.html index.htm;
}
location /image {
root /home/xiong/www;
autoindex on;
autoindex_exact_size off;
autoindex_localtime on;
}
再然后:重啟nginx
cd /usr/local/nginx/sbin
./nginx -s reload
最后測試:
http://ip/image回車
問題
403 Forbidden
分析:
一、由于啟動(dòng)用戶和nginx工作用戶不一致所致,所以要修改nginx.conf
ps aux | grep "nginx: worker process" | awk'{print $1}' #查看啟動(dòng)nginx的啟動(dòng)用戶通殃,這里是root,但這處修改沒用
二曲聂,權(quán)限問題齐疙,如果nginx沒有web目錄的操作權(quán)限贞奋,也會(huì)出現(xiàn)403錯(cuò)誤轿塔。
chmod -R 777 /home/xiong/
Nginx用作反向代理
先介紹一下正向代理
-----正向代理現(xiàn)實(shí)中一般用在翻墻勾缭,作緩存用的,加速資源的訪問采驻,如公司里很多人會(huì)訪問新浪網(wǎng)礼旅,可以把它緩存到代理服務(wù)器痘系,對(duì)客戶端上網(wǎng)進(jìn)行認(rèn)證授權(quán)汰翠,還有上網(wǎng)行為管理,如翻墻時(shí)原始服務(wù)器并不知道誰訪問他了佛纫, 這樣可以陒藏客戶信息呈宇。
反向代理簡介
------有這么一個(gè)場景,現(xiàn)在有一個(gè)服務(wù)器集群(分布式)蜈漓,當(dāng)客端訪問服務(wù)器時(shí)迎变,它是怎樣訪問到服務(wù)器的衣形,處理方法:在服務(wù)器集群前有一個(gè)反向代理服務(wù)器,用于分配訪問資源訪問句狼,反向代理不是客戶端設(shè)置的腻菇。分配策略(負(fù)載均衡)后面介紹筹吐。
反向代理作用:
1.實(shí)現(xiàn)負(fù)載均衡,提高處理和響應(yīng)速度
2.保證內(nèi)網(wǎng)的安全洋侨。隱藏服務(wù)器信息希坚。
Nginx作為反向代理的配置
首先:我們用tomcat作為內(nèi)部原始服務(wù)器,啟動(dòng)之锅知,端口號(hào):8081,8080已被nginx用了昌妹,這是在云主機(jī)上寮現(xiàn)的飞崖,沒有備案固歪,所以80 443端被禁用了逢防。
----下載后tomcat winscp傳到Llinux的/usr/local中
解壓,進(jìn)入解壓后的目錄局嘁,修改端口號(hào)為8081(根目錄下的conf目錄下的server.xml),執(zhí)行
./startup.sh
tar -zxvf apache-....tar.gz
mv apache- tomcat9 #改一下根目錄名悦昵,好方便操作
cd tomcat9/conf
vi server.xml
注意:修改好端號(hào)8080--8081好后,關(guān)閉一于服務(wù)器枚赡,再啟動(dòng)下
# ./usr/local/tomcat9/bin/shutdown.sh
# ./usr/local/tomcat9/bin/startup.sh
測試tomcat啟動(dòng)否,有用否.我現(xiàn)在有兩種 方法卢肃。
方法一莫湘,---可以從Eclipse(或STS)中運(yùn)行并部署到tomcat的webapps中的項(xiàng)目(upload)文件全部上傳到linux中安裝的根錄目錄下的webapps中,
方法二示弓,----ECLIPSE中右點(diǎn)項(xiàng)目(upload)---export-----web.war文件,導(dǎo)出為擴(kuò)展名為war的文件囱皿,再上傳到linux中的webapps中,該文件還可以自定義名字铆帽。再關(guān)閉tomcat服務(wù)萨螺,又啟動(dòng)tomcat服務(wù)慰技,相當(dāng)于重啟服務(wù)
------------------測試:http://ip:端口號(hào)/upload/index.jsp
然后:進(jìn)入nginx安裝根目錄下掏颊,找到conf中的配置文件nginx.conf,最好先備份一份乌叶。再編輯,直接在主server后面加一個(gè)服務(wù),yum方式安裝的nginx在/etc/nginx/conf.d/中復(fù)制default.conf為proxy.conf乐横。
#后臺(tái)服務(wù)器列表
upstream tomcat-server {
server 139.198.11.194:8081; #真實(shí)服務(wù)器的端口8081
}
server {
listen 8086; #代理端口8086
server_name localhost;
location / {
proxy_pass http://tomcat-server; #指定代理的后臺(tái)服務(wù)器
}
}
這里我們的niginx的8086端口服務(wù)反向代理tomcat的服務(wù)。注意:自定義的服務(wù)名中不能有_下劃線催什。
最后:重啟nginx溯乒,測試裆悄。nginx命令安裝時(shí)自動(dòng)載入到環(huán)境變量中光稼,所以在任何地方都可以重啟
nginx -s reload
瀏覽器中輸入:http://ip:代理端口/
nginx實(shí)現(xiàn)負(fù)載均衡
------將任務(wù)通過某種策略分配到多個(gè)實(shí)體上去,實(shí)現(xiàn)負(fù)載在不同實(shí)體間的平衡冰垄。
-------在nginx實(shí)現(xiàn)反向代理的基礎(chǔ)上實(shí)現(xiàn)負(fù)截均衡
首先:模擬建設(shè)多臺(tái)實(shí)在服務(wù)器(tomcat服務(wù)),即我們?cè)谝慌_(tái)機(jī)上復(fù)制多個(gè)(2個(gè))tomcat安裝目錄蝴罪,分別改成不同的端口號(hào)(8082,8083欢搜。8081被原始tomcat用了),還有《Server
cd /usr/local
cp -r tomcat9 taobao1
cp -r tomcat9 taobao2
vi taobao1/conf/server.xml
vi taobao1/conf/server.xml
分別兩處要改的地方:
//1處,原port="8005"
<Server port="8012" shutdown="SHUTDOWN">
<Listener className="org.apache.catalina.startup.VersionLoggerListener" />
//2處 原port="80"
<Connector port="8082" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" />
然后:到/etc/nginx/conf.d/中復(fù)制原來做的反向代理服務(wù)的proxy.conf 文件唧领,或者就用這個(gè)文件配負(fù)載均衡權(quán)重
upstream tomcat-server {
server 139.198.11.194:8081 weight=3;
server 139.198.11.194:8082 weight=1;
server 139.198.11.194:8083 weight=1;
}
server {
listen 8086;
server_name localhost;
location / {
proxy_pass http://tomcat-server;
#proxy_set_header Host $http_host;
}
}
測試負(fù)載均衡效果:http://ip:8086/,不停的刷新網(wǎng)頁, 可以看到權(quán)重大的運(yùn)行的次數(shù)多
注:這里我改了tomcat的默認(rèn)項(xiàng)目下面的index.jsp頁面,該頁面在webapps/ROOT/下
nginx實(shí)現(xiàn)動(dòng)靜分離
--------一個(gè)項(xiàng)目中有動(dòng)態(tài)資源和靜態(tài)資源受啥,tomcat作為動(dòng)態(tài)服務(wù)器是很歷害的居暖,但他處理靜態(tài)資源不如nginx,這就要我們把靜態(tài)資源放到nignx服務(wù)器中省骂,再用nginx反向代理傳給tomcat服務(wù)器钞澳。
- 在是上面反向代理示例中的例子代碼改下,我們先把訪問靜態(tài)資源的功能做空(即設(shè)置的目錄中沒有資源)逃延,看看頁面效果讽膏。
upstream tomcat-server {
server 139.198.11.194:8081 weight=3;
server 139.198.11.194:8082 weight=1;
server 139.198.11.194:8083 weight=1;
}
server {
listen 8086;
server_name localhost;
#處理動(dòng)態(tài)資源
location / {
proxy_pass http://tomcat-server;
#proxy_set_header Host $http_host;
}
#處理靜態(tài)資源
location ~ .*\.(js|css|ico|png|eot|jpg|ttf|svg|woff) { #這樣寫上的話俐末,服務(wù)找這些文件卓箫,會(huì)被動(dòng)態(tài)服務(wù)器拒絕
root /usr/local/www/static;
}
}
測試效果,http://192.168.1.13:8086/,如果沒有出現(xiàn)如下效果旅急,則有可能是網(wǎng)頁沒有清理緩存數(shù)據(jù)。
- 我們把靜態(tài)資源復(fù)制到/usr/local/www/static中
cd /usr/local/tomcat9/webapps/ROOT/
cp tomcat.svg tomcat.css /usr/local/www/static/
測試效果:http://192.168.1.13:8086/如下圖所示迫摔。如果沒有出現(xiàn)如下效果,極有可能是沒有讀取權(quán)限辖众,為nginx啟動(dòng)會(huì)自建用戶nginx,所以設(shè)置static下所有文件有777權(quán)限
static]#chmod 777 *
nginx高并發(fā)處理
- 負(fù)載均衡:集群
- 動(dòng)靜分離啤它,自已沒有nignx服務(wù)器的話,可以考慮有很多網(wǎng)絡(luò)上的第三方服務(wù)CDN(內(nèi)容分發(fā)網(wǎng)絡(luò))塌碌,比如bootstartp
- 限流,流量控制(把超出的流量放進(jìn)一個(gè)隊(duì)列接剩,如果隊(duì)列裝不下,可以直接拒絕)
- 降級(jí):服務(wù)降載(比如鹃两,日志不記錄正確的日志)
- 緩存:以空間換時(shí)間舀凛,提高系統(tǒng)效率