首页 > 编程语言 > Nginx+SpringCloud Gateway搭建项目访问环境
2022
02-10

Nginx+SpringCloud Gateway搭建项目访问环境

现如今的项目开发基本都是微服务方式,导致一个系统中会有很多的服务,每个模块都对应着不同的端口,为了方便访问,通常会让某个服务绑定一个域名,比如商品服务:product.xxx.com;订单服务:order.xxx.com,此时可以使用Nginx来搭建一个域名访问环境,基于前后端分离开发的项目经常会遇到跨域问题,使用Nginx也能轻松解决。

安装Nginx

首先拉取nginx的镜像:

docker pull nginx:1.10

然后随意地启动一个nginx实例:

docker run -p 80:80 --name nginx -d nginx:1.10

启动该nginx实例的目的是将nginx中的配置文件复制出来:

docker container cp nginx:/etc/nginx .

这样当前目录下就会产生一个nginx文件夹,将其先重命名为conf,然后再创建一个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容器内的文件夹作一个一一映射。

准备SpringBoot应用

创建一个SpringBoot应用,并引入依赖:

<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/ :

 

现在的需求是通过访问域名 myspringboot.com 也能够访问到该页面,所以来修改Windows中的hosts文件:

192.168.66.10 myspringboot.com

这段内容的作用是当访问 myspringboot.com 时,实际访问的是192.168.66.10,即我们的Linux系统。

此时来到Linux,配置一下Nginx,在conf.d目录下创建的配置文件都会被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;
    }
}

这段配置表示监听myspringboot.com:80而来的请求,若是访问 / 则会被其中的location /处理,将该请求转发至http://192.168.0.105:8080/:

 

添加网关

一般情况下,Nginx都会配合网关一起使用,这是因为微服务一般会做集群部署,此时请求就无法准确地决定具体该转向哪个服务,而是应该由其自动负载到每个服务上,所以,应该加入网关来实现这一功能。

创建一个SpringBoot应用,并引入依赖:

<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中:

spring:
  cloud:
    nacos:
      discovery:
        server-addr: 192.168.66.10:8848
  application:
    name: MyGateway
server:
  port: 9000

此时修改Nginx的配置,首先在http块添加对网关的配置:

upstream my_gateway{
  server 192.168.0.105:9000 # 配置网关的地址
}

然后修改server块:

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又会将其交给网关处理,我们再来配置一下网关,使其将请求转发给指定的服务处理:

spring:
  cloud:
    gateway:
      routes:
        - id: springbootdemo_route
          uri: lb://SpringBootDemo
          predicates:
            - Path=/**

这段配置会监听所有的请求,因为Path的值为 /** ,当请求来到网关时,直接将其转交给MySpringBoot服务, lb:// 表示负载均衡,效果如下: image.png 现在的请求就是经过Nginx再经过网关最后到达的具体服务。

到此这篇关于Nginx+SpringCloud Gateway搭建项目访问环境的文章就介绍到这了,更多相关Nginx SpringCloud Gateway搭建内容请搜索自学编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持自学编程网!

编程技巧