您好,登錄后才能下訂單哦!
這篇文章主要講解了“Nginx+SpringCloud Gateway怎么搭建項目訪問環(huán)境”,文中的講解內(nèi)容簡單清晰,易于學(xué)習(xí)與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學(xué)習(xí)“Nginx+SpringCloud Gateway怎么搭建項目訪問環(huán)境”吧!
安裝Nginx
準備SpringBoot應(yīng)用
添加網(wǎng)關(guān)
現(xiàn)如今的項目開發(fā)基本都是微服務(wù)方式,導(dǎo)致一個系統(tǒng)中會有很多的服務(wù),每個模塊都對應(yīng)著不同的端口,為了方便訪問,通常會讓某個服務(wù)綁定一個域名,比如商品服務(wù):product.xxx.com;訂單服務(wù):order.xxx.com,此時可以使用Nginx來搭建一個域名訪問環(huán)境,基于前后端分離開發(fā)的項目經(jīng)常會遇到跨域問題,使用Nginx也能輕松解決。
首先拉取nginx的鏡像:
docker pull nginx:1.10
然后隨意地啟動一個nginx實例:
docker run -p 80:80 --name nginx -d nginx:1.10
啟動該nginx實例的目的是將nginx中的配置文件復(fù)制出來:
docker container cp nginx:/etc/nginx .
這樣當(dāng)前目錄下就會產(chǎn)生一個nginx文件夾,將其先重命名為conf,然后再創(chuàng)建一個nginx文件夾,并將conf文件夾移動進去:
mv nginx conf mkdir nginx mv conf/ nginx/
然后正式啟動一個新的nginx實例:
docker run -p 80:80 --name nginx \ -v /mydata/nginx/html:/usr/share/nginx/html \ -v /mydata/nginx/logs:/var/log/nginx \ -v /mydata/nginx/conf:/etc/nginx \ -d nginx:1.10
將剛才準備好的nginx文件夾與nginx容器內(nèi)的文件夾作一個一一映射。
創(chuàng)建一個SpringBoot應(yīng)用,并引入依賴:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> </dependency>
將其注冊到Nacos中:
spring: cloud: nacos: discovery: server-addr: 192.168.66.10:8848 application: name: SpringBootDemo
啟動項目,訪問 http://localhost:8080/ :
現(xiàn)在的需求是通過訪問域名 myspringboot.com 也能夠訪問到該頁面,所以來修改Windows中的hosts文件:
192.168.66.10 myspringboot.com
這段內(nèi)容的作用是當(dāng)訪問 myspringboot.com 時,實際訪問的是192.168.66.10,即我們的Linux系統(tǒng)。
此時來到Linux,配置一下Nginx,在conf.d目錄下創(chuàng)建的配置文件都會被Nginx自動掃描到:
cd /mydata/nginx/conf/conf.d touch mysb.conf
添加配置:
server { listen 80; server_name myspringboot.com; location / { proxy_pass http://192.168.0.105:8080/; } error_page 500 502 503 504 /50x.html; location = /50x.html { root /usr/share/nginx/html; } }
這段配置表示監(jiān)聽myspringboot.com:80而來的請求,若是訪問 / 則會被其中的location /處理,將該請求轉(zhuǎn)發(fā)至http://192.168.0.105:8080/:
一般情況下,Nginx都會配合網(wǎng)關(guān)一起使用,這是因為微服務(wù)一般會做集群部署,此時請求就無法準確地決定具體該轉(zhuǎn)向哪個服務(wù),而是應(yīng)該由其自動負載到每個服務(wù)上,所以,應(yīng)該加入網(wǎng)關(guān)來實現(xiàn)這一功能。
創(chuàng)建一個SpringBoot應(yīng)用,并引入依賴:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-gateway</artifactId> </dependency> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> </dependency>
同樣需要將網(wǎng)關(guān)注冊到Nacos中:
spring: cloud: nacos: discovery: server-addr: 192.168.66.10:8848 application: name: MyGateway server: port: 9000
此時修改Nginx的配置,首先在http塊添加對網(wǎng)關(guān)的配置:
upstream my_gateway{ server 192.168.0.105:9000 # 配置網(wǎng)關(guān)的地址 }
然后修改server塊:
server { listen 80; server_name myspringboot.com; location / { proxy_pass http://my_gateway; # 轉(zhuǎn)發(fā)至網(wǎng)關(guān) } error_page 500 502 503 504 /50x.html; location = /50x.html { root /usr/share/nginx/html; } }
現(xiàn)在訪問 myspringboot.com/ ,請求會被交給Nginx,Nginx又會將其交給網(wǎng)關(guān)處理,我們再來配置一下網(wǎng)關(guān),使其將請求轉(zhuǎn)發(fā)給指定的服務(wù)處理:
spring: cloud: gateway: routes: - id: springbootdemo_route uri: lb://SpringBootDemo predicates: - Path=/**
這段配置會監(jiān)聽所有的請求,因為Path的值為 /** ,當(dāng)請求來到網(wǎng)關(guān)時,直接將其轉(zhuǎn)交給MySpringBoot服務(wù), lb:// 表示負載均衡,效果如下: image.png 現(xiàn)在的請求就是經(jīng)過Nginx再經(jīng)過網(wǎng)關(guān)最后到達的具體服務(wù)。
感謝各位的閱讀,以上就是“Nginx+SpringCloud Gateway怎么搭建項目訪問環(huán)境”的內(nèi)容了,經(jīng)過本文的學(xué)習(xí)后,相信大家對Nginx+SpringCloud Gateway怎么搭建項目訪問環(huán)境這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是億速云,小編將為大家推送更多相關(guān)知識點的文章,歡迎關(guān)注!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。