本次实现的目标
- 整体项目框架的搭建。
- 动静分离的基本实现,将JD的静态页面通过Nginx的配置实现访问。
整体框架
各模块名词解释
src/main/resources/statics
存放所有静态资源的的目录,为了后期打包方便,所以将静态资源放在了当前项目中。因为要使用前后端分离的模式开发整个工程,所有该目录可以放置在任何位置。
SpringBootMicroservices
它继承了SpringBoot父级依赖,本身依赖了org.springframework.cloud,后续所有的微服务都作为该模块的子模块。例如:用户微服务、订单微服务、图片上传微服务等等。
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>JD_ALL</artifactId>
<groupId>liwen.zhao</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>SpringBootMicroservices</artifactId>
<groupId>liwen.zhao</groupId>
<version>1.0-SNAPSHOT</version>
<!--dependencyManagement中导入springcloud的依赖版本资源-->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Edgware.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
</project>
- JDCommons 作为微服务的通用模块,提供了通用的Json返回值封装、JavaBean、通用类库等等。
JDSpringCloudParent
它继承了SpringBoot父级依赖,本身依赖了org.springframework.cloud,微服务框架相关的模块都作为该模块的子模块。例如:Zuul网关、Eureka注册中心、Ribbon等等。
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<groupId>liwen.zhao</groupId>
<artifactId>JD_ALL</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>JDSpringCloudParent</artifactId>
<packaging>pom</packaging>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Edgware.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
</project>
最终在IDEA中体现的结构是这样的:
动静分离
在Nginx中,配置虚拟服务器:
如果对Nginx不熟悉,可以看我的另一篇文章Nginx 使用详解
以下的配置表示,只要在浏览器访问localhost/jd,将最终访问本地项目根目录的src/main/resources/statics/index.html
server {
listen 80;
server_name localhost;
#访问localhost/jd,访问项目根目录的src/main/resources/statics/index.html
location ^~/jd {
alias D:/AllCodes/bigdata/JD_ALL/src/main/resources/statics;
index index.html index.htm;
}
}
最终实现的效果
总结
在Nginx的配置中,遇到了一些坑。
在nginx 请求处理部分,也就是location块中,root 和 alias 有些细微的差别:root与alias主要区别在于nginx如何解释location后面的uri,这会使两者分别以不同的方式将请求映射到服务器文件上。
这里的alias表示,使用alias路径替换location路径。也就是说你访问的是http://localhost/jd/,最终指向的是:D:/AllCodes/bigdata/JD_ALL/src/main/resources/statics/index.html
location ^~/jd {
alias D:/AllCodes/bigdata/JD_ALL/src/main/resources/statics;
index index.html index.htm;
}
root的处理结果是:root路径+location路径。也就是说你访问的是http://localhost/jd/,最终指向的是:D:/AllCodes/bigdata/JD_ALL/src/main/resources/statics/jd/index.html
location ^~/jd {
root D:/AllCodes/bigdata/JD_ALL/src/main/resources/statics;
index index.html index.htm;
}
代码地址:https://gitee.com/zhaoliwen/JD_ALL.git