# sca **Repository Path**: mapc/sca ## Basic Information - **Project Name**: sca - **Description**: SpringCloudAlibaba微服务解决方案demo - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 1 - **Forks**: 0 - **Created**: 2021-04-04 - **Last Updated**: 2022-02-10 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # SpringCloudAlibaba微服务架构解决方案 SpringCloudAlibaba - 网关:Gateway - 分布式配置:Nacos - 服务注册/发现:Nacos - 服务调用:RestTemplate/OpenFeign/Feign/Dubbo - 服务限流熔断降级:Sentinel(推荐)、(Hystrix,测试OpenFeign对Hystrix的熔断支持) - 分布式事务:Seata - 分布式消息:Spring Cloud Stream-RocketMQ/RabbitMQ/Kakfa - 链路追踪:SkyWalking、Zipkin+Sleuth、CAT、Pinpoint - 容器技术:Docker/Kubernetes - 自动化部署:Jenkins 其它中间件: - 分布式搜索、数据分析引擎:Elasticsearch(ELK) - 分布式缓存:Redis - 分布式协调服务(一致性):Zookeeper - 分布式数据库(分库分表、读写分离等):ShardingSphere(Sharding-JDBC+Sharding-Proxy)、MyCat - 数据同步:Canal 其它重要点: - Java/Spring(ioc、aop、mvc等)/SpringBoot/SpringCloud/Mybatis等 - 数据库:MySQL - 并发 - JVM - 设计模式 - 数据结构与算法 - 性能优化 ## nacos #### 注册中心 #### 配置中心 - 需要在bootstrap命名的配置文件中配置nacos配置才行,不然报错 - nacos配置中心文件命名必须符合`${spring.application.name}. ${file-extension:properties}`或 `${spring.application.name}-${profile}. ${file-extension:properties}`格式,不然获取不到配置 ## OpenFeign 对Feign进行了增强,使Feign支持了Spring MVC注解,并整合了Ribbon和Hystrix,赋予了负载均衡、熔断降级等强大的功能支持. ## Gateway spring cloud gateway是基于webflux的,spring cloud gateway的pom.xml中已经引入了spring-boot-starter-webflux对web支持。 不要在网关模块添加spring-boot-start-web,不然报错。 在传统Spring Boot应用中,用@ControllerAdvice来处理全局异常。 但是在Spring Cloud项目有网关层的报错信息并不会交由@ControllerAdvice全局异常切面来处理。而是交由Spring默认错误处理器DefaultErrorWebExceptionHandler(源码可看ErrorWebFluxAutoConfiguration)。 原因是@ControllerAdvice处理的错误是DispatcherServlet转发找到具体控制器之后的报错而 网关层的报错是进入到具体控制器之前。 网关全局异常处理,重写 ErrorWebExceptionHandler来实现。 ```java /** * 网关异常默认由DefaultErrorWebExceptionHandler处理,只作用在webflux环境下,重写ErrorWebExceptionHandler实现定制响应 */ @Slf4j @Order(-1) @Component @RequiredArgsConstructor public class GlobalExceptionHandler implements ErrorWebExceptionHandler { private final ObjectMapper objectMapper; @Override public Mono handle(ServerWebExchange exchange, Throwable ex) { ServerHttpResponse response = exchange.getResponse(); if (response.isCommitted()) { return Mono.error(ex); } // header set response.getHeaders().setContentType(MediaType.APPLICATION_JSON); if (ex instanceof ResponseStatusException) { response.setStatusCode(((ResponseStatusException) ex).getStatus()); } return response.writeWith(Mono.fromSupplier(() -> { DataBufferFactory bufferFactory = response.bufferFactory(); try { return bufferFactory.wrap(objectMapper.writeValueAsBytes(R.failed(ex.getMessage()))); } catch (JsonProcessingException e) { log.error("Error writing response", ex); return bufferFactory.wrap(new byte[0]); } })); } } ``` ## Sentinel 限流、熔断/降级等规则持久化到Nacos,配置需要分开 ```yaml spring: cloud: sentinel: transport: dashboard: localhost:8080 port: 8719 datasource: ds1: nacos: server-addr: localhost:8015 namespace: bf100dae-330c-4fce-a783-d63aecac842b groupId: ${spring.application.name} dataId: ${spring.application.name}-sentinel-flow.json rule-type: flow #限流 data-type: json ds2: nacos: server-addr: localhost:8015 namespace: bf100dae-330c-4fce-a783-d63aecac842b groupId: ${spring.application.name} dataId: ${spring.application.name}-sentinel-degrade.json rule-type: degrade #降级 data-type: json ``` Sentinel支持网关限流,提供了Spring Cloud Gateway 的适配模块。 引入spring-cloud-alibaba-sentinel-gateway或sentinel-spring-cloud-gateway-adapter依赖皆可。 ## Spring Cloud Stream spring-cloud-starter-stream-rabbit/kafka 阿里巴巴提供了rocketmq适配。 开发只需要定义消息发送和接收通道绑定,配置通道目的地是同个队列,即可进行消息发送和消费。 ## Seata 两阶段事务提交模型。 TC、TM、RM。 抛出异常被全局异常捕获的时候,发现seata分布式事务不会回滚了。解决思路:通过AOP动态创建/关闭Seata分布式事务。 ## SkyWalking IDEA部署探针,在idea中的edit configurations中为每个服务的vm options中添加: ```text -javaagent:skywalking-agent.jar地址 -Dskywalking.agent.service_name=服务名 -Dskywalking.collector.backend_service=localhost:11800 ``` 其中: - -Dskywalking.agent.service_name:用于重写 agent/config/agent.config 配置文件中的服务名 - -Dskywalking.collector.backend_service:用于重写 agent/config/agent.config 配置文件中的服务地址 ## Docker Dockerfile镜像构建 Docker-compose容器编排,一键微服务部署 #### 常用指令 docker inspect 查询容器的配置和状态信息 --- ## 存在需要解决的问题 - SkyWalking无法收集Gateway网关数据。 - B服务调用A服务报错抛出全局异常,能够走Openfeign+Sentinel熔断/降级。但是B服务调用C服务报错抛出全局异常,却不能正常熔断/降级(相当纳闷)。 --- 参考资料: - [1、@RestControllerAdvice全局异常处理起作用的原理](https://blog.csdn.net/weixin_36142042/article/details/104850543) - [2、Spring Cloud Gateway 全局通用异常处理](https://www.tqwba.com/x_d/jishu/75070.html) - [3、分布式必学springcloud(十一)——消息驱动 SpringCloud Stream](https://blog.csdn.net/kisscatforever/article/details/80090962) - [4、SpringCloud Stream 整合RabbitMQ](https://blog.csdn.net/qq_21067307/article/details/103839356) - [5、对比了几种分布式事务方案,我选择了Seata](https://blog.csdn.net/qq_35067322/article/details/110914143) - [6、docker 运行 seata-server 配置nacos](https://blog.csdn.net/weixin_39352976/article/details/111542065) - [7、docker 运行 seata-server 配置nacos](http://t.zoukankan.com/binz-p-12841125.html) - [8、集成seata1.3.0遇到的一些问题](https://www.jianshu.com/p/e28a0aa5f88d) - [9、通过AOP动态创建/关闭Seata分布式事务](http://seata.io/zh-cn/blog/seata-spring-boot-aop-aspectj.html) - [10、seata1.0实现feign降级和全局异常处理的时候事务的回滚](https://linzhefeng23.blog.csdn.net/article/details/103726590) - [11、SpringCloud整合Skywalking实现链路追踪](https://my.oschina.net/u/4398725/blog/4535360) - [12、SpringCloud gateway + Nacos+ Skywalking 研究展示](https://blog.csdn.net/shenhonglei1234/article/details/100996518) - [13、基于Docker-Compose部署微服务(一)](https://www.codercto.com/a/79388.html)