本文介绍在IDEA编辑器中建立Spring Cloud的子项目包
总共分为5个包:
外层使用maven quickstart建立,子modules直接选择了springboot
图1-1
 
第一步, 建立eureka
第二步, 建立第三方数据提供层.(仅仅模拟数据提供处),
第三步, 建立数据获取层,这部分是数据获取中层. 也许中层服务就是这么来的
第四步, 建立数据获取redis层,这层把单独的数据获取单独出来,可以想象一下获取货品数据和获取员工数据的不在一个springboot中,Are you clear ?
 
图1-1表示的是在IDEA中,建立子Modules的操作,快捷键为ctrl+shift+alt+S

 
为了方便,我这里建里面的modules时,直接选取了springboot: (使用maven也可以)
图1-2
 
图1-2表示了在总项目里点击绿色+号后新建Spring Initializr

 
完成:
图1-3
 
data-service 可以理解为既要获取学生数据也要获取餐厅食堂数据的服务层, 在这一层里,将数据放入了缓存,进行刷新啊,删除啊,等其它操作
eureka-server 不解释了,这是服务注册中心,你可以理解为一个盆,用来装其它服务的,其它服务理解为毛巾,肥皂,洗发液
third-part-data-service 可以理解为数据库提供层,只是在其static静态资源下放了一些.json文件
util-boot 这个是用来单独放工具类的,公用的工具类,不然都放入各微服务中即可 (可以省略)
 
图1-3表示了通过maven和Spring Initializr建立出来的微服务分包分层结构

 
第一步要将几个模块进行pom文件的改造.
首先看之前的我整理的项目的外层pom文件: (父级)
 
紫色的modelVersion开始指定了该项目标识 (父级的)
往下的parent指定了父级的父级是谁,(是springboot的模块)
再往下浅绿的是指定版本配置文件
再往下蓝色的很清晰,是依赖 (这里有的其它子项目都可以享有)
再往下的蓝色指定springcloud的依赖
最下面橘红色指明了要共享cloud服务的子模块名词 (他们的artifactId)
 
<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">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.zq.springcloud</groupId>
    <artifactId>springcloudzq</artifactId>
    <version>1.0-SNAPSHOT</version>
    <name>springcloudzq</name>
    <packaging>pom</packaging>
                                               
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.6.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
 
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
        <spring-cloud.version>Greenwich.SR1</spring-cloud.version>
    </properties>
 
    <dependencies>
        <!--hutool-->
        <dependency>
            <groupId>cn.hutool</groupId>
            <artifactId>hutool-all</artifactId>
            <version>4.3.1</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
 
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
 
    <modules>
        <module>eureka-server</module>
        <module>data-service</module>
        <module>third-part-index-data-proj</module>
        <module>util-boot</module>
    </modules>
 
</project>
 
 

 
下面第二步尝试加入两个新子模块,用来单独获取data-service已经获取到的学生数据和餐厅数据. 
 
the-code-service 可以理解为只回去学生数据的服务层 通过缓存获取code
the-json-service 可以理解为 餐厅食堂明细数据的服务层 通过缓存获取json
 
建立完毕后,需要在外层pom文件中加入依赖:
<modules>
    <module>eureka-server</module>
    <module>data-service</module>
    <module>third-part-index-data-proj</module>
    <module>util-boot</module>
    <module>the-code-service</module>
    <module>the-json-service</module>
</modules>
 
图1-4
 
之后需要将每个子模块的pom文件进行更改,以和父pom文件对应起来.
 
图1-4表示了加入单独的两个模块后的分层结构,父pom文件即图中可见的pom.xml文件

 
第三步操作将每个子Modules中的pom文件进行修改,和父pom关联
首先看到父pom文件中最上面的artifactId为 springcloudzq
<artifactId>springcloudzq</artifactId>
那在子Modules中,声明的parent就指明这个artifactId
<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">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>com.zq.springcloud</groupId>
        <artifactId>springcloudzq</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <artifactId>data-service</artifactId>
红色为xml文件依赖
蓝色是版本信息
绿色即要进行和父项目关联的parent
紫色为子模块的artifactId
 
之后还有dependencies 和</project>结束标签
 
现在拿新增的两个模块的pom文件作为演示:
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>com.zq.springcloud</groupId>
        <artifactId>springcloudzq</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <artifactId>the-code-service</artifactId>
 
    <dependencies>
        <!-- springboot web -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!-- eureka-client -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <!-- redis -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>
 
    </dependencies>
 
 
