SpringCloud学习随笔(一)
SpringCloud Alibaba
版本关系
版本依赖关系及说明可以参考如下链接:参考链接
版本选择
本次开发选择最新版本: 2021.0.4.0
查询版本对应关系:https://start.spring.io/actuator/info
{"git": {"branch": "b63fdb671d5422ead117d3f62cdbf1bc41bd41db","commit": {"id": "b63fdb6","time": "2022-12-13T20:20:17Z"}},"build": {"version": "0.0.1-SNAPSHOT","artifact": "start-site","versions": {"spring-boot": "3.0.0","initializr": "0.20.0-SNAPSHOT"},"name": "start.spring.io website","time": "2022-12-13T20:35:38.244Z","group": "io.spring.start"},"bom-ranges": {"codecentric-spring-boot-admin": {"2.4.3": "Spring Boot >=2.3.0.M1 and <2.5.0-M1","2.5.6": "Spring Boot >=2.5.0.M1 and <2.6.0-M1","2.6.8": "Spring Boot >=2.6.0.M1 and <2.7.0-M1","2.7.4": "Spring Boot >=2.7.0.M1 and <3.0.0-M1","3.0.0-M4": "Spring Boot >=3.0.0-M1 and <3.1.0-M1"},"solace-spring-boot": {"1.1.0": "Spring Boot >=2.3.0.M1 and <2.6.0-M1","1.2.2": "Spring Boot >=2.6.0.M1 and <3.0.0-M1"},"solace-spring-cloud": {"1.1.1": "Spring Boot >=2.3.0.M1 and <2.4.0-M1","2.1.0": "Spring Boot >=2.4.0.M1 and <2.6.0-M1","2.3.2": "Spring Boot >=2.6.0.M1 and <3.0.0-M1"},"spring-cloud": {"Hoxton.SR12": "Spring Boot >=2.2.0.RELEASE and <2.4.0.M1","2020.0.6": "Spring Boot >=2.4.0.M1 and <2.6.0-M1","2021.0.0-M1": "Spring Boot >=2.6.0-M1 and <2.6.0-M3","2021.0.0-M3": "Spring Boot >=2.6.0-M3 and <2.6.0-RC1","2021.0.0-RC1": "Spring Boot >=2.6.0-RC1 and <2.6.1","2021.0.5": "Spring Boot >=2.6.1 and <3.0.0-M1","2022.0.0-M1": "Spring Boot >=3.0.0-M1 and <3.0.0-M2","2022.0.0-M2": "Spring Boot >=3.0.0-M2 and <3.0.0-M3","2022.0.0-M3": "Spring Boot >=3.0.0-M3 and <3.0.0-M4","2022.0.0-M4": "Spring Boot >=3.0.0-M4 and <3.0.0-M5","2022.0.0-M5": "Spring Boot >=3.0.0-M5 and <3.0.0-RC1","2022.0.0-RC1": "Spring Boot >=3.0.0-RC1 and <3.0.0-RC2","2022.0.0-RC2": "Spring Boot >=3.0.0-RC2 and <3.1.0-M1"},"spring-cloud-azure": {"4.5.0": "Spring Boot >=2.5.0.M1 and <3.0.0-M1","6.0.0-beta.4": "Spring Boot >=3.0.0-M1 and <3.1.0-M1"},"spring-cloud-gcp": {"2.0.11": "Spring Boot >=2.4.0-M1 and <2.6.0-M1","3.4.0": "Spring Boot >=2.6.0-M1 and <3.0.0-M1"},"spring-cloud-services": {"2.3.0.RELEASE": "Spring Boot >=2.3.0.RELEASE and <2.4.0-M1","2.4.1": "Spring Boot >=2.4.0-M1 and <2.5.0-M1","3.3.0": "Spring Boot >=2.5.0-M1 and <2.6.0-M1","3.4.0": "Spring Boot >=2.6.0-M1 and <2.7.0-M1","3.5.0": "Spring Boot >=2.7.0-M1 and <3.0.0-M1"},"spring-shell": {"2.1.4": "Spring Boot >=2.7.0 and <3.0.0-M1","3.0.0-M3": "Spring Boot >=3.0.0-M1 and <3.1.0-M1"},"vaadin": {"14.9.2": "Spring Boot >=2.1.0.RELEASE and <2.6.0-M1","23.2.11": "Spring Boot >=2.6.0-M1 and <2.7.0-M1","23.3.0": "Spring Boot >=2.7.0-M1 and <2.8.0-M1"},"wavefront": {"2.0.2": "Spring Boot >=2.1.0.RELEASE and <2.4.0-M1","2.1.1": "Spring Boot >=2.4.0-M1 and <2.5.0-M1","2.2.2": "Spring Boot >=2.5.0-M1 and <2.7.0-M1","2.3.1": "Spring Boot >=2.7.0-M1 and <3.0.0-M1"}},"dependency-ranges": {"native": {"0.9.0": "Spring Boot >=2.4.3 and <2.4.4","0.9.1": "Spring Boot >=2.4.4 and <2.4.5","0.9.2": "Spring Boot >=2.4.5 and <2.5.0-M1","0.10.0": "Spring Boot >=2.5.0-M1 and <2.5.2","0.10.1": "Spring Boot >=2.5.2 and <2.5.3","0.10.2": "Spring Boot >=2.5.3 and <2.5.4","0.10.3": "Spring Boot >=2.5.4 and <2.5.5","0.10.4": "Spring Boot >=2.5.5 and <2.5.6","0.10.5": "Spring Boot >=2.5.6 and <2.5.9","0.10.6": "Spring Boot >=2.5.9 and <2.6.0-M1","0.11.0-M1": "Spring Boot >=2.6.0-M1 and <2.6.0-RC1","0.11.0-M2": "Spring Boot >=2.6.0-RC1 and <2.6.0","0.11.0-RC1": "Spring Boot >=2.6.0 and <2.6.1","0.11.0": "Spring Boot >=2.6.1 and <2.6.2","0.11.1": "Spring Boot >=2.6.2 and <2.6.3","0.11.2": "Spring Boot >=2.6.3 and <2.6.4","0.11.3": "Spring Boot >=2.6.4 and <2.6.6","0.11.5": "Spring Boot >=2.6.6 and <2.7.0-M1","0.12.0": "Spring Boot >=2.7.0-M1 and <2.7.1","0.12.1": "Spring Boot >=2.7.1 and <3.0.0-M1"},"okta": {"1.4.0": "Spring Boot >=2.2.0.RELEASE and <2.4.0-M1","1.5.1": "Spring Boot >=2.4.0-M1 and <2.4.1","2.0.1": "Spring Boot >=2.4.1 and <2.5.0-M1","2.1.6": "Spring Boot >=2.5.0-M1 and <3.0.0-M1"},"mybatis": {"2.1.4": "Spring Boot >=2.1.0.RELEASE and <2.5.0-M1","2.2.2": "Spring Boot >=2.5.0-M1 and <2.7.0-M1","2.3.0": "Spring Boot >=2.7.0-M1 and <3.0.0-M1","3.0.0": "Spring Boot >=3.0.0-M1"},"camel": {"3.5.0": "Spring Boot >=2.3.0.M1 and <2.4.0-M1","3.10.0": "Spring Boot >=2.4.0.M1 and <2.5.0-M1","3.13.0": "Spring Boot >=2.5.0.M1 and <2.6.0-M1","3.17.0": "Spring Boot >=2.6.0.M1 and <2.7.0-M1","3.19.0": "Spring Boot >=2.7.0.M1 and <3.0.0-M1"},"picocli": {"4.7.0": "Spring Boot >=2.5.0.RELEASE and <3.1.0-M1"},"open-service-broker": {"3.2.0": "Spring Boot >=2.3.0.M1 and <2.4.0-M1","3.3.1": "Spring Boot >=2.4.0-M1 and <2.5.0-M1","3.4.1": "Spring Boot >=2.5.0-M1 and <2.6.0-M1","3.5.0": "Spring Boot >=2.6.0-M1 and <2.7.0-M1"}}}
技术栈
微服务架构技术框架
微服务目录 | 相关技术 |
---|---|
服务开发 | Springboot2.7.6 |
服务配置与管理 | Nacos2.0.4 |
服务注册与发现 | Nacos2.0.4 |
服务调用 | OpenFeign |
服务熔断 | Sentinel1.8.5 |
负载均衡 | Spring Cloud LoadBalance、Robbin |
消息队列 | RocketMQ4.9.4 |
服务路由(API网关) | Spring Cloud Gateway |
服务监控 | Sentinel1.8.5 |
全链路追踪 |
skywalking
|
服务部署 | Docker、kubernetes |
数据流操作开发包 | Spring Cloud Stream |
分布式事务 | Seata1.5.2 |
数据缓存 | redis7、ehcache |
持久层框架 | Mybatis Plus 3.5.0 |
调度器 | xxl-job |
Nacos-服务冶理
官网地址
home
搭建环境
第1步: 安装nacos
下载地址: https://github.com/alibaba/nacos/releases下载zip格式的安装包,然后进行解压缩操作
第2步: 启动nacos
#linux系统sh startup.sh -m standalone#windows系统startup.cmd -m standalone
第3步: 访问nacos
打开浏览器输入http://localhost:8848/nacos,即可访问服务, 默认密码是nacos/nacos
服务注册
添加依赖
<dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId></dependency>
配置服务提供者
i. 在 application.properties
中配置 Nacos server 的地址
# springspring:application:# 应用名称name: nk-systemcloud:nacos:discovery:# 服务注册地址server-addr: 127.0.0.1:8848
ii. 通过 Spring Cloud 原生注解 @EnableDiscoveryClient
开启服务注册发现功能:
@SpringBootApplication@EnableFeignClients@EnableDiscoveryClient //开启注册发现public class NkAssetApplication {public static void main(String[] args) {SpringApplication.run(NkAssetApplication.class, args);}}
openFeign
添加依赖
<dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-openfeign</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-loadbalancer</artifactId></dependency>
开启注解
@SpringBootApplication@EnableFeignClients //开启注解@EnableDiscoveryClientpublic class NkAssetApplication {public static void main(String[] args) {SpringApplication.run(NkAssetApplication.class, args);}}
服务调用
/*** 调用人员服务* @author hesai*/@FeignClient(value="nk-system",fallback = SysUserService.class)public interface ISysUserService {/*** 查询人员信息* @return*/@GetMapping("/sys/user")public String findAll();}
Sentinel-服务容错
集成
<dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alibaba-sentinel</artifactId></dependency>
安装
官方地址
1、下载安装包
Release v1.8.5 · alibaba/Sentinel · GitHub
1.8.5下载地址:https://github.com/alibaba/Sentinel/releases/download/1.8.5/sentinel-dashboard-1.8.5.jar
2、启动服务
java -Dserver.port=8080 -Dcsp.sentinel.dashboard.server=localhost:8080 -Dproject.name=sentinel-dashboard -jar sentinel-dashboard-1.8.5.jar
增加配置
spring:application:# 应用名称name: nk-systemcloud:#服务熔断sentinel:transport:#port: 9999dashboard: 127.0.0.1:8080
GateWay-服务网关
添加依赖
<dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-gateway</artifactId><exclusions><exclusion><!--需要排除springboot web依赖,不然启动报错--><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></exclusion></exclusions></dependency><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId></dependency><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId></dependency><!-- 一定要引入,不然无法使用服务名调用 --><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-loadbalancer</artifactId></dependency><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alibaba-sentinel</artifactId></dependency><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-alibaba-sentinel-gateway</artifactId></dependency>
添加配置
server:port: 9000# springspring:application:name: nk-gatewaycloud:nacos:discovery:# 服务注册地址server-addr: 127.0.0.1:8848sentinel:eager: truetransport:# 控制台地址dashboard: 127.0.0.1:8080gateway:discovery:locator:enabled: true #让gateway通过nacos找到其他微服务
增加注解
@SpringBootApplication@EnableDiscoveryClientpublic class NkGatewayApplication {public static void main(String[] args) {SpringApplication.run(NkGatewayApplication.class, args);}}
Nacos-服务配置
添加依赖
<!--nacos 服务配置 --><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId></dependency>
修改配置
不能使用原来的application.yml
作为配置文件,而是新建一个bootstrap.yml
作为配置文件
配置文件加载的优先级(由高到低) bootstrap.properties ->bootstrap.yml -> application.properties -> application.yml
spring:application:# 应用名称name: nk-assetprofiles:active: dev #环境标识cloud:nacos:config: #服务配置server-addr: 127.0.0.1:8848 #nacos中心地址file-extension: yaml # 配置文件格式
网关聚合Swagger
引入依赖
<dependency><groupId>io.springfox</groupId><artifactId>springfox-boot-starter</artifactId> </dependency>
增加SwaggerProvider
@Primary @Component public class SwaggerProvider implements SwaggerResourcesProvider, WebFluxConfigurer {/*** Swagger2默认的url后缀*/public static final String SWAGGER2URL = "/v2/api-docs";
增加SwaggerHandler
@RestController@RequestMapping("/swagger-resources")public class SwaggerHandler{@Autowired(required = false)private SecurityConfiguration securityConfiguration;@Autowired(required = false)private UiConfiguration uiConfiguration;
设置StripPrefix = 1
/*** 重写StripPrefix = 1,支持全局* @author hesai* @date 2023年02月08日 14:42*/public class RequestGlobalFilter implements GlobalFilter, Ordered {@Overridepublic Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {ServerHttpRequest request = exchange.getRequest();// 重写StripPrefixaddOriginalRequestUrl(exchange, request.getURI());
总结
容错思路:隔离、超时、限流、熔断、降级
隔离方式有:线程池隔离和信号量隔离
服务熔断状态:关闭状态、开启状态、半熔断状态
sentinel流控模式:直接(默认)、关联、链路
事务特性:A:原子性(Atomicity)、C:一致性(Consistency)、I:隔离性(Isolation)、D:持久性(Durability)
TCC事务
Seata实现事务控制原理
Seata事务模式:AT、TCC、SAGA 和 XA 事务模式
Nacos注册服务工作过程原理
ribbin负载策略
问题
nacos
nacos无法启动
ERROR: Please set the JAVA_HOME variable in your environment, We need java(x64)! jdk8 or later is better! !!
原因
:nacos/bin/startup.sh 中配置的JAVA_HOME路径不是系统JDK的路径
找到JDK安装路径
[hesai@localhost bin]$ which java
[hesai@localhost bin]$ which java/bin/java[hesai@localhost bin]$ ls -lrt /bin/javalrwxrwxrwx. 1 root root 22 11月 4 2021 /bin/java -> /etc/alternatives/java[hesai@localhost bin]$ ls -lrt /etc/alternatives/javalrwxrwxrwx. 1 root root 71 11月 4 2021 /etc/alternatives/java -> /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.262.b10-1.el7.x86_64/jre/bin/java
修改/etc/profile文件
/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.262.b10-1.el7.x86_64export JAVA_HOME=/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.262.b10-1.el7.x86_64/ export JRE_HOME=$JAVA_HOME/jreexport CLASSPATH=$JAVA_HOME/lib:$JRE_HOME/lib:$CLASSPATHexport PATH=$JAVA_HOME/bin:$JRE_HOME/bin:$PATH
服务无法下线
错误:The Raft Group [naming_instance_metadata] did not find the Leader node
原因
:注册的Ip啥的混乱了
解决方法:停掉nacos,再删掉 data目录下的protocol 文件夹,再重启nacos.最后再重启需要注册的那些服务.
openFeign调用一直走熔断
原因:
1、熔断配置问题
2、参考链接
//启动类注解的扫描不到类,增加指定包路径@EnableFeignClients(basePackages = {"com.matech.nk.api"})
spring扫描时把openFeign实现类当成普通servcie自动注入,导致代理调用错了类,一直进入熔断类
swagger问题
无法加载接口出来
原因:配置文件中缺少配置
spring:mvc:pathmatch:matching-strategy: ant-path-matcher
原因:
@ApiImplicitParam 配置的参数值与实际方法中参数不一致
#检查控制层配置
No API definition provided
原因:
可能网关中配置有nacos地址与微服务中配置的IP不一致,nacos中找不到其他服务,如下错误配置
网关中没有配置环境标识,加载不到配置中心的参数
网关配置
:
server:port: 9000# springspring:application:name: nk-gateway#重要,没有配置这个,无法加载到nk-gateway-dev.yml文件profiles:active: dev #环境标识 cloud:nacos:discovery:# 服务注册地址server-addr: 192.168.1.145:8848config:#nacos中心地址server-addr: 192.168.1.145:8848# 配置文件格式file-extension: ymlsentinel:eager: truetransport:# 控制台地址dashboard: 192.168.1.145:8080
服务配置
#环境配置server:port: 9001# spring配置spring:application:# 应用名称name: nk-systemprofiles:active: dev #环境标识cloud:nacos:discovery:# 服务注册地址server-addr: 192.168.1.206:8848config:#nacos中心地址server-addr: 192.168.1.206:8848# 配置文件格式file-extension: ymlshared-configs:- sys-application-${spring.profiles.active}.${spring.cloud.nacos.config.file-extension}
redis问题
对像无法序化
com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "workingOrganization"
原因
:缓存中属性与实体属性不一致
文件上传
文件上传大小限制
The field file exceeds its maximum permitted size of 1048576 bytes."
增加配置:
spring: servlet:multipart:enabled: truemax-file-size: 100MBmax-request-size: 100MB
SpringCloud学习随笔(一)相关推荐
- Ibatis学习随笔
Ibatis学习随笔 < person > < id > 1 </ id > < firstName > Clinton </ ...
- Delphi面向对象学习随笔六:接口
Delphi面向对象学习随笔六:接口 Delphi面向对象学习随笔六:接口 作者:巴哈姆特 (转载请注明出处并保持完整) 在对象化中,类的继承是一个非常强大的机制:而更加强大的继承机制应该是来自从 ...
- SpringCloud学习笔记(1)- Spring Cloud Alibaba
文章目录 SpringCloud学习笔记(1)- Spring Cloud Alibaba 服务治理 Nacos 服务注册 Nacos 服务发现与调用 Ribbon 负载均衡 Sentinel 服务限 ...
- SpringCloud学习笔记(1)- Spring Cloud Netflix
文章目录 SpringCloud学习笔记(1)- Spring Cloud Netflix 单体应用存在的问题 Spring Cloud Eureka Eureka Server代码实现 Eureka ...
- SpringCloud学习系列之七 ----- Zuul路由网关的过滤器和异常处理
前言 在上篇中介绍了SpringCloud Zuul路由网关的基本使用版本,本篇则介绍基于SpringCloud(基于SpringBoot2.x,.SpringCloud Finchley版)中的路由 ...
- Spring-Cloud 学习笔记-(4)负载均衡器Ribbon
目录 Spring-Cloud 学习笔记-(4)负载均衡器Ribbon 1.前言 2.什么是负载均衡 2.1.问题分析 2.2.什么是Ribbon 3.快速入门 3.1.实现方式一 3.1.1.修改代 ...
- SpringCloud学习笔记(6)----Spring Cloud Netflix之负载均衡-Ribbon的使用
1. 什么是负载均衡? 负载均衡,就是分发请求流量到不同的服务器. 负载均衡一般分为两种 1. 服务器端负载均衡(nginx) 2. 客户端负载均衡(Ribbon) 2. 服务提供者(spring-c ...
- python rowcount_PyQt(Python+Qt)学习随笔:QTableWidget的currentItem、rowCount、columnCount等部件状态属性访问方法...
老猿将QTableWidget表格部件中反映部件当前情况的一些方法归类为部件状态访问方法,包括部件的行数.列数.当前项.当前行.当前列等属性访问方法. 1.行数rowCount QTableWidge ...
- C#程序集Assembly学习随笔(第一版)_AX
①什么是程序集? 可以把程序集简单理解为你的.NET项目在编译后生成的*.exe或*.dll文件. 嗯,这个确实简单了些,但我是这么理解的.详细: http://blog.csdn.net/sws83 ...
最新文章
- 微软开源: 老旧照片修复的AI算法
- cuda runtime error (59) : device-side assert triggered when running transfer_learning_
- 零基础自学python的app-零基础转行Python,为什么有些人自学几天就放弃了?
- MVC4实现批量更新数据
- 数字化时代,阿里、京东们能否解决困扰市场已久的“倒奶问题”?
- textarea里的回车和换行符与json里的回车和换行符
- Android Studio 3.1.4 报错Failed to find style 'coordinatorLayoutStyle' in current theme
- java定义整形输出_java程序命令行接受字符转换为整形并相加输出
- java中prepend的用法_jQuery中prepend()方法用法实例
- struts2+hibernate3.3+spring3.0 实现零配置
- spring cloud 学习(5) - config server
- 「leetcode」1207. 独一无二的出现次数:【数组在哈希法中的经典应用】详解
- SQL循环算出移动加权平均
- 数仓(五):数据建模--ER模型/维度建模,概念模型/逻辑模型/ 物理模型
- IOI 2022 简要题解
- 面试珠玑 嵌入式C程序员经典笔试题一
- mysql知识系列:报错right syntax to use near IDENTIFIED BY
- 图片扫描文字识别工具:text scanner mac中文版
- 《大话处理器》简要学习笔记
- HTML+CSS网页设计期末课程大作——体育足球(5页面)
热门文章
- J2ee管理项目-毕业论文
- Linux-4412用汇编程序控制寄存器R0、R1、R2实现KED亮灭
- 解决windows命令行\符号不正确显示成¥的问题
- 美国文理学院的计算机科学,康奈尔大学文理学院的cs专业怎么样?
- MyBatis-Plus中Page类各个参数的具体含义
- 一起学习LLVM(四):学习llvm-cbe
- FSSC22000认证辅导,GFSI 旨在维持食品安全管理方案的基准审核流程
- 2022浙江最新高级消防设施操作员模拟试题题库及答案
- 常见的HTTP响应状态码类型
- 她是计算机系公认的系花 却只喜欢打游戏,我选择了计算机专业,自以为自己不是系花就是班花,结果扎心了...