基于此,作者会把一整套完整的架构贡献出来并手把手教给大家,免费的哦~
环境:windows系统,jdk1.8,maven工具,idea开发工具
整套架构初步规划包含技能点:
springcloud、springboot、mybatis、分环境打包、mybatis-plus、动态数据源、druid、增删改查一键生成、gateway网关、feign的使用、熔断机制、如何防止雪崩、分布式+集群、注册中心:zookeeper+zkui和nacos、一个项目如何进行zk和nacos同时使用、动态配置:一个配置,所有集群节点共同热点使用
首先新建一个maven项目:
至此,整个项目最外层架子已经搭起来了,下面咱们继续填充模块:
至此项目即这样:
然后在增加一个parent,为何要增加parent呢,因为parent里无业务性源码,只有一个pom引用,表示所有的微服务都集成parent,使用parent下的pom引用的第三方jar,这样就不用每个微服务的pom给自己在引入同样的jar了。
好了,以同样的方式在搞一个B业务服务:
A服务与B服务在建立自己的真正的微服务:
同样的方式建B微服务:
删除无用的文件夹:
parent的src进行保留,后续有用。
大家是不是很奇怪,作者为何要建一个micro-business-A,里面还要在新建micro-business-A-api呢?大家千万别急,一定要跟着作者一起继续,绝对会让你恍然大悟的。
大家有没有想过:如果A服务使用了一个dateUtil工具类,如果B也想使用呢?难道给A建一个dateUtil类,同时给B服务也要建吗?NONONO~,在建一个common它不香吗~
大家一起来:
好了,至此,我们最简单,最规整的架子就搭起来了,是时候给pom填充内容了。
首先打开最micro-app的pom:
源码分享:
<properties>
<java.version>1.8</java.version>
<spring-boot.version>2.3.1.RELEASE</spring-boot.version>
<spring-boot-admin.version>2.2.1</spring-boot-admin.version>
<mybatis.version>3.3.1</mybatis.version>
<servlet.version>4.0.1</servlet.version>
<fastjson.version>1.2.68</fastjson.version>
<lombok.version>1.18.12</lombok.version>
<commons-lang3.version>3.10</commons-lang3.version>
<commons-collections.version>3.2.2</commons-collections.version>
<commons-collections4.version>4.4</commons-collections4.version>
<druid.version>1.1.22</druid.version>
<mapstruct.version>1.3.1.Final</mapstruct.version>
<jansi.version>1.18</jansi.version>
<mysql.version>5.1.41</mysql.version>
<jackson.version>2.10.3</jackson.version>
<velocity.version>1.7</velocity.version>
<swagger2.version>2.9.2</swagger2.version>
<swagger-annotations.version>1.6.1</swagger-annotations.version>
<reflections.version>0.9.12</reflections.version>
<mybatis-plus-boot-starter.version>3.3.1</mybatis-plus-boot-starter.version>
<spring-cloud-openfeign.version>2.2.2.RELEASE</spring-cloud-openfeign.version>
<dynamic-datasource.version>3.1.1</dynamic-datasource.version>
<spring-cloud.version>Hoxton.SR3</spring-cloud.version>
<jedis.version>3.2.0</jedis.version>
<netflix.version>2.2.2.RELEASE</netflix.version>
<maven-source-plugin.version>3.2.1</maven-source-plugin.version>
<maven-resources-plugin.version>3.1.0</maven-resources-plugin.version>
<maven-compiler-plugin.version>3.8.1</maven-compiler-plugin.version>
<maven-surefire-plugin.version>2.22.2</maven-surefire-plugin.version>
<maven-javadoc-plugin.version>3.1.1</maven-javadoc-plugin.version>
<common-base.version>1.0.0</common-base.version>
<common-base-document.version>1.0.1</common-base-document.version>
<spring-web.version>5.2.7.RELEASE</spring-web.version>
</properties>
<dependencies>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
<scope>compile</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>${maven-source-plugin.version}</version>
<configuration>
<attach>true</attach>
</configuration>
<executions>
<execution>
<phase>compile</phase>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
好了,micro-app的pom已经完成,大家跟着作者继续编写parent的pom:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<version>${spring-boot.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<version>${spring-boot.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>${mybatis.version}</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-generator</artifactId>
<version>${mybatis.version}</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.version}</version>
</dependency>
<!-- alibaba start -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>${druid.version}</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>${fastjson.version}</version>
</dependency>
<!-- alibaba end -->
<!-- apache commons start -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>${commons-lang3.version}</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-collections4</artifactId>
<version>${commons-collections4.version}</version>
</dependency>
<!-- apache commons end -->
<!-- 对象属性复制 https://mapstruct.org/ -->
<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct</artifactId>
<version>${mapstruct.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity</artifactId>
<version>${velocity.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
<version>${spring-boot.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis-reactive</artifactId>
<version>${spring-boot.version}</version>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>${jedis.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
<version>${netflix.version}</version>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>runtime</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<resources>
<!-- 资源文件配置 -->
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
<excludes>
<exclude>config/**</exclude>
</excludes>
</resource>
<!-- 过滤配置文件到config目录 -->
<resource>
<directory>src/main/resources/config</directory>
<filtering>true</filtering>
<targetPath>config</targetPath>
<includes>
<include>application.yml</include>
<include>application-${profileActive}.yml</include>
<include>banner.txt</include>
<include>*.xml</include>
<include>*.properties</include>
</includes>
</resource>
</resources>
<plugins>
<!-- 由于没有直接继承spring-boot-starter-parent,此处修添加主类和设置repackage -->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>${spring-boot.version}</version>
<configuration>
<layout>ZIP</layout>
<!--使用外部配置文件,jar包里没有资源文件-->
<addResources>true</addResources>
<outputDirectory>${project.build.directory}</outputDirectory>
</configuration>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>${maven-resources-plugin.version}</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven-compiler-plugin.version}</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
<annotationProcessorPaths>
<path>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-processor</artifactId>
<version>${mapstruct.version}</version>
</path>
<path>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>
<!-- 打包时跳过测试 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>${maven-surefire-plugin.version}</version>
<configuration>
<skipTests>true</skipTests>
<testFailureIgnore>true</testFailureIgnore>
</configuration>
</plugin>
<!-- 打包插件 -->
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.1.0</version>
<configuration>
<finalName>${project.build.finalName}-server</finalName>
<descriptors>
<descriptor>src/main/assembly/assembly.xml</descriptor>
</descriptors>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>${maven-javadoc-plugin.version}</version>
<configuration>
<encoding>UTF-8</encoding>
<skip>true</skip>
<additionalOptions>
<additionalOption>-Xdoclint:none</additionalOption>
</additionalOptions>
</configuration>
<executions>
<execution>
<id>attach-javadocs</id>
<phase>install</phase>
<goals>
<goal>jar</goal>
</goals>
<configuration>
<encoding>UTF-8</encoding>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
<!--MAVEN打包选择运行环境-->
<!-- 1:local(默认) 本地 2:dev:开发环境 3:test:测试环境 5.prod:生产环境 -->
<profiles>
<profile>
<id>local</id>
<properties>
<profileActive>local</profileActive>
</properties>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
<profile>
<id>dev</id>
<properties>
<profileActive>dev</profileActive>
</properties>
<activation>
<activeByDefault>false</activeByDefault>
</activation>
</profile>
<profile>
<id>test</id>
<properties>
<profileActive>test</profileActive>
</properties>
<activation>
<activeByDefault>false</activeByDefault>
</activation>
</profile>
<profile>
<id>prod</id>
<properties>
<profileActive>prod</profileActive>
</properties>
<activation>
<activeByDefault>false</activeByDefault>
</activation>
</profile>
</profiles>
其中有作者使用过的一些第三方工具和打包插件
maven-assembly-plugin(等下会讲如何使用)以及maven打包环境的选择,默认是
local(本地环境),其中有:
1:local(默认) 本地 2:dev:开发环境 3:test:测试环境 5.prod:生产环境
供选择,当然可自己追加。
继续追加common的pom:
<dependencies>
<dependency>
<groupId>org.fusesource.jansi</groupId>
<artifactId>jansi</artifactId>
<version>${jansi.version}</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>${servlet.version}</version>
</dependency>
<dependency>
<groupId>io.swagger</groupId>
<artifactId>swagger-annotations</artifactId>
<version>${swagger-annotations.version}</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>${fastjson.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>${commons-lang3.version}</version>
</dependency>
<!-- mybatis-plus begin -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>${mybatis-plus-boot-starter.version}</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-generator</artifactId>
<version>${mybatis-plus-boot-starter.version}</version>
</dependency>
<!-- mybatis-plus end -->
<dependency>
<groupId>commons-collections</groupId>
<artifactId>commons-collections</artifactId>
<version>${commons-collections.version}</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-collections4</artifactId>
<version>${commons-collections4.version}</version>
</dependency>
<dependency>
<groupId>org.reflections</groupId>
<artifactId>reflections</artifactId>
<version>${reflections.version}</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>dynamic-datasource-spring-boot-starter</artifactId>
<version>3.1.1</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.12</version>
<scope>compile</scope>
</dependency>
</dependencies>
同样都是作者常用的第三方工具包。
同时common增加工具类,删除resources(用不上),如下图:
上面的工具类我会把源码共享出来,大家不用着急。
刚才上面有提到打包插件maven-assembly-plugin,那具体怎么用呢?请看如下:
A微服务和B微服务和parent都增加assembly.xml,A和B的assembly.xml一样,并且和parent的assembly.xml不一样,大家注意下。
那到底为什么要用这个插件呢?
目前作者使用这个插件主要是打出来的包不是jar形式,而是tar.gz的包,包中包含了jar包以及下面咱们继续要增加的内容:
只给微服务下的src增加即可,里面包含了启动,关闭,重启项目,同时支持linux以及windows。那具体怎么用呢?后续我会慢慢道来,bin中的内容这里就不贴了,稍后会把源码共享出来,咱们先继续往下走:
上面说了,parent就是把需要共享的包提出来,那提出来给什么用呢?当然是每一个api微服务了,所以每个api服务都需要继承parent:
A和B的api一样。
同时也增加平常使用的jar包:(A和B都加)
<dependencies> <dependency> <groupId>com.laifeiyang.dev</groupId> <artifactId>micro-common</artifactId> <version>1.0.0-SNAPSHOT</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <version>${spring-boot.version}</version> </dependency> <dependency> <groupId>com.baomidou</groupId> <artifactId>dynamic-datasource-spring-boot-starter</artifactId> <version>${dynamic-datasource.version}</version> </dependency>
</dependencies>
好了,到此基本上一个项目的架构就搭起来了,是不是还缺少什么呢?微服务的启动方式是什么?
呢?是的,是main启动,所以在每个微服务里必须增加启动类
A和B一样。
在增加配置文件:B同A一样,配置这里就不贴了,源码中有。
至此最简单规范无业务的微服务就已经搭建起来了。
打包效果:B同A一样
启动效果:B同A一样
对了,如果大家要打包不同的环境,一定要注意打包的方式:
package -P test 测试环境
package -P prod 生产环境
package -P dev 开发环境
package -P local 或 package 或 install 本地环境
由于规范比较多,类也比较多,所以这里就不给大家展示所有的类了,如果大家有兴趣可进行源码的获取,并且会继续更新哦,目前作者会一直持续更新到整个架构序列技能点全部共享出来。
获取源码:https://github.com/feeyanna/FlyingCoding.git