Spring Cloud 零基础篇

前言

1.课程内容(SpringCloud+SpringCloud Alibaba)

2.技术要求
java 8 + maven + git、github + Nginx + RabbitMQ + SpringBoot 2.0

1.微服务架构零基础理论入门

1.1.微服务架构概述

  • 什么是微服务

    • Martin Fowler,Mar 2014:微服务架构是一种架构模式,它提倡将单一应用程序划分为一组小的服务,服务之间互相协调、互相配合,为用户提供最终价值。每个服务运行在其独立的进程中,服务与服务间采用轻量级的通信机制互相协作(通常是基于HTTP协议的RESTful API)。每个服务都围绕着具体业务进行构建,并且能够被独立的部署到生产环境、类生产环境等。另外,应当尽量避免统一的、集中式的服务管理机制,对具体的一个服务而言,应根据业务上下文,选择合适的语言、工具对其进行构建。
  • 主题词01:95后数字化生活-落地维度
    生活->技术:一个厂家提供还是多个厂家提供?一个厂家提供兼容性好,多个厂家提供要定义接口规范

  • 主题词02:分布式微服务架构-落地维度

1.2.Spring Cloud简介

  • 是什么?
    SpringCloud=分布式微服务架构的一站式解决方案,是多种微服务架构落地技术的集合体,俗称微服务全家桶
    猜猜SpringCloud这个大集合里有多少种技术?

SpringCloud俨然已成为微服务开发的主流技术栈,在国内开发者社区非常火爆。
“微”力十足,互联网大厂微服务架构案例

京东的:

阿里的:

京东物流的:

1.3.Spring Cloud技术栈

  • 各技术栈功能介绍

  • 总结

1.4.Spring Boot和Spring Cloud版本选型

1.Spring Cloud和Spring Boot版本对应关系:

2.使用的版本

  • Spring Cloud: Hoxton.SR12
  • Spring Boot: 2.3.12.RELEASE
  • Spring Cloud Alibaba: 2.2.6.RELEASE
  • JDK: 1.8
  • Maven: 3.6及以上
  • MySQL: 5.7及以上

1.5.Spring Cloud停更说明

  • 停更不停用
    • 被动修复bugs
    • 不再接受合并请求
    • 不再发布新版本

2.微服务架构编码构建

约定>配置>编码
首先,做一个最简单的“订单-支付模块”微服务(普通的Spring Boot工程),它将要慢慢的融入Spring Cloud H版、Spring Cloud Alibaba新技术,理论和动手相结合。
say say easy, do do hard!

2.1.IDEA新建project工作空间

2.1.1.搭建微服务cloud整体聚合父工程Project

a. New Project - maven工程 - create from archetype: maven-archetype-site

b. 聚合总父工程名字
c. Maven选版本
d. 工程名字
e. 字符编码 - Settings - File encoding
f. 注解生效激活 - Settings - Annotation Processors
g. Java编译版本选8
h. File Type过滤 - Settings - File Type

2.1.2.修改聚合父工程Project的pom.xml文件

