基於springcloud搭建項目-公共篇(二)


上一篇已經寫過如何搭建注冊中心eureka,這一篇主要是搭建一些公共的api接口服務,並把實體類單獨拿出來放到一個服務上引用,比較簡單的

1、首先。創建一個實體類服務,這樣就不用在每個服務里創建實體了,只需要把實體的依賴加入到pom.xml中就可以引用,

可以實現各服務間實體共享,這里的服務命名為study-entity,不需要添加任何配置,結構如下:

 

 2、在pom.xml中加入依賴,在依賴中要依賴父項目,這樣一個封裝實體的服務就創建好了

<!--父項目依賴-->
<parent>
        <groupId>com.study</groupId>
        <artifactId>study-cloud</artifactId>
        <version>1.0-SNAPSHOT</version>
        <relativePath>../study-cloud/pom.xml </relativePath><!-- lookup parent from repository -->
    </parent>
   

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
        <dependency>
            <groupId>javax.persistence</groupId>
            <artifactId>persistence-api</artifactId>
            <version>1.0.2</version>
        </dependency>
    </dependencies>

3、創建api服務,命名為study-api,主要是是用於管理接口,供其他服務之間可以相互調用,結構同上,不需要配置文件

 4、添加study-api的pom.xml依賴,引入父項目依賴,和實體依賴

    <!--父項目-->
    <parent>
        <groupId>com.study</groupId>
        <artifactId>study-cloud</artifactId>
        <version>1.0-SNAPSHOT</version>
        <relativePath>../study-cloud/pom.xml </relativePath><!-- lookup parent from repository -->
    </parent>
<groupId>com.study</groupId> <artifactId>study-api</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>study-api</name> <description>Demo project for Spring Boot</description> <dependencies> <!--實體引入--> <dependency> <groupId>com.study</groupId> <artifactId>study-entity</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies>

5、最后,看下study-cloud父工程,創建項目時,以study-cloud作為父項目的都會在父項目中出現自動引入

 

 6、創建完成,這兩個服務以后編碼的過程中會用到,所以不想寫測試代碼,沒有測試結果哦


免責聲明!

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



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