maven 聚合工程 用spring boot 搭建 spring cloud 微服务 模块式开发项目

时间:2023-03-08 23:17:59
maven 聚合工程 用spring boot 搭建 spring cloud 微服务 模块式开发项目

项目的简单介绍: 项目采用maven聚合工程 用spring boot 搭建 spring cloud的微服务 模块式开发

项目的截图:maven 聚合工程 用spring boot 搭建 spring cloud 微服务 模块式开发项目

搭建开始: 能上图 我少打字

1.首先搭建maven的聚合工程

  1.1创建聚合工程的父模块

  maven 聚合工程 用spring boot 搭建 spring cloud 微服务 模块式开发项目

  maven 聚合工程 用spring boot 搭建 spring cloud 微服务 模块式开发项目

  maven 聚合工程 用spring boot 搭建 spring cloud 微服务 模块式开发项目

  maven 聚合工程 用spring boot 搭建 spring cloud 微服务 模块式开发项目

  1.2设置父模块的POM文件

  主要是配置 spring boot版本,spring cloud 版本,和一些通用的依赖 比如这里的 lombok依赖

<?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.cloud</groupId>
<artifactId>cloud</artifactId>
<version>1.0-SNAPSHOT</version>
<!--packing默认是jar类型,
<packaging>pom</packaging> 父类型都为pom类型
<packaging>jar</packaging> 内部调用或者是作服务使用
<packaging>war</packaging> 需要部署的项目-->
<packaging>pom</packaging> <parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.3.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<org.mapstruct.version>1.2.0.Final</org.mapstruct.version>
<lombok.version>1.18.4</lombok.version>
<spring-cloud.version>Finchley.RELEASE</spring-cloud.version>
</properties> <!--dependencyManagement 标签不会引入依赖 -->
<dependencyManagement>
<dependencies>
<!--控制cloud的版本-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement> <dependencies>
<!-- 启用注解读取配置文件支持 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency> <!-- lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<scope>provided</scope>
</dependency> </dependencies> <build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
<annotationProcessorPaths>
<path>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
</path>
</annotationProcessorPaths>
</configuration>
</plugin> </plugins>
</build>
</project>

2.使用spring boot创建 微服务 的注册中心 eureka

  2.1创建 eureka

  maven 聚合工程 用spring boot 搭建 spring cloud 微服务 模块式开发项目

   maven 聚合工程 用spring boot 搭建 spring cloud 微服务 模块式开发项目

  maven 聚合工程 用spring boot 搭建 spring cloud 微服务 模块式开发项目

  maven 聚合工程 用spring boot 搭建 spring cloud 微服务 模块式开发项目

  

  maven 聚合工程 用spring boot 搭建 spring cloud 微服务 模块式开发项目

  2.2 因为是用 spring boot方式创建的模块 需要修改POM文件的一些配置

  maven 聚合工程 用spring boot 搭建 spring cloud 微服务 模块式开发项目

  maven 聚合工程 用spring boot 搭建 spring cloud 微服务 模块式开发项目

  maven 聚合工程 用spring boot 搭建 spring cloud 微服务 模块式开发项目

  maven 聚合工程 用spring boot 搭建 spring cloud 微服务 模块式开发项目

  2.3 在父模块的POM文件中引入子模块

  maven 聚合工程 用spring boot 搭建 spring cloud 微服务 模块式开发项目

  maven 聚合工程 用spring boot 搭建 spring cloud 微服务 模块式开发项目

  2.4 在application.yml中加入配置

  2.4.1 创建开发环境的 配置文件

      maven 聚合工程 用spring boot 搭建 spring cloud 微服务 模块式开发项目

    2.4.2 添加配置

      application.yml

spring:
application:
name: boot-eureka
profiles:
#指向开发环境的yml文件
active: dev

      application-dev.yml

server:
port: 8367
eureka:
instance:
hostname: localhost
server: #配置属性,但由于 Eureka 自我保护模式以及心跳周期长的原因,经常会遇到 Eureka Server 不剔除已关停的节点的问题
enable-self-preservation: false
eviction-interval-timer-in-ms: 5000
peer-eureka-nodes-update-interval-ms: 1000
wait-time-in-ms-when-sync-empty: 0
# 不向注册中心注册自己
client:
fetch-registry: false
register-with-eureka: false
eureka-server-total-connections: 200
serviceUrl:
defaultZone: http://127.0.0.1:${server.port}/eureka/

 2.4.3 修改启动类

 maven 聚合工程 用spring boot 搭建 spring cloud 微服务 模块式开发项目

  启动后访问 http://127.0.0.1:8367

  出现 下图  eureka配置 启动成功

  maven 聚合工程 用spring boot 搭建 spring cloud 微服务 模块式开发项目

  3.创建提供服务的项目

  创建流程和之前是一样 只是 对应的依赖不一样 依赖如下图

  maven 聚合工程 用spring boot 搭建 spring cloud 微服务 模块式开发项目

  创建完成后 修改POM文件 和eureka一样  配置文件一样 两个

  3.1 添加配置

  application.yml

