2.从零开始搭建基于SpringCloud的京东整站_框架搭建

本次实现的目标

  1. 整体项目框架的搭建。
  2. 动静分离的基本实现,将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

下一篇:
3.从零开始搭建基于SpringCloud的京东整站_文件上传微服务

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值