</project>
 
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>com.zq.springcloud</groupId>
        <artifactId>springcloudzq</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <artifactId>the-json-service</artifactId>
    
    <dependencies>
        <!-- springboot web -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!-- eureka-client -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <!-- redis -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>
 
    </dependencies>
 
 
</project>
 
?为什么两个pom中依赖文件是相同的,因为有些模块例如eureka server,util-boot所依赖的不是web或是client,redis之类.
 

 
第四步
将两个新模块 the-code-service, the-json-service 新加业务层
启动测试:
启动eureka-server, third-part, data-service, the-code, the-json
 
图1-5
 
图1-5表示启动eureka-server后访问其域名端口查看eureka监控,此时没有服务注册进来.
 
启动redis,如果不启动,因为在third-part中进行端口的监测,会报错:
Connected to the target VM, address: '127.0.0.1:50599', transport: 'socket'
Disconnected from the target VM, address: '127.0.0.1:50599', transport: 'socket'
检查到端口6379 未启用,判断redis服务器没有启动,本服务无法使用,故退出
检查到端口8760已启用,eureka服务运行中.
Process finished with exit code 1
启动后正常:
Connected to the target VM, address: '127.0.0.1:50691', transport: 'socket'
检查到端口6379已启用,redis服务运行中.检查到端口8760已启用,eureka服务运行中.[限时2秒输入]第三方数据默认端口8090启动[默认端口:8090]
2019-08-28 09:42:30.934  INFO 12692 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.cloud.autoconfigure.ConfigurationPropertiesRebinderAutoConfiguration' of type [org.springframework.cloud.autoconfigure.ConfigurationPropertiesRebinderAutoConfiguration$$EnhancerBySpringCGLIB$$f4e8d998] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
 
 
  .   ____          _            __ _ _
/\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot ::        (v2.1.6.RELEASE)
 
 
启动成功后,再次查看eureka页面监控:
图1-6
 
图1-6表示新启动的服务已经注册到了eureka服务中心.
 
图1-7
 
当data-service注册进eureka后可以调用端口8001的服务请求了.
图1-8
 
 
图1-7表示数据提供层注册进了eureka服务中心.
图1-8表示通过8001端口获取数据.

 
第五步查看新增的模块获取redis数据是否正常
图1-9
 
图1-9表示此时redis中已经加入了code和json的数据
 
继续启动the-code-service,the-json-service,看数据是否获取正常:
 
图2-1
 
有bug,莫慌,只要是 resolve 或是 cast 或是 type 出现的都是转型的错误,而这里不是转型错误,而是redis中的数据的包结构和获取时的包结构名称不一致导致.
com.zq.dataservice.bean  //获取数据的data-service包结构
com.zq.thecodeservice.bean  //the-code-service包结构
com.zq.thejsonservice.bean  //the-json-service包结构
改为一致的
com.zq.dataservice.bean
图2-2
 
图2-1表示有bug了,此时获取数据出现了错误.
图2-2表示redis中当时存入时通过data-service时的实体类Index所在的包结构.
 
图2-3
 
图2-3表示修复bug后获取数据正常.

 
工具:diffmerge
提取码:s24m
 
推荐使用diffmerge软件,进行文件夹比较。把你自己做的项目文件夹,和我的可运行项目文件夹进行比较。 
这个软件很棒的,可以知道文件夹里哪两个文件不对,并且很明显地标记出来 
 
 
https://github.com/deadzq/springcloudzq-  demo地址(先模仿,再应用,有格式更好的文档)
 
 

