現如今的項目開發基本都是微服務方式,導致一個系統中會有很多的服務,每個模塊都對應著不同的端口,為了方便訪問,通常會讓某個服務綁定一個域名,比如商品服務:product.xxx.com;訂單服務:order.xxx.com,此時可以使用Nginx來搭建一個域名訪問環境,基于前后端分離開發的項目經常會遇到跨域問題,使用Nginx也能輕松解決。
安裝Nginx
首先拉取nginx的鏡像:
1
|
docker pull nginx:1.10 |
然后隨意地啟動一個nginx實例:
1
|
docker run -p 80:80 --name nginx -d nginx:1.10 |
啟動該nginx實例的目的是將nginx中的配置文件復制出來:
1
|
docker container cp nginx: /etc/nginx . |
這樣當前目錄下就會產生一個nginx文件夾,將其先重命名為conf,然后再創建一個nginx文件夾,并將conf文件夾移動進去:
1
2
3
|
mv nginx conf mkdir nginx mv conf/ nginx/ |
然后正式啟動一個新的nginx實例:
1
2
3
4
5
|
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容器內的文件夾作一個一一映射。
準備SpringBoot應用
創建一個SpringBoot應用,并引入依賴:
1
2
3
4
5
6
7
8
9
10
11
12
|
< 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中:
1
2
3
4
5
6
7
|
spring: cloud: nacos: discovery: server-addr: 192.168.66.10:8848 application: name: SpringBootDemo |
啟動項目,訪問 http://localhost:8080/ :
現在的需求是通過訪問域名 myspringboot.com 也能夠訪問到該頁面,所以來修改Windows中的hosts文件:
192.168.66.10 myspringboot.com
這段內容的作用是當訪問 myspringboot.com 時,實際訪問的是192.168.66.10,即我們的Linux系統。
此時來到Linux,配置一下Nginx,在conf.d目錄下創建的配置文件都會被Nginx自動掃描到:
1
2
|
cd /mydata/nginx/conf/conf .d touch mysb.conf |
添加配置:
1
2
3
4
5
6
7
8
9
10
11
12
13
|
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 ; } } |
這段配置表示監聽myspringboot.com:80而來的請求,若是訪問 / 則會被其中的location /處理,將該請求轉發至http://192.168.0.105:8080/:
添加網關
一般情況下,Nginx都會配合網關一起使用,這是因為微服務一般會做集群部署,此時請求就無法準確地決定具體該轉向哪個服務,而是應該由其自動負載到每個服務上,所以,應該加入網關來實現這一功能。
創建一個SpringBoot應用,并引入依賴:
1
2
3
4
5
6
7
8
9
10
11
12
|
< 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 > |
同樣需要將網關注冊到Nacos中:
1
2
3
4
5
6
7
8
9
|
spring: cloud: nacos: discovery: server-addr: 192.168.66.10:8848 application: name: MyGateway server: port: 9000 |
此時修改Nginx的配置,首先在http塊添加對網關的配置:
1
2
3
|
upstream my_gateway{ server 192.168.0.105:9000 # 配置網關的地址 } |
然后修改server塊:
1
2
3
4
5
6
7
8
9
10
11
12
13
|
server { listen 80; server_name myspringboot.com; location / { proxy_pass http: //my_gateway ; # 轉發至網關 } error_page 500 502 503 504 /50x .html; location = /50x .html { root /usr/share/nginx/html ; } } |
現在訪問 myspringboot.com/ ,請求會被交給Nginx,Nginx又會將其交給網關處理,我們再來配置一下網關,使其將請求轉發給指定的服務處理:
1
2
3
4
5
6
7
8
|
spring: cloud: gateway: routes: - id: springbootdemo_route uri: lb://SpringBootDemo predicates: - Path=/** |
這段配置會監聽所有的請求,因為Path的值為 /** ,當請求來到網關時,直接將其轉交給MySpringBoot服務, lb:// 表示負載均衡,效果如下: image.png 現在的請求就是經過Nginx再經過網關最后到達的具體服務。
到此這篇關于Nginx+SpringCloud Gateway搭建項目訪問環境的文章就介紹到這了,更多相關Nginx SpringCloud Gateway搭建內容請搜索服務器之家以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持服務器之家!
原文鏈接:https://juejin.cn/post/6993563090604261406