实战一:建立springcloud基础项目结构

一,,创建父工程,用于管理项目依赖版本

  1,new -> project -> maven

  2,修改pom.xml,这里管理了springboot,springcloud,springcloud alibaba 三个的版本

<dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>2.2.9.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>

            <dependency>
                <groupId>com.alibaba.cloud</groupId>
                <artifactId>spring-cloud-alibaba-dependencies</artifactId>
                <version>2.2.1.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>

            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Hoxton.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

  3,大各子module中用到的版本号,可放到父工程的properties中统一管理

<properties>
        <java.version>1.8</java.version>
        <lombok.version>1.18.12</lombok.version>
        <mybatisplus.version>3.4.0</mybatisplus.version>
    </properties>

二,创建通用模块(common模块),注:模块名取common好像有问题,建议不要用common作为模块名。

  1,new -> module -> maven,父工程选第一步中中新建的工程,或者直接使用Spring Initializr,然后修改pom文件为下方格式

  2,修改pom.xml:parent为父工程,依赖为通用依赖项,其它保持不变。

注1:parent为父工程
<parent>
<groupId>net.biui</groupId>
<artifactId>springcloud_study</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>

注2:各子module通用依赖项写在这里
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>

<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>${mybatisplus.version}</version>
</dependency>

<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

三,创建微服务模块 

  1,new -> module -> maven,父工程选第一步中中新建的工程,或者直接使用Spring Initializr,然后修改pom文件为下方格式

  2,修改pom.xml

注1:parent为父工程
<
parent> <groupId>net.biui</groupId> <artifactId>springcloud_study</artifactId> <version>1.0-SNAPSHOT</version> </parent>

<dependencies>
  注2:引入通用模块
<dependency> <groupId>net.biui</groupId> <artifactId>studycommon</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency>
  注3: 下方为本微服务需要用到的依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency> </dependencies>

四,在idea上运行微服务时,可能出现的问题

  1,如果出现通用模块jar文件未找到,则先install通用模块

  2,报junit的错误,可以直接在idea上跳过test步骤,直接install; 或删除java/test下的文件

posted @ 2020-09-02 15:24  王东波  阅读(292)  评论(0编辑  收藏  举报