- SpringCloud
- 什么是微服务
- springcloud体系
- springcloud简介
- Boot和Cloud版本选型
- 版本
- 选型
- Cloud组件停更和升级
- 1. 停更引发的“升级惨案”
- 2. Cloud升级
- 项目:订单-支付微服务
- 父工程pom文件
- 支付模块构建
- 热部署Devtools
- 消费者订单模块
- RestTemplate
- 开启Run DashBoard
- 工程重构
可以阅读以前做的笔记:https://qkongtao.cn/?p=549
springcloud体系- 服务注册与发现
- 服务调用
- 服务熔断
- 负载均衡
- 服务降级
- 服务消息队列
- 配置中心管理
- 服务网关
- 服务监控
- 全链路追踪署
- 自动化构建部
- 服务定时任务调度操作
SpringCloud:分布式微服务架构的一站式解决方案,是多种微服务架构落地技术的集合体,俗称微服务全家桶
Spring Cloud官方文档:https://cloud.spring.io/spring-cloud-static/Hoxton.SR1/reference/htmlsingle/
Spring Cloud中文文档:https://www.bookstack.cn/read/spring-cloud-docs/docs-index.md
Spring Boot官方文档:https://docs.spring.io/spring-boot/docs/2.2.2.RELEASE/reference/htmlsingle/
案例:
-
京东促销架构示例
-
阿里服务架构示例
-
微服务基础服务示例
-
springcloud基础技术栈示例
Spring Boot 2.X 版
源码地址:https://github.com/spring-projects/spring-boot/releases/
Spring Boot 2 的新特性:https://github.com/spring-projects/spring-boot/wiki/spring-Boot-2.0-Release-Notes
通过上面官网发现,Boot官方强烈建议你升级到2.X以上版本
Spring Cloud H版
源码地址:https://github.com/spring-projects/spring-cloud
官网:https://spring.io/projects/spring-cloud
SpringCloud和Springboot之间的依赖关系如何看:https://spring.io/projects/spring-cloud#overview
更详细的版本对应查看方法(json):https://start.spring.io/actuator/info
结果
接下来开发用到的组件版本
- Cloud - Hoxton.SR1
- Boot - 2.2.2.RELEASE
- Cloud Alibaba - 2.1.0.RELEASE
- Java - Java 8
- Maven - 3.5及以上
- MySQL - 5.7及以上
- 停更不停用
- 被动修复bugs
- 不再接受合并请求
- 不再发布新版本
- 服务注册中心
× Eureka
√ Zookeeper
√ Consul
√ Nacos - 服务调用
√ Ribbon
√ LoadBalancer - 服务调用2
× Feign
√ OpenFeign - 服务降级
× Hystrix
√ resilience4j
√ sentienl - 服务网关
× Zuul
! Zuul2
√ gateway - 服务配置
× Config
√ Nacos - 服务总线
× Bus
√ Nacos
4.0.0 cn.kt springcloud pom 1.0-SNAPSHOT UTF-8 1.8 1.8 4.12 1.2.17 1.16.18 5.1.47 1.1.9 2.1.4 org.springframework.boot spring-boot-dependencies 2.2.2.RELEASE pom import org.springframework.cloud spring-cloud-dependencies Hoxton.SR1 pom import com.alibaba.cloud spring-cloud-alibaba-dependencies 2.2.0.RELEASE pom import mysql mysql-connector-java ${mysql.version} com.alibaba druid-spring-boot-starter ${druid.verison} org.mybatis.spring.boot mybatis-spring-boot-starter ${mybatis.spring.boot.verison} org.projectlombok lombok ${lombok.version} junit junit ${junit.version} log4j log4j ${log4j.version} cloud2020 org.springframework.boot spring-boot-maven-plugin 2.2.2.RELEASE true true
复习DependencyManagement和Dependencies
Maven使用dependencyManagement元素来提供了一种管理依赖版本号的方式。
通常会在一个组织或者项目的最顶层的父POM中看到dependencyManagement元素。
使用pom.xml中的dependencyManagement元素能让所有在子项目中引用个依赖而不用显式的列出版本量。
Maven会沿着父子层次向上走,直到找到一个拥有dependencyManagement元素的项目,然后它就会使用这个
dependencyManagement元素中指定的版本号。
例如:
mysq1 mysql-connector-java 5.1.2
然后在子项目里就可以添加mysql-connector时可以不指定版本号
mysq1 mysql-connector-java
这样做的好处就是:如果有多个子项目都引用同一样依赖,则可以避免在每个使用的子项目里都声明一个版本号,这样当想升级或切换到另一个版本时,只需要在顶层父容器里更新,而不需要一个一个子项目的修改;另外如果某个子项目需要另外的一个版本,只需要声明version就可。
- dependencyManagement里只是声明依赖,并不实现引入,因此子项目需要显示的声明需要用的依赖。
- 如果不在子项目中声明依赖,是不会从父项目中继承下来的;只有在子项目中写了该依赖项,并且没有指定具体版本,才会从父项目中继承该项,并且version和scope都读取自父pom。
- 如果子项目中指定了版本号,那么会使用子项目中指定的jar版本。
IDEA右侧旁的Maven插件有Toggle ’ Skip Tests’ Mode按钮,这样maven可以跳过单元测试
父工程创建完成执行mvn : install将父工程发布到仓库方便子工程继承。
支付模块构建创建微服务模块套路:建Module、改POM、写YML、主启动、业务类
- 新建子模块子模块
名字:cloud_pay_8001 - pom依赖
springcloud cn.kt 1.0-SNAPSHOT 4.0.0 cloud-provider-payment8001 org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-actuator org.mybatis.spring.boot mybatis-spring-boot-starter com.alibaba druid-spring-boot-starter mysql mysql-connector-java org.springframework.boot spring-boot-starter-jdbc org.springframework.boot spring-boot-devtools runtime true org.projectlombok lombok true org.springframework.boot spring-boot-starter-test org.springframework.cloud spring-cloud-starter-netflix-eureka-client org.springframework.cloud spring-cloud-starter-zipkin
- 创建application.yml
server:
port: 8001
spring:
application:
name: cloud-payment-service
datasource:
# 当前数据源操作类型
type: com.alibaba.druid.pool.DruidDataSource
# mysql驱动类
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/db2019?useUnicode=true&characterEncoding=
UTF-8&useSSL=false&serverTimezone=GMT%2B8
username: root
password: root
mybatis:
mapper-locations: classpath*:mapper/*.xml
type-aliases-package: com.eiletxie.springcloud.entities
- 主启动类
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class PaymentMain001 {
public static void main(String[] args) {
SpringApplication.run(PaymentMain001.class, args);
}
}
- 业务类
开发时使用,生产环境关闭
- 添加坐标
org.springframework.boot spring-boot-devtools runtime true
- 添加plugin到pom.xml
下段配置复制到聚合父类总工程的pom.xml
org.springframework.boot spring-boot-maven-plugin true true
- 启动自动编译
File -> Settings(New Project Settings->Settings for New Projects) ->Complier
下面项勾选
Automatically show first error in editor
Display notification on build completion
Build project automatically
Compile independent modules in parallel
- 修改允许热部署插件
键入Ctrl + Shift + Alt + / ,打开Registry,勾选:
compiler.automake.allow.when.app.running
actionSystem.assertFocusAccessFromEdt
- 重启IDEA
- 建Module
创建名为cloud-consumer-order80的maven工程。
-
改POM
-
写YML
-
主启动
-
业务类
RestTemplate提供了多种便捷访问远程Http服务的方法,是一种简单便捷的访问restful服务模板类,是Spring提供的用于访问Rest服务的客户端模板工具集
官网地址:https://docs.spring.io/spring-framework/docs/5.2.2.RELEASE/javadoc-api/org/springframework/web/client/RestTemplate.html
使用:
- 使用restTemplate访问restful接口非常的简单粗暴无脑。
- (url, requestMap, ResponseBean.class)这三个参数分别代表。
- REST请求地址、请求参数、HTTP响应转换被转换成的对象类型。
如果多个微服务项目时,Run DashBoard没有自动启动,
可以通过修改idea的workspace.xml的方式来快速打开Run Dashboard窗口
- 打开工程路径下的.idea文件夹的workspace.xml
- 在
中修改或添加以下代码:
由于idea版本差异,可能需要关闭重启。
启动后效果:
观察cloud-consumer-order80与cloud-provider-payment8001两工程有重复代码(entities包下的实体),建一个公共模块重构。
-
新建模块cloud-api-commons
-
POM.xml
springcloud cn.kt 1.0-SNAPSHOT 4.0.0 cn.kt cloud-api-commons org.springframework.boot spring-boot-devtools runtime true org.projectlombok lombok true cn.hutool hutool-all 5.1.0
-
添加公共部分代码到公共模块,并删除其他模块中的该代码
-
maven clean、install cloud-api-commons工程,以供给其他工程调用。
-
在其他使用到的公共代码的工程中的pom中添加自己的依赖
cn.kt cloud-api-commons ${project.version}
- 进行测试