spring:
application:
name: boot-service
profiles:
active: dev

  application-dev.yml

server:
port: 7432
eureka:
instance:
prefer-ip-address: true
lease-renewal-interval-in-seconds: 5
lease-expiration-duration-in-seconds: 20
client:
service-url:
defaultZone: http://127.0.0.1:8367/eureka/

  3.2 添加方法 这里我简单的写了个测试方法

  maven 聚合工程 用spring boot 搭建 spring cloud 微服务 模块式开发项目

  maven 聚合工程 用spring boot 搭建 spring cloud 微服务 模块式开发项目

  maven 聚合工程 用spring boot 搭建 spring cloud 微服务 模块式开发项目

  启动完成后 eureka会看到刚才启动的服务信息

  maven 聚合工程 用spring boot 搭建 spring cloud 微服务 模块式开发项目

  4.创建 内部暴露服务接口的service-client模块

  创建流程和上面一样 依赖如下图

  maven 聚合工程 用spring boot 搭建 spring cloud 微服务 模块式开发项目

  创建完成后 修改POM文件 没有  配置文件 这个模块 只是暴露服务 不需要启动

  maven 聚合工程 用spring boot 搭建 spring cloud 微服务 模块式开发项目

  maven 聚合工程 用spring boot 搭建 spring cloud 微服务 模块式开发项目

  如果 A服务接口需要调用B服务接口

  在 spring cloud 中 流程如下图

   maven 聚合工程 用spring boot 搭建 spring cloud 微服务 模块式开发项目

  则需要在 A服务的POM文件中 添加   service-client模块 如下图

  maven 聚合工程 用spring boot 搭建 spring cloud 微服务 模块式开发项目

  maven 聚合工程 用spring boot 搭建 spring cloud 微服务 模块式开发项目

  

  5.创建 外部暴露给前端的api层

  创建流程一样 依赖如下

  

  maven 聚合工程 用spring boot 搭建 spring cloud 微服务 模块式开发项目

   创建完成后 修改POM文件 和 boot-service一样  配置文件一样 两个

   POM文件中 把service-client模块 引入

  maven 聚合工程 用spring boot 搭建 spring cloud 微服务 模块式开发项目

  maven 聚合工程 用spring boot 搭建 spring cloud 微服务 模块式开发项目

  maven 聚合工程 用spring boot 搭建 spring cloud 微服务 模块式开发项目

  maven 聚合工程 用spring boot 搭建 spring cloud 微服务 模块式开发项目

  启动后eureka中会显示 如下图

  maven 聚合工程 用spring boot 搭建 spring cloud 微服务 模块式开发项目

  现在 用POSTMAN 测试 访问API层接口

  maven 聚合工程 用spring boot 搭建 spring cloud 微服务 模块式开发项目

  可以看到service 被调用

 maven 聚合工程 用spring boot 搭建 spring cloud 微服务 模块式开发项目

  GET请求 可以 这样玩  但是 如果是POST 请求 就需要把请求 对象VO 单独抽离成一个项目  给service ,service-client,api 层使用

 创建vo模块 流程一样 依赖都不需要

  maven 聚合工程 A模块 调用 B模块 需要A模块中引入B模块

  所以 service ,service-client,api 这3个模块都需要引入 vo模块

  maven 聚合工程 用spring boot 搭建 spring cloud 微服务 模块式开发项目

  vo模块中的vo对象

  maven 聚合工程 用spring boot 搭建 spring cloud 微服务 模块式开发项目

  boot-service模块中添加对应的传入对象测试方法

  maven 聚合工程 用spring boot 搭建 spring cloud 微服务 模块式开发项目

  maven 聚合工程 用spring boot 搭建 spring cloud 微服务 模块式开发项目

  service-client层

  maven 聚合工程 用spring boot 搭建 spring cloud 微服务 模块式开发项目

  api层

  maven 聚合工程 用spring boot 搭建 spring cloud 微服务 模块式开发项目

  POSTMAN 测试 前端 POST请求

  maven 聚合工程 用spring boot 搭建 spring cloud 微服务 模块式开发项目

  maven 聚合工程 用spring boot 搭建 spring cloud 微服务 模块式开发项目

  成功接受到对象

  谢谢阅读!!!