在IDEA編輯器中建立Spring Cloud的子項目包(構建微服務)


本文介紹在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地址(先模仿,再應用,有格式更好的文檔)
 
 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM