一、SpringCloud_Alibaba项目搭建

1. 搭建父工程

  1. eclipse --> file --> new --> Maven project


    搭建父工程
  2. 删除src目录
  3. 修改pom文件
  • 指定父工程

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.13.RELEASE</version>
        <relativePath/>
    </parent>
    
  • 依赖版本锁定

      <!-- 依赖版本管理 -->
      <!-- spring-cloud版本可以去 https://start.spring.io 根据springBoot自己查看 -->
      <properties>
        <java.version>1.8</java.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <spring-cloud.version>Greenwich.SR5</spring-cloud.version>
        <spring-cloud-alibaba.version>2.1.0.RELEASE</spring-cloud-alibaba.version>
      </properties>
      <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>
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-alibaba-dependencies</artifactId>
            <version>${spring-cloud-alibaba.version}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
      </dependencies>
    </dependencyManagement>
    
  • 最终父工程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>
    
    <groupId>com.test</groupId>
    <artifactId>springcloud-alibaba</artifactId>
    <version>1.0.0-SNAPSHOT</version>
    <packaging>pom</packaging>
    <name>springcloud-alibaba</name>
    <description>springcloud-alibaba 学习</description>
    
    <!-- 父工程 -->
    <parent>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-parent</artifactId>
      <version>2.1.14.RELEASE</version>
      <relativePath/>
    </parent>
    
    <!-- 依赖版本管理 -->
    <!-- spring-cloud版本可以去 https://start.spring.io 根据springBoot自己查看 -->
    <properties>
      <java.version>1.8</java.version>
      <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
      <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
      <spring-cloud.version>Greenwich.SR5</spring-cloud.version>
      <spring-cloud-alibaba.version>2.1.2.RELEASE</spring-cloud-alibaba.version>
    </properties>
    <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>
          <dependency>
              <groupId>com.alibaba.cloud</groupId>
              <artifactId>spring-cloud-alibaba-dependencies</artifactId>
              <version>${spring-cloud-alibaba.version}</version>
              <type>pom</type>
              <scope>import</scope>
          </dependency>
        </dependencies>
      </dependencyManagement>
     </project>
    
  1. 创建基础模块 -- common(存放实体类)
  • 创建maven项目
  • 修改pom文件
    <?xml version="1.0"?>
    <project
      xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"
      xmlns="http://maven.apache.org/POM/4.0.0"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
      <modelVersion>4.0.0</modelVersion>
      <parent>
        <groupId>com.test</groupId>
        <artifactId>springcloud-alibaba</artifactId>
        <version>1.0.0-SNAPSHOT</version>
      </parent>
      <artifactId>shop-common</artifactId>
      <name>shop-common</name>
      <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
      </properties>
      <dependencies>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        <!-- Mybatis-Plus -->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.3.2</version>
        </dependency>
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-generator</artifactId>
            <version>3.3.2</version>
        </dependency>
        <dependency>
            <groupId>org.apache.velocity</groupId>
            <artifactId>velocity-engine-core</artifactId>
            <version>2.2</version>
        </dependency>
        <!-- hutool -->
        <dependency>
            <groupId>cn.hutool</groupId>
            <artifactId>hutool-all</artifactId>
            <version>5.3.5</version>
        </dependency>
        <!--引入swagger -->
        <!-- 排除springfox-swagger2 引入的swagger-annotations、swagger-models 1.5.20版本,手动引入1.5.21版本的jar, 
            在使用@ApiModelProperty注解在字段上时,如果字段的类型为Long或是int类型, 那么程序启动后,访问swagger-ui.html的页面,程序会报错: 
            java.lang.NumberFormatException: For input string: "" -->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.9.2</version>
            <exclusions>
                <exclusion>
                    <groupId>io.swagger</groupId>
                    <artifactId>swagger-annotations</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>io.swagger</groupId>
                    <artifactId>swagger-models</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.9.2</version>
        </dependency>
        <dependency>
            <groupId>io.swagger</groupId>
            <artifactId>swagger-annotations</artifactId>
            <version>1.5.21</version>
        </dependency>
        <dependency>
            <groupId>io.swagger</groupId>
            <artifactId>swagger-models</artifactId>
            <version>1.5.21</version>
        </dependency>
        <!-- druid -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>1.1.22</version>
        </dependency>
      </dependencies>
    </project>
    
  1. 创建业务微服务模块
  • 创建maven moudle模块
  • 导入依赖
```
<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.test</groupId>
  <artifactId>springcloud-alibaba</artifactId>
  <version>1.0.0-SNAPSHOT</version>
</parent>
<artifactId>shop_product</artifactId>
<name>shop_product</name>
<description>商品微服务</description>

<dependencies>
  <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
  </dependency>
  <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
        <scope>runtime</scope>
        <optional>true</optional>
  </dependency>
  <dependency>
      <groupId>com.test</groupId>
      <artifactId>shop-common</artifactId>
      <version>1.0.0-SNAPSHOT</version>
  </dependency>
  <!-- log4j2 -->
    <!-- Spring Boot官方文档中解释,在启用spring-boot-starter后会默认使用logback作为日志框架, 
        本项目选用log4j2,因此需要全局exclude掉Spring Boot默认日志 -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
        <exclusions>
            <exclusion>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-logging</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-log4j2</artifactId>
    </dependency>
    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-web</artifactId>
    </dependency>
  </dependencies>
</project>
```
  • 创建SpringBoot主类
package com.test.shop.product;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class ShopProductApplication {

 public static void main(String[] args) {
     SpringApplication.run(ShopProductApplication.class, args);

 }

}
  • 添加配置文件
# server
server:
port: 8011
servlet:
  context-path: /shop-product
# log4j2
logging:
config: classpath:conf/log4j2.xml
level:
  com.test.shop.product.dao: debug
  
# spring
spring: 
profiles:
  active: dev
# spring注册MBean
jmx:
  # jmx管理bean,开启
  enabled: true
  server: mbeanServer
  # 每个项目对应的jmx域名
  default-domain: shop-product
#devtools  -- SpringBoot 热部署插件 devtools ,同时启动多个Application时,控制台会报警告
devtools:
  livereload:
    port: 35736

# mybatis
mybatis-plus:  
type-aliases-package: com.test.shop.common.entity
  # 指定mybatis映射文件的位置
mapper-locations: classpath:mapper/*.xml
configuration: 
  # 开启驼峰命名转换(开启后需要注意字段写法,不然返回值为null)
  map-underscore-to-camel-case: true
global-config:
  db-config:
    id-type: AUTO
  • 创建必要的接口和实现类(controller service dao)