在IDEA编辑器中建立Spring Cloud的子项目包(构建微服务)的更多相关文章

  1. 基于Spring Cloud和Netflix OSS构建微服务,Part 2

    在上一篇文章中,我们已使用Spring Cloud和Netflix OSS中的核心组件,如Eureka.Ribbon和Zuul,部分实现了操作模型(operations model),允许单独部署的微 ...

  2. 基于Spring Cloud和Netflix OSS 构建微服务-Part 1

    前一篇文章<微服务操作模型>中,我们定义了微服务使用的操作模型.这篇文章中,我们将开始使用Spring Cloud和Netflix OSS实现这一模型,包含核心部分:服务发现(Servic ...

  3. 今天介绍一下自己的开源项目,一款以spring cloud alibaba为核心的微服务架构项目,为给企业与个人提供一个零开发基础的微服务架构。

    LaoCat-Spring-Cloud-Scaffold 一款以spring cloud alibab 为核心的微服务框架,主要目标为了提升自己的相关技术,也为了给企业与个人提供一个零开发基础的微服务 ...

  4. spring cloud 入门,看一个微服务框架的「五脏六腑」

    Spring Cloud 是一个基于 Spring Boot 实现的微服务框架,它包含了实现微服务架构所需的各种组件. 注:Spring Boot 简单理解就是简化 Spring 项目的搭建.配置.组 ...

  5. SpringCloud(10)使用Spring Cloud OAuth2和JWT保护微服务

    采用Spring Security AOuth2 和 JWT 的方式,避免每次请求都需要远程调度 Uaa 服务.采用Spring Security OAuth2 和 JWT 的方式,Uaa 服务只验证 ...

  6. spring cloud(学习笔记)微服务启动错误(1)

    今天下午在启动spring cloud微服务的时候,报了这个错误: Error starting ApplicationContext. To display the auto-configurati ...

  7. Spring Cloud(Dalston.SR5)--Zuul 网关-微服务集群

    通过 url 映射的方式来实现 zuul 的转发有局限性,比如每增加一个服务就需要配置一条内容,另外后端的服务如果是动态来提供,就不能采用这种方案来配置了.实际上在实现微服务架构时,服务名与服务实例地 ...

  8. Spring Cloud(1):微服务简介

    架构的演进: 1.十年前:用户->单一服务器->单一数据库(支持十万级用户) 2.五年前:用户->负载均衡器->多台服务器->缓存集群->主从数据库(支持百万级用户 ...

  9. 使用Spring Cloud OAuth2和JWT保护微服务

    采用Spring Security AOuth2 和 JWT 的方式,避免每次请求都需要远程调度 Uaa 服务.采用Spring Security OAuth2 和 JWT 的方式,Uaa 服务只验证 ...

随机推荐

  1. - Java中boolean类型占用多少个字节 MD

    目录 目录 Java中boolean类型占用多少个字节 1个bit(1位) 1个Byte(1字节,8位) 4个Byte(4字节,32位) 分析 官方文档中的描述 Markdown版本笔记 我的GitH ...

  2. .Net Core 获取应用物理路径的常见问题

    如果要得到传统的ASP.Net应用程序中的相对路径或虚拟路径对应的服务器物理路径,只需要使用使用Server.MapPath()方法来取得Asp.Net根目录的物理路径. 但是在Asp.Net Cor ...

  3. dotnet core系列之Background tasks with hosted services (后台任务)

    这篇简单讲asp.net core 中的后台任务 用到的包: Microsoft.AspNetCore.App metapackage 或者加入 Microsoft.Extensions.Hostin ...

  4. Ubuntu18.04 配置网卡、替换软件源

    2019/10/29, Ubuntu Server 18.04 摘要:Ubuntu Server 18.04 采用netplan作为网络配置管理,修改IP使其连上网络,修改替换软件源 修改网卡配置 首 ...

  5. Spring Boot 的自动配置探究、自制一个starter pom

    //TODO @Conditional @Condition

  6. SAP HANA学习资料大全 Simple Finane + Simple Logisitic [非常完善的学习资料汇总]

    Check out this SDN blog if you plan to write HANA Certification exam http://scn.sap.com/community/ha ...

  7. tomcat重启session不失效问题

    本地写代码每次重启都要重新登录浪费了很多时间,如何重启不用重新登录呢,只要让tomcat在关闭时将session写入文件中,在启动时从文件中读取session即可. 只需在conf/context.x ...

  8. 微信小程序分页加载列表

    1.假设加载的数据为 2.wxml <view class="page"> <view class="page__bd"> <vi ...

  9. dm9000网卡 S3C2440

    配置U-Boot支持dm9000网卡 原理图 # vi drivers/net/Makefile obj-$(CONFIG_DRIVER_NET_CS8900) += cs8900.o obj-$(C ...

  10. iOS 关于NavigationController返回的一些笔记

    1.理解NavigationController返回机制 一般NavigationController下的子view只有一层或者有很多层,子view返回最顶层则可以直接用 [self.navigati ...