微服务基础架构入门
技术要求:
java8+maven+git、github+Nginx+RabbitMQ+SpringBoot2.0
1.微服务架构概述
微服务架构是一种架构模式,他提倡将单一的应用程序划分为一组小的服务,服务之间互相协调,互相配合,为用户提供最终价值。每个服务运行在其独立的进程中,服务于服务间采用轻量级的通行机制互相协作(通常是基于HTTP协议的RESTful API)。每个服务都围绕着具体业务进行架构,并且能够被独立的部署到生产环境,类生产环境等。另外应当尽量避免同一的,集中式的服务管理机制,对具体的一个服务而言,应根据业务上下文,选择合适的语言、工具对象进行构建。
2.SpringCould简介
SpringCould=分布式微服务架构的一站式解决方案,是多种微服务架构落地技术的集合体,俗称微服务全家桶;
3.当前版本springCloud最合理配置
接下来进入正题
首先创建一个父项目,可以是maven项目也可以是springboot项目
父项目:springcloud2020
父项目所需要的导入的依赖
<!--统一管理jar包版本-->
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>12</maven.compiler.source>
<maven.compiler.target>12</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>5.1.6</mysql.version>
<druid.version>1.1.16</druid.version>
<mybatis.spring.boot.version>2.1.1</mybatis.spring.boot.version>
</properties>
<!--子模块继承之后,提供作用:锁定版本+子model不用写groupId和version-->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-project-info-reports-plugin</artifactId>
<version>3.0.0</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>
<!--spring cloud 阿里巴巴-->
<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>
<scope>runtime</scope>
</dependency>
<!-- druid-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>${druid.version}</version>
</dependency>
<!--mybatis-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>${mybatis.spring.boot.version}</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>
</dependencies>
</dependencyManagement>
将所有的版本号都放在dependencyManagement当中的好处是,可以避免在每个使用的子项目都声明一个版本号,这样当想升级或者切换到另一个版本时,只需要在顶层父容器里更新,而不需要一个一个子项目的修改;另外如果某个子项目需要另外的版本,只需要声明version即可;
dependencyManagement里只声明依赖,并不实现引入,因此子项目需要显示的声明需要用的依赖。dependencies进行引用;
如果不在子项目中声明依赖,是不会从父项目中继承下来;只有在子项目中写了该依赖,并且指定版本,才会从父项目中继续继承该项目,并且version和scope都读取自父pom
如果子项目中指定了版本号,那么会使用子项目中指定的jar版本;