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 的地址

 # spring
 spring:
   application:
     # 应用名称
     name: nk-system
   cloud:
     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 //开启注解
 @EnableDiscoveryClient
 public 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-system
   cloud:
     #服务熔断
     sentinel:
       transport:
         #port: 9999
         dashboard: 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
 ​
 # spring
 spring:
   application:
     name: nk-gateway
   cloud:
     nacos:
       discovery:
         # 服务注册地址
         server-addr: 127.0.0.1:8848
     sentinel:
       eager: true
       transport:
         # 控制台地址
         dashboard: 127.0.0.1:8080
     gateway:
       discovery:
         locator:
           enabled: true #让gateway通过nacos找到其他微服务

增加注解

 @SpringBootApplication
 @EnableDiscoveryClient
 public 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-asset
  profiles:
    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 {
     @Override
     public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
 ​
          ServerHttpRequest request = exchange.getRequest();
 ​
         // 重写StripPrefix
         addOriginalRequestUrl(exchange, request.getURI());

总结

  1. 容错思路:隔离、超时、限流、熔断、降级

  2. 隔离方式有:线程池隔离和信号量隔离

  3. 服务熔断状态:关闭状态、开启状态、半熔断状态

  4. sentinel流控模式:直接(默认)、关联、链路

  5. 事务特性:A:原子性(Atomicity)、C:一致性(Consistency)、I:隔离性(Isolation)、D:持久性(Durability)

  6. TCC事务

  7. Seata实现事务控制原理

  8. Seata事务模式:AT、TCC、SAGA 和 XA 事务模式

  9. Nacos注册服务工作过程原理

  10. 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/java
 lrwxrwxrwx. 1 root root 22 11月  4 2021 /bin/java -> /etc/alternatives/java
 [hesai@localhost bin]$ ls -lrt /etc/alternatives/java
 lrwxrwxrwx. 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_64
 export JAVA_HOME=/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.262.b10-1.el7.x86_64/          
 export JRE_HOME=$JAVA_HOME/jre
 export CLASSPATH=$JAVA_HOME/lib:$JRE_HOME/lib:$CLASSPATH
 export 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
 ​
 # spring
 spring:
   application:
     name: nk-gateway
   #重要,没有配置这个,无法加载到nk-gateway-dev.yml文件
   profiles:
     active: dev #环境标识  
   cloud:
     nacos:
       discovery:
         # 服务注册地址
         server-addr: 192.168.1.145:8848
       config:
         #nacos中心地址
         server-addr: 192.168.1.145:8848
         # 配置文件格式
         file-extension: yml
     sentinel:
       eager: true
       transport:
         # 控制台地址
         dashboard: 192.168.1.145:8080

服务配置

 #环境配置
 server:
   port: 9001
 # spring配置
 spring:
   application:
     # 应用名称
     name: nk-system
   profiles:
     active: dev #环境标识
   cloud:
     nacos:
       discovery:
         # 服务注册地址
         server-addr: 192.168.1.206:8848
       config:
         #nacos中心地址
         server-addr: 192.168.1.206:8848
         # 配置文件格式
         file-extension: yml
         shared-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: true
      max-file-size: 100MB
      max-request-size: 100MB    

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值