在IDEA编辑器中建立Spring Cloud的子项目包(构建微服务)

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

 
为了方便,我这里建里面的modules时,直接选取了springboot: (使用maven也可以)
在IDEA编辑器中建立Spring Cloud的子项目包(构建微服务)在IDEA编辑器中建立Spring Cloud的子项目包(构建微服务)
图1-2
 
图1-2表示了在总项目里点击绿色+号后新建Spring Initializr

 
完成:
在IDEA编辑器中建立Spring Cloud的子项目包(构建微服务)在IDEA编辑器中建立Spring Cloud的子项目包(构建微服务)
图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>
 
在IDEA编辑器中建立Spring Cloud的子项目包(构建微服务)在IDEA编辑器中建立Spring Cloud的子项目包(构建微服务)
图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
 
在IDEA编辑器中建立Spring Cloud的子项目包(构建微服务)
图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页面监控:
在IDEA编辑器中建立Spring Cloud的子项目包(构建微服务)在IDEA编辑器中建立Spring Cloud的子项目包(构建微服务)
图1-6
 
图1-6表示新启动的服务已经注册到了eureka服务中心.
 
在IDEA编辑器中建立Spring Cloud的子项目包(构建微服务)在IDEA编辑器中建立Spring Cloud的子项目包(构建微服务)
图1-7
 
当data-service注册进eureka后可以调用端口8001的服务请求了.
在IDEA编辑器中建立Spring Cloud的子项目包(构建微服务)在IDEA编辑器中建立Spring Cloud的子项目包(构建微服务)
在IDEA编辑器中建立Spring Cloud的子项目包(构建微服务)
图1-8
 
 
图1-7表示数据提供层注册进了eureka服务中心.
图1-8表示通过8001端口获取数据.

 
第五步查看新增的模块获取redis数据是否正常
在IDEA编辑器中建立Spring Cloud的子项目包(构建微服务)在IDEA编辑器中建立Spring Cloud的子项目包(构建微服务)
图1-9
 
图1-9表示此时redis中已经加入了code和json的数据
 
继续启动the-code-service,the-json-service,看数据是否获取正常:
 
在IDEA编辑器中建立Spring Cloud的子项目包(构建微服务)在IDEA编辑器中建立Spring Cloud的子项目包(构建微服务)
在IDEA编辑器中建立Spring Cloud的子项目包(构建微服务)在IDEA编辑器中建立Spring Cloud的子项目包(构建微服务)
图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
在IDEA编辑器中建立Spring Cloud的子项目包(构建微服务)在IDEA编辑器中建立Spring Cloud的子项目包(构建微服务)
图2-2
 
图2-1表示有bug了,此时获取数据出现了错误.
图2-2表示redis中当时存入时通过data-service时的实体类Index所在的包结构.
 
在IDEA编辑器中建立Spring Cloud的子项目包(构建微服务)
在IDEA编辑器中建立Spring Cloud的子项目包(构建微服务)在IDEA编辑器中建立Spring Cloud的子项目包(构建微服务)
图2-3
 
图2-3表示修复bug后获取数据正常.

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