记录一下近两年接触的微服务框架的知识,一是为了日后需要搭建框架时方便查阅,二是与大家分享下我所了解的知识,如有不对的或描述未清楚的地方欢迎大家在评论区进行留言
接下来我会教大家如何搭建一个微服务框架和继承一些项目中常用到的组件
先带大家看下我搭建的demo结构
这些向大家介绍一下各个模块的分工
- api模块主要是服务与服务之间调用的接口外放包,service_one_api和service_two_api分别对应service_one和service_two服务的接口外放包,后面会在继承feign组件时为大家介绍如何使用
- entity模块主要是服务与服务之间传输数据的对象存放的地方,主要api模块和service模块会用到这个模块
- common模块主要是存放一些服务之间的公用代码,比如core(工具箱)等等之类的模块
- service模块主要存放的就是具体的服务模块
- gateway 网关服务模块 主要是对请求过滤拦截和路由
以上我就先分为这几个模块创建demo,实际使用中可以根据自己项目需求来进行模块之间关系的搭建
接下来我将为大家展示下各个模块的pom文件
- gateway 网关服务模块的pom和使用方式会在后面的文章进行单独讲解
- 这个是cloud_demo主模块的pom主要用来管理整个服务框架的第三方包,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">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-build</artifactId>
<version>2.3.0.RELEASE</version>
<relativePath/>
</parent>
<groupId>org.wcy</groupId>
<artifactId>cloud_demo</artifactId>
<packaging>pom</packaging>
<version>1.0.0</version>
<modules>
<module>common</module>
<module>api</module>
<module>service</module>
</modules>
<name>cloud_demo</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<junit.version>4.11</junit.version>
<cloud-alibaba.version>2.1.4.RELEASE</cloud-alibaba.version>
<cloud.version>Greenwich.SR6</cloud.version>
<lombok.version>1.18.12</lombok.version>
<spring-boot.version>2.1.13.RELEASE</spring-boot.version>
<fastjson-version>1.2.73</fastjson-version>
<org.mapstruct.version>1.3.0.Final</org.mapstruct.version>
<mybatis-plus.version>3.3.1</mybatis-plus.version>
<spring-boot-starter-cache-version>2.3.2.RELEASE</spring-boot-starter-cache-version>
<mysql-connector-java.version>8.0.11</mysql-connector-java.version>
<druid-spring-boot-starter.version>1.1.10</druid-spring-boot-starter.version>
<spring-data-elasticsearch.version>4.0.4.RELEASE</spring-data-elasticsearch.version>
<hibernate-validator.version>6.1.6.Final</hibernate-validator.version>
<commons-pool2.version>2.8.0</commons-pool2.version>
</properties>
<dependencyManagement>
<dependencies>
<!-- cloud-alibaba全家桶-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>${cloud-alibaba.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!-- cloud全家桶-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!-- web启动器-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>${spring-boot.version}</version>
</dependency>
<!--开启springboot缓存支持-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
<version>${spring-boot.version}</version>
</dependency>
<!--添加redis缓存组件-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis-reactive</artifactId>
<version>${spring-boot.version}</version>
</dependency>
<!--Apache下一个开源的公共资源池-->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
<version>${commons-pool2.version}</version>
</dependency>
<!-- AlibabaJson工具类-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>${fastjson-version}</version>
</dependency>
<!-- mapstruct 映射bean的工具-->
<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-jdk8</artifactId>
<version>${org.mapstruct.version}</version>
</dependency>
<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-processor</artifactId>
<version>${org.mapstruct.version}</version>
</dependency>
<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct</artifactId>
<version>${org.mapstruct.version}</version>
</dependency>
<!-- mybatis Plus 插件-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus</artifactId>
<version>${mybatis-plus.version}</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>${mybatis-plus.version}</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-extension</artifactId>
<version>${mybatis-plus.version}</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-generator</artifactId>
<version>${mybatis-plus.version}</version>
</dependency>
<!-- rsa开始 -->
<dependency>
<groupId>org.bouncycastle</groupId>
<artifactId>bcprov-jdk16</artifactId>
<version>1.46</version>
</dependency>
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.10</version>
</dependency>
<!-- rsa结束 -->
<!--nacos注册中心-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
<version>2.1.0.RELEASE</version>
</dependency>
<!--alibaba服务监控组件-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
<version>2.1.0.RELEASE</version>
</dependency>
<!--mysql连接驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql-connector-java.version}</version>
</dependency>
<!--druid数据监控-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>${druid-spring-boot-starter.version}</version>
</dependency>
<!-- es依赖 -->
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-elasticsearch</artifactId>
<version>${spring-data-elasticsearch.version}</version>
</dependency>
<dependency>
<groupId>org.hibernate.validator</groupId>
<artifactId>hibernate-validator</artifactId>
<version>${hibernate-validator.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<!--Lombok是一个Java库,能自动插入编辑器并构建工具,简化Java开发。通过添加注解的方式,不需要为类编写getter或eques方法,同时可以自动化日志变量。-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
</dependency>
<!-- springboot单元测试框架 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<version>${spring-boot.version}</version>
</dependency>
</dependencies>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.1.0.RELEASE</version>
</plugin>
</plugins>
</pluginManagement>
<plugins>
<!-- 编译插件(设置源代码的JDK版本,目标代码JDK版本,编译字符集) -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<source>8</source>
<target>8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*Mapper.xml</include>
</includes>
<filtering>false</filtering>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<!--包含resources文件夹以及子文件夹下所有资源-->
<include>**/*.*</include>
</includes>
</resource>
</resources>
</build>
</project>
- 这个是api模块的pom,里面只有feign组件因为api模块内的子模块都是服务间通讯外放的接口,子模块内的pom基本不需要引入其他什么包所以我就暂时补贴出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">
<modelVersion>4.0.0</modelVersion>
<packaging>pom</packaging>
<modules>
<module>service_one_api</module>
<module>service_two_api</module>
</modules>
<parent>
<groupId>org.wcy</groupId>
<artifactId>cloud_demo</artifactId>
<version>1.0.0</version>
</parent>
<groupId>org.wcy</groupId>
<artifactId>api</artifactId>
<version>1.0.0</version>
<name>api</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<properties>
<entity.version>1.0.0</entity.version>
</properties>
<dependencies>
<!--服务间数据传输模块-->
<dependency>
<groupId>org.wcy</groupId>
<artifactId>entity</artifactId>
<version>${entity.version}</version>
</dependency>
<!--微服务通信框架feign-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
</dependencies>
</project>
- common模块下的都是些公用代码模块,具体模块内的工具类有使用什么包就引入什么包
- service模块是主要的微服务模块,里面可能会用到的一些包比如说mysql驱动的包,elasticsearch的包,nacos微服务注册中心的包等等,我们目前只引用我们使用的包,后面文章在介绍对应组件的使用的时候会引入使用的包
<?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>
<parent>
<groupId>org.wcy</groupId>
<artifactId>cloud_demo</artifactId>
<version>1.0.0</version>
</parent>
<groupId>org.wcy</groupId>
<artifactId>service</artifactId>
<packaging>pom</packaging>
<version>1.0.0</version>
<modules>
<module>service_one</module>
<module>service_two</module>
</modules>
<name>service</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<properties>
<entity.version>1.0.0</entity.version>
</properties>
<dependencies>
<!--服务间数据传输模块-->
<dependency>
<groupId>org.wcy</groupId>
<artifactId>entity</artifactId>
<version>${entity.version}</version>
</dependency>
<!-- web启动器-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
</project>
至此springCloud的框架搭建介绍完毕,后面我会为大家介绍各个组件的集成与使用,感谢大家观看!