微服务demo

这篇博客详细介绍了如何创建和配置微服务项目,包括父工程的建立、微服务提供者和消费者的创建,以及如何进行工程重构,将重复代码打包成api-common模块并引入到各个微服务中,以减少冗余。
摘要由CSDN通过智能技术生成

目录

1、父工程

1.1、项目创建

1.2、依赖配置

2、微服务模块

2.1、创建微服务提供者

2.1.1、搭建Module模块

2.1.2、修改微服务POM依赖

2.1.3、修改yaml配置文件

2.1.4、主启动

2.1.5、业务类

2.1.6、测试

2.2、创建微服务提供者 

2.2.1、创建module模块,与2.1.1一致

2.2.2、修改POM文件

2.2.3、写application.yaml配置文件

2.2.4、主启动

2.2.5、业务类

2.2.6、测试(同2.1.6)

3、工程重构

3.1、创建 api-common module 用于收录重复代码

3.2、在api-common中加入重复代码

3.3、将api-common打包存入本地仓库

 3.4、在需要调用的微服务模块中的POM文件,引入依赖

3.5、在微服务中使用


1、父工程

1.1、项目创建

1、创建maven工程

 2、删除src等多余目录

1.2、依赖配置

 1)父工程打包方式 <packaging>pom</packaging>

 2)<dependencyManagement>标签:

  • 锁定版本+子module不用写groupId和version
  • dependencyManagement只声明依赖,并不是引入,子项目需要显式的声明需要引用的依赖
  • 当子项目中写了该依赖项,不指定具体的版本信息时,才会从父项目中继承该项
<?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.uclass.springcloud</groupId>
  <artifactId>cloud</artifactId>
  <version>1.0-SNAPSHOT</version>
  <modules>
    <module>Provider-Payment8001</module>
    <module>Consumer-Order80</module>
      <module>Api-Commons</module>
  </modules>
  <packaging>pom</packaging>

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

  <!-- 父工程,子模块继承,作用:锁定版本+子module不用写groupId和version
       dependencyManagement只是声明依赖,并不是引入,子项目需要显式的声明需要引用的依赖
       同时,只有当子项目中写了该依赖项,不指定具体的版本信息时,才会从父项目中继承该项-->
  <dependencyManagement>
    <dependencies>
      <dependency>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-project-info-reports-plugin</artifactId>
        <version>3.2.2</version>
      </dependency>

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

      <dependency>
        <groupId>com.alibaba.cloud</groupId>
        <artifactId>spring-cloud-alibaba-dependencies</artifactId>
        <version>2.1.0.RELEASE</version>
        <type>pom</type>
        <scope>import</scope>
      </dependency>
      <!--mysql-->
      <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>${mysql.version}</version>
      </dependency>
      <!-- druid-->
      <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>2.1.4</version>
      </dependency>
      <!--junit-->
      <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>${junit.version}</version>
      </dependency>
      <!--log4j-->
      <dependency>
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
        <version>${log4j.version}</version>
      </dependency>

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

  </dependencyManagement>

  <!--  热部署插件-->
  <build>
    <plugins>
      <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值