零基础微服务架构理论入门介绍
一个基于分布式的服务架构应该含有的架构内容如下:
1.1SpringCloud是什么
1.2基于微服务的系统
1.3服务与之对用的技术
1.4课程目录
第一章_SpringBoot和SpringCloud版本选择
详细可见SpringCloud2020.mmap文件
1.概述
2.SpringBoot版本选择
-
git源码地址:https://github.com/spring-projects/spring-boot/releases/
-
SpringBoot2.0新特性:https://github.com/spring-projects/spring-boot/wiki/Spring-Boot-2.0-Release-Notes
通过上面官网发现,Boot官方强烈建议你升级到2.X以上版本
3.SpringCloud版本选择
- git源码地址:https://github.com/spring-projects/spring-cloud
- 官网:https://spring.io/projects/spring-cloud
- Cloud版本命名规则:A-Z(目前2023年9月4日,官网更新至)
4.SpringBoot和SpringCloud之间的版本对应关系
4.1初略的版本对应关系
-
链接:https://spring.io/projects/spring-cloud#overview
-
结果:SpringCloud版本至少为G
4.2更详细的版本对应关系
-
链接:https://start.spring.io/actuator/info
-
视频查的结果:
今日查的结果:
5.SpringCloud学习中各种技术版本
- 根据SpringCloud版本选择SpringBoot版本
6.SpringBoot中2.X版本常用的pom
第二章_关于Cloud组件的升级
其中sentienl为阿里巴巴开发的服务熔断技术
1.参考资料
- SpringCloud的Hoxton SR1版本文档
https://cloud.spring.io/spring-cloud-static/Hoxton.SR1/reference/htmlsingle/
- SpringCloud的中文文档
https://www.bookstack.cn/read/spring-cloud-docs/docs-index.md
第三章_新建工程
1.父工程project空间新建
- maven版本以及工作环境
- 允许注解生效
2.父工程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><groupId>com.peng.springcloud</groupId><artifactId>cloud2020</artifactId><version>1.0-SNAPSHOT</version><!-- 使用maven分模块管理,有一个父级项目,父级项目中packaging(打包类型)为pom默认属性为jar,则maven会将项目打成jar包--><packaging>pom</packaging><!--统一管理jar包版本--><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><maven.compiler.source>1.8</maven.compiler.source><maven.compiler.target>1.8</maven.compiler.target><junit.version>4.12</junit.version><lombok.version>1.2.17</lombok.version><log4j.version>1.16.18</log4j.version><mysql.version>5.1.47</mysql.version><druid.version>1.1.16</druid.version><mybatis.spring.boot.version>1.3.0</mybatis.spring.boot.version></properties><!-- 子模块继承之后,提供作用:锁定版本,即子模块不用谢groupId和version--><dependencyManagement><dependencies><!--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>
</project>
3.DependencyManagement和Dependencies的区别
- 演示:
4.支付模块构建
前说:由订单模块启动支付模块
4.1套路
- 建module
- 改POM
- 写YML
- 主启动
- 业务类
4.2过程
(1)建module:cloud-provider-payment8001
直接new Module即可
(2)改POM
-
补充maven爆红解决方法
<?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"><parent><artifactId>cloud2020</artifactId><groupId>com.peng.springcloud</groupId><version>1.0-SNAPSHOT</version></parent><modelVersion>4.0.0</modelVersion><artifactId>cloud-provider-payment8001-2</artifactId><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-actuator</artifactId></dependency><dependency><groupId>org.mybatis.spring.boot</groupId><artifactId>mybatis-spring-boot-starter</artifactId></dependency><dependency><groupId>com.alibaba</groupId><artifactId>druid-spring-boot-starter</artifactId><version>1.1.10</version></dependency><!-- mysql-connector-java --><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId></dependency><!-- jdbc--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-jdbc</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-devtools</artifactId><scope>runtime</scope><optional>true</optional></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><optional>true</optional></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId></dependency></dependencies></project>
(3)写YML
<?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"><parent><artifactId>cloud2020</artifactId><groupId>com.peng.springcloud</groupId><version>1.0-SNAPSHOT</version></parent><modelVersion>4.0.0</modelVersion><artifactId>cloud-provider-payment8001-2</artifactId><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-actuator</artifactId></dependency><dependency><groupId>org.mybatis.spring.boot</groupId><artifactId>mybatis-spring-boot-starter</artifactId></dependency><dependency><groupId>com.alibaba</groupId><artifactId>druid-spring-boot-starter</artifactId><version>1.1.10</version></dependency><!-- mysql-connector-java --><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId></dependency><!-- jdbc--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-jdbc</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-devtools</artifactId><scope>runtime</scope><optional>true</optional></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><optional>true</optional></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId></dependency></dependencies></project>
(4)主启动
(5)业务类
①建表
CREATE TABLE `payment` ( `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT 'ID', `serial` varchar(200) DEFAULT '', PRIMARY KEY (`id`))
ENGINE=InnoDB
AUTO_INCREMENT=1
DEFAULT CHARSET=utf8
②entites实体类
- payment类
- 返回的json字符串
③dao层
- 接口PaymentDao
- mybaits的映射文件PaymentMapper.xml
④service层
⑤controller层
4.3.开启热部署
(1)引入jar包-子工程
<dependency> <groupId>org.springframework.boot</groupId><artifactId>spring-boot-devtools</artifactId><scope>runtime</scope><optional>true</optional>
</dependency>
(2)添加插件-父工程
<build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId><configuration><fork>true</fork><addResources>true</addResources></configuration></plugin></plugins>
</build>
(3)设置
(4)开启
快捷键:ctrl+shift+alt+/,选中第一个registry
(5)重启
5.消费模块构建
建module,改pom,改yml,主启动,业务类
5.1使用restTemplate实现服务间的调用
(1)创建restTemplate对象并注入到容器中
(2)Controller层
(3)注意要添加支付模块的创建订单的@RequestBody注解
@RequestBody主要用来接收前端传递给后端的json字符串中的数据的(请求体中的数据的);
因为订单模块返回给支付模块的数据,即payment数据为json格式数据
(4)补充:关闭占用80端口的进程方法
5.2工程重构(提取公共类)
- 新建工程、改pom、提取公共类、maven命令(clean、install)、订单80和支付8081分别改造