Nginx+SpringCloud Gateway搭建項目訪問環(huán)境
點擊關(guān)注公眾號,Java干貨及時送達(dá)

現(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
首先拉取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文件夾移動進(jìn)去:
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將剛才準(zhǔn)備好的nginx文件夾與nginx容器內(nèi)的文件夾作一個一一映射。
準(zhǔn)備SpringBoot應(yīng)用
創(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/:

添加網(wǎng)關(guān)
一般情況下,Nginx都會配合網(wǎng)關(guān)一起使用,這是因為微服務(wù)一般會做集群部署,此時請求就無法準(zhǔn)確地決定具體該轉(zhuǎn)向哪個服務(wù),而是應(yīng)該由其自動負(fù)載到每個服務(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)在訪問 http://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:// 表示負(fù)載均衡,效果如下:

現(xiàn)在的請求就是經(jīng)過Nginx再經(jīng)過網(wǎng)關(guān)最后到達(dá)的具體服務(wù)。
本文作者:汪偉俊 為Java技術(shù)迷專欄作者 投稿,未經(jīng)允許請勿轉(zhuǎn)載
往 期 推 薦
3、互聯(lián)網(wǎng)人為什么學(xué)不會擺爛
4、為什么國外JetBrains做 IDE 就可以養(yǎng)活自己,國內(nèi)不行?區(qū)別在哪?
點分享
點收藏
點點贊
點在看