<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.neo.springcloud</groupId>
  <artifactId>cloud2021</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>pom</packaging>

  <!-- 统一管理jar包版本 -->
  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
    <junit.version>4.12</junit.version>
    <log4j.version>1.2.17</log4j.version>
    <lombok.version>1.16.18</lombok.version>
    <mysql.version>5.1.47</mysql.version>
    <druid.version>1.1.16</druid.version>
    <mybatis.spring.boot.version>1.3.0</mybatis.spring.boot.version>
  </properties>

  <!-- 子模块继承之后,提供作用:锁定版本+子moudle不用写groupId和version -->
  <dependencyManagement>
    <dependencies>
      <!-- spring boot 2.3.12 -->
      <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-dependencies</artifactId>
        <version>2.3.12.RELEASE</version>
        <type>pom</type>
        <scope>import</scope>
      </dependency>
      <!-- spring cloud Hoxton.SR12 -->
      <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-dependencies</artifactId>
        <version>Hoxton.SR12</version>
        <type>pom</type>
        <scope>import</scope>
      </dependency>
      <!-- spring cloud alibaba 2.2.6.RELEASE -->
      <dependency>
        <groupId>com.alibaba.cloud</groupId>
        <artifactId>spring-cloud-alibaba-dependencies</artifactId>
        <version>2.2.6.RELEASE</version>
        <type>pom</type>
        <scope>import</scope>
      </dependency>
      <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>${mysql.version}</version>
      </dependency>
      <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>druid</artifactId>
        <version>${druid.version}</version>
      </dependency>
      <dependency>
        <groupId>org.mybatis.spring.boot</groupId>
        <artifactId>mybatis-spring-boot-starter</artifactId>
        <version>${mybatis.spring.boot.version}</version>
      </dependency>
    </dependencies>
  </dependencyManagement>

  <build>
    <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
      <plugins>
        <plugin>
          <artifactId>maven-clean-plugin</artifactId>
          <version>3.1.0</version>
        </plugin>
        <plugin>
          <artifactId>maven-site-plugin</artifactId>
          <version>3.7.1</version>
        </plugin>
        <plugin>
          <artifactId>maven-project-info-reports-plugin</artifactId>
          <version>3.0.0</version>
        </plugin>
      </plugins>
    </pluginManagement>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-site-plugin</artifactId>
        <configuration>
          <locales>en,fr</locales>
        </configuration>
      </plugin>
    </plugins>
  </build>

  <reporting>
    <plugins>
      <plugin>
        <artifactId>maven-project-info-reports-plugin</artifactId>
      </plugin>
    </plugins>
  </reporting>
</project>

2.1.3.Maven工程落地细节

1.Maven中的DependencyManagement和Dependencies的区别
dependencyManagement元素中指定的版本号。
例如在父项目里:

<dependencyManagement>
    <dependencies>
      <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.47</version>
      </dependency>
      ...
    </dependencies>
</dependencyManagement>

然后在子项目里就可以添加mysql-connector时可以不指定版本号,例如:

<dependencies>
  <dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
  </dependency>
</dependencies>

这样做的好处就是:如果有多个子项目都引用同一样依赖,则可以避免在每个使用的子项目里都声明一个版本号,这样当想升级或切换到另一个版本时,只需要在顶层父容器里更新,而不需要一个一个子项目的修改;另外如果某个子项目需要另外的一个版本,只需要声明version即可。

dependencyManagement里只是声明依赖,并不实现引入,因此子项目需要显示的声明需要用的依赖。
如果不在子项目中声明依赖,是不会从父项目中继承下来的;只是在子项目中写了该依赖项,并且没有指定具体版本,才会从父项目中继承该项,并且version和scope都读取自父pom;如果子项目中指定了版本号,那么会使用子项目中指定的jar版本。

2.Maven工程跳过单元测试
IDEA中点击“闪电”小按钮:

2.1.4.父工程执行mvn:install发布到Maven仓库方便子工程继承

2.2.Rest微服务工程构建

2.2.1.构建步骤

2.2.1.1.cloud-provider-payment8001微服务提供者支付Module模块

构建微服务模块:

  1. 建module
  2. 改POM
  3. 写YML
  4. 主启动
  5. 业务类

1.建完module后发现父pom.xml文件多了一个modules标签

2.改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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>cloud2021</artifactId>
        <groupId>com.neo.springcloud</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>cloud-provider-payment8001</artifactId>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring-boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>1.1.10</version>
        </dependency>
        <!-- mysql-connector-java -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        <!-- jdbc -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

</project>

3.写YML

server:
  port: 8001
spring:
  application:
    name: cloud-payment-service
  datasource:
    type: com.alibaba.druid.pool.DruidDataSource  # 当前数据源操作类型
    driver-class-name: org.gjt.mm.mysql.Driver  # mysql驱动包
    url: jdbc:mysql://localhost:3306/db2021?useUnicode=true&characterEncoding=utf-8&useSSL=false
    username: root
    password: root

