域名环境
使用switchhost配置域名的IP地址
下载地址:https://sm.myapp.com/original/System/SwitchHosts_v0.2.2.1785.7z
配置如下:
# gulimall
192.168.241.130 gulimall.com
负载均衡
nginx配置文件详情
一个nginx.conf会包含多个server快(conf.d内*.conf文件)
拷贝配置文件 cp default.conf gulimall.conf
在http块配置上游服务器,在nginx.conf中,server地址为windows的IP,并吧gulimalls配置文件包括进来
查看文档(Using nginx as HTTP load balancer) https://nginx.org/en/docs/
配置监听的域名,以及 / 请求转发的域名
配置网关断言规则
文档:https://docs.spring.io/spring-cloud-gateway/docs/2.2.4.RELEASE/reference/html/=》Route Predicate Factories=》The Host Route Predicate Factory
- id: host-router # 配置nginx代理过来的请求
uri: lb://gulimall-product
predicates: # 什么情况下路由给它
- Host=gulimalls.com,item.gulimalls.com # 默认前端项目都带上api前缀
注意:此段配置要放在所有微服务路由后面,因为gulimall.com会拦截访问api请求:gulimalls.com/api/product/attr/list
nginx反向代理时会丢失host信息,配置server块,转发时同时转发hosts
动静分离
简介
1.出于教学目的未采用前后端分离,使用视图
2.静态资源放在nginx中(图片、js、css[视图所需的资源])
3.动态资源(需要服务器处理的请求)独立部署、运行、升级
4.nginx接收用户请求转发至网关,网关作统一限流、鉴权,网关再转发给各模块
静态资源的配置
流程
thymeleaf官方文档,查看语法
https://www.thymeleaf.org/documentation.html
可以下载pdf文档:using thymeleaf
1.添加依赖
<!--模板引擎thymeleaf-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
2.关闭缓存【调试时可以实时查看效果】
spring:
thymeleaf:
cache: false
suffix: .html # 默认配置
prefix: /templates/ # 默认配置
3.复制.html到templates文件夹内
复制静态资源到static文件夹内
4.修改controller包名为app,表示rest请求
新建web包,表示请求跳转
将静态资源放入docker种nginx映射的文件中
此时前端需要访问建泰资源加上/static路径即可