mybatis:
  mapperLocations: classpath:mapper/*.xml
  type-aliases-package: com.neo.springcloud.entities  # 所有Entity别名类所在包

4.主启动

package com.neo.springcloud;

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

/**
 * @ClassName: PaymentMain8001
 * @Title: cloud2021
 * @Package: com.neo.springcloud
 * @Description:
 * @Author: Kisen
 * @Date: 2021/7/18 21:56
 */
@SpringBootApplication
public class PaymentMain8001 {
    public static void main(String[] args) {
        SpringApplication.run(PaymentMain8001.class, args);
    }
}

5.编写业务类
controller、service、dao、mysql

1)建表sql

-- db2021.payment definition

CREATE TABLE `payment` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT 'ID',
  `serial` varchar(200) DEFAULT '',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

2)定义Entity类和CommonResult类

package com.neo.springcloud.entities;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;

/**
 * @ClassName: Payment
 * @Title: cloud2021
 * @Package: com.neo.springcloud.entities
 * @Description:
 * @Author: Kisen
 * @Date: 2021/7/18 22:13
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Payment implements Serializable {

    private Long id;
    private String serial;
}
package com.neo.springcloud.entities;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * @ClassName: CommonResult
 * @Title: cloud2021
 * @Package: com.neo.springcloud.entities
 * @Description:
 * @Author: Kisen
 * @Date: 2021/7/18 22:16
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
public class CommonResult<T> {

    private Integer code;
    private String message;
    private T data;
}

3)定义dao接口

package com.neo.springcloud.dao;

import com.neo.springcloud.entities.Payment;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;

/**
 * @ClassName: PaymentDao
 * @Title: cloud2021
 * @Package: com.neo.springcloud.dao
 * @Description:
 * @Author: Kisen
 * @Date: 2021/7/18 22:21
 */
@Mapper
public interface PaymentDao {

    int create(Payment payment);

    Payment getPaymentById(@Param("id") Long id);
}

mybatis的映射文件PaymentMapper.xml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="com.neo.springcloud.dao.PaymentDao">

    <resultMap id="BaseResultMap" type="com.neo.springcloud.entities.Payment">
        <id column="id" property="id" jdbcType="BIGINT"/>
        <id column="serial" property="serial" jdbcType="VARCHAR"/>
    </resultMap>

    <insert id="create" parameterType="Payment" useGeneratedKeys="true" keyProperty="id">
        insert into payment(serial)
        values (#{serial})
    </insert>

    <select id="getPaymentById" parameterType="Long" resultMap="BaseResultMap">
        select * from payment where id=#{id}
    </select>

</mapper>

4)编写service类
接口类:

package com.neo.springcloud.service;

import com.neo.springcloud.entities.Payment;
import org.apache.ibatis.annotations.Param;

/**
 * @InterfaceName: PaymentService
 * @Title: cloud2021
 * @Package: com.neo.springcloud.service
 * @Description:
 * @Author: Kisen
 * @Date: 2021/7/18 22:59
 */
public interface PaymentService {

    public int create(Payment payment);

    public Payment getPaymentById(@Param("id") Long id);
}

实现类:

package com.neo.springcloud.service.impl;

import com.neo.springcloud.dao.PaymentDao;
import com.neo.springcloud.entities.Payment;
import com.neo.springcloud.service.PaymentService;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @ClassName: PaymentServiceImpl
 * @Title: cloud2021
 * @Package: com.neo.springcloud.service.impl
 * @Description:
 * @Author: Kisen
 * @Date: 2021/7/18 23:00
 */
@Service
public class PaymentServiceImpl implements PaymentService {

    @Resource
    private PaymentDao paymentDao;


    public int create(Payment payment) {
        return paymentDao.create(payment);
    }

    public Payment getPaymentById(@Param("id") Long id) {
        return paymentDao.getPaymentById(id);
    }
}

5)编写controller类

package com.neo.springcloud.controller;

import com.neo.springcloud.entities.CommonResult;
import com.neo.springcloud.entities.Payment;
import com.neo.springcloud.service.PaymentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @ClassName: PaymentController
 * @Title: cloud2021
 * @Package: com.neo.springcloud.controller
 * @Description:
 * @Author: Kisen
 * @Date: 2021/7/18 23:05
 */
@RestController
@Slf4j
public class PaymentController {

    @Resource
    private PaymentService paymentService;

    @PostMapping(value = "/payment/create")
    public CommonResult create(Payment payment) {
        int result = paymentService.create(payment);
        log.info("***插入结果:" + result);
        if (result > 0) {
            return new CommonResult(200, "插入数据库成功", result);
        } else {
            return new CommonResult(444, "插入数据库失败");
        }
    }

    @GetMapping(value = "/payment/get/{id}")
    public CommonResult getPaymentById(@PathVariable("id") Long id) {
        Payment payment = paymentService.getPaymentById(id);
        log.info("***插入结果:" + payment);
        if (payment != null) {
            return new CommonResult(200, "查询成功", payment);
        } else {
            return new CommonResult(444, "没有对应记录,查询ID:" + id);
        }
    }
}

6.启动PaymentMain8001类,测试
1). 使用postman工具模拟post,发送请求

2). IDEA中Run和Run DashBoard

通过修改IDEA的workspace.xml(项目路径.idea)的方式快速打开Run DashBoard窗口
在RunDashboard下面添加代码:

<component name="RunDashboard">
    <option name="configurationTypes">
      <set>
        <option value="SpringBootApplicationConfigurationType" />
      </set>
    </option>
  </component>
2.2.1.2.热部署Devtools

1.添加devtools到项目

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-devtools</artifactId>
    <scope>runtime</scope>
    <optional>true</optional>
</dependency>

2.添加插件到聚合父类总工程的pom.xml

<build>
    <finalName>cloud2021</finalName>
    <plugins>
      <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
        <configuration>
          <fork>true</fork>
          <addResources>true</addResources>
        </configuration>
      </plugin>
    </plugins>
<build

3.开启自动构建
Settings>Compiler>勾选:

4.更新值
ctrl+shift+alt+/键>Registry>勾选:

5.重启IDEA

2.2.1.3.cloud-consumer-order80微服务消费者订单Module模块

1.建cloud-consumer-order80

2.改POM
a. 继承父工程,pom文件中的g(groupId)和v(version)可以不用写
b. spring-boot-starter-web、spring-boot-starter-actuator是标配,必须要有

<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>cloud2021</artifactId>
        <groupId>com.neo.springcloud</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>cloud-consumer-order80</artifactId>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

</project>

3.写YML
a. 新建application.yml文件

server:
  port: 80

4.主启动
a. 新建主启动类OrderMain80

package com.neo.springcloud;

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

/**
 * @ClassName: OrderMain80
 * @Title: cloud2021
 * @Package: com.neo.springcloud
 * @Description:
 * @Author: Kisen
 * @Date: 2021/7/20 23:34
 */
@SpringBootApplication
public class OrderMain80 {
    public static void main(String[] args) {
        SpringApplication.run(OrderMain80.class, args);
    }
}

5.业务类
a. Order客户端应该只有Controller,没有Service、Dao,直接调用Payment服务端,就像我们直接调用腾讯、阿里的开放API
b. 拷贝Payment服务端entities实体类Payment、Json封装体CommonResult
c. 新建OrderController,两个服务之间调用最原始的阶段是httpClient,进化到现在使用restTemplate,它对httpClient进行了一层封装,实现了订单微服务和支付微服务之间的横向调用。

d. RestTemplate是什么?

RestTemplate提供了多种便捷访问远程HTTP服务的方法,是一种简单便捷的访问restful服务模板类,是Spring提供的用于访问Rest服务的客户端模板工具类
官网地址:https://docs.spring.io/spring-framework/docs/5.2.9.RELEASE/javadoc-api/org/springframework/web/client/RestTemplate.html
使用:
使用restTemplate访问restful接口非常的简单粗暴无脑。(url, requestMap, ResponseBean.class)这个三个参数分别代表REST请求地址、请求参数、HTTP响应被转换成的对象类型。

f. 要使用RestTemplate,需要将其注入到Spring容器中
新增配置类,注入RestTemplate:

package com.neo.springcloud.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;

/**
 * @ClassName: ApplicationContextConfig
 * @Title: cloud2021
 * @Package: com.neo.springcloud.config
 * @Description:
 * @Author: Kisen
 * @Date: 2021/7/21 0:08
 */
@Configuration
public class ApplicationContextConfig {

    @Bean
    public RestTemplate getRestTemplate() {
        return new RestTemplate();
    }
}

g. 订单微服务Controller层使用restTemplate调用支付微服务

package com.neo.springcloud.controller;

import com.neo.springcloud.entities.CommonResult;
import com.neo.springcloud.entities.Payment;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;

/**
 * @ClassName: OrderController
 * @Title: cloud2021
 * @Package: com.neo.springcloud.controller
 * @Description:
 * @Author: Kisen
 * @Date: 2021/7/21 0:11
 */
@RestController
@Slf4j
public class OrderController {

    public static final String PAYMENT_URL = "http://localhost:8001";

    @Resource
    private RestTemplate restTemplate;

    @GetMapping("/consumer/payment/create")
    public CommonResult create(Payment payment) {
        return restTemplate.postForObject(PAYMENT_URL + "/payment/create", payment, CommonResult.class);
    }

    @GetMapping("/consumer/payment/get/{id}")
    public CommonResult getPayment(@PathVariable("id") Long id) {
        return restTemplate.getForObject(PAYMENT_URL + "/payment/get/" + id, CommonResult.class);
    }
}

6.测试
a. 启动支付微服务、订单微服务
b. 访问支付微服务
1). 地址栏输入http://localhost/consumer/payment/get/2,查询支付信息

2). 地址栏输入http://localhost/consumer/payment/create?serial=ctuser,新建支付信息

返回信息显示插入数据库成功,但数据库插入了serial字段为NULL,原因是:支付微服务接口参数Payment没有添加@RequestBody注解

添加@RequestBody注解后,因为开启了devtools功能,支付微服务会自动热部署;重新访问新建支付信息URL,数据库插入成功!

2.2.1.4.工程重构

1.观察问题
从已有的支付微服务、订单微服务项目代码中不难发现,entities包下有重复的实体类Payment、CommonResult

于是,代码重复的部分我们可以将其抽取到公共的工程包,供大家统一调配使用

2.新建工程包cloud-api-commons
a. New Module

b. 修改pom.xml文件
添加如下依赖:

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
        <scope>runtime</scope>
        <optional>true</optional>
    </dependency>
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <optional>true</optional>
    </dependency>
    <!-- Hutool是一个小而全的Java工具类库,通过静态方法封装,降低相关API的学习成本,提高工作效率,使Java拥有函数式语言般的优雅,让Java语言也可以“甜甜的”。 -->
    <dependency>
        <groupId>cn.hutool</groupId>
        <artifactId>hutool-all</artifactId>
        <version>5.7.2</version>
    </dependency>
</dependencies>

3.拷贝Payment实体、CommonResult通用封装类到工程目录com.neo.springcloud.entities

4.将Maven打包发布上传到公用库,供其它工程调用
执行mvn clean、mvn install命令

5.分别改造订单微服务、支付微服务
1). 删除各自原先有的entities文件夹
2). pom.xml文件中各自引入cloud-api-commons通用包

<!-- 引入自己定义的api通用包,可以使用Payment支付Entity -->
<dependency>
    <groupId>com.neo.springcloud</groupId>
    <artifactId>cloud-api-commons</artifactId>
    <version>${project.version}</version>
</dependency>

3). 启动项目测试

2.2.1.5.目前工程样图

posted @ 2021-07-17 23:20  冰枫丶  阅读(177)  评论(1编辑  收藏  举报