当前位置: 首页 > news >正文

服务限流、降级、熔断-SpringCloud

本文所使用的组件:Nacos(服务中心和注册中心)、OpenFeign(服务调用)、Sentinel(限流、降级)、Hystrix(熔断)

项目结构:

  1. service-provider:提供服务的微服务。

  2. service-consumer:消费服务的微服务。

1. 添加依赖

在两个服务的pom.xml文件中添加Spring Cloud Alibaba、Nacos、Sentinel、Hystrix和OpenFeign的依赖。

<dependencies><!-- Spring Cloud Alibaba Nacos Discovery --><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId></dependency><!-- Spring Cloud Alibaba Sentinel --><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alibaba-sentinel</artifactId></dependency><!-- Hystrix --><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-hystrix</artifactId></dependency><!-- Spring Cloud OpenFeign --><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-openfeign</artifactId></dependency><!-- Spring Boot Web Starter --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency>
</dependencies>

2. Docker-compose.yml文件

创建docker-compose.yml文件,启动Nacos和Sentinel。

version: '3'
services:nacos-server:image: nacos/nacos-server:latestcontainer_name: nacos-serverports:- "8848:8848"environment:- MODE=standalone- SPRING_DATASOURCE_PLATFORM=mysqlsentinel-server:image: apache/incubating-sentinel-dashboard:latestcontainer_name: sentinel-serverports:- "8080:8080"- "8719:8719"
  • 部署在云服务中需要打开8080、8848端口,端口冲突可以更换端口。

3. service-provider(提供服务的微服务)

3.1 启动类

创建启动类并添加nacos注册客户端。

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.openfeign.EnableFeignClients;@SpringBootApplication
@EnableFeignClients
public class ServiceProviderApplication {public static void main(String[] args) {SpringApplication.run(ServiceProviderApplication.class, args);}
}

3.2 控制器

创建控制器提供服务。

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;@RestController
public class ServiceController {@GetMapping("/provider")public String provider() {return "Hello from Service Provider";}
}

4. service-consumer(消费服务的微服务)

4.1 OpenFeign客户端

创建Feign客户端用于调用service-provider

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;@FeignClient(name = "service-provider", fallback = ServiceFallback.class)
public interface ServiceClient {@GetMapping("/provider")String provider();
}@Component
public class ServiceFallback implements ServiceClient {@Overridepublic String provider() {// 服务降级逻辑return "Fallback response from Service Consumer";}
}

4.2 控制器

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;@RestController
public class ConsumerController {private final ServiceClient serviceClient;public ConsumerController(ServiceClient serviceClient) {this.serviceClient = serviceClient;}@GetMapping("/consumer")@HystrixCommand(commandKey = "consumerCommand", fallbackMethod = "fallback")@SentinelResource(value = "serviceClientCall", blockHandler = "blockHandler")public String consumer() {return serviceClient.provider();}public String fallback() {// 熔断降级逻辑return "Service is down, hystrix fallback";}public String blockHandler(BlockException ex) {// Sentinel降级逻辑return "Service is blocked by Sentinel";}
}
  • @HystrixCommand注解用于指定熔断器的命令键和降级方法,@SentinelResource注解用于指定资源名和降级方法。

5. application.yml

在两个服务添加application.yml文件。

spring:cloud:nacos:discovery:server-addr: nacos-server:8848config:server-addr: nacos-server:8848sentinel:transport:dashboard: sentinel-server:8080datasource:ds1:type: filefile:filePath: /path/to/your/sentinel/ruleshystrix:command:default:execution:isolation:strategy: THREADthread:timeoutInMilliseconds: 3000
  • filePath:文件路径

6. 配置规则

在Sentinel Dashboard(http://localhost:8080)中添加限流规则:

资源名:serviceClientCall
限流模式:QPS
阈值:10

总结:上述为一套完整的服务治理流程,对于某些场景下可以使用guava框架去实现单体限流。主要学习思想,本文所使用的组件可替换为其他具有相关功能的组件。

不积跬步,无以至千里 --- xiaokai

http://www.lryc.cn/news/491097.html

相关文章:

  • 2024最新YT-DLP使用demo网页端渲染
  • 《第十部分》1.STM32之通信接口《精讲》之IIC通信---介绍
  • wireshark使用lua解析自定义协议
  • (Keil)MDK-ARM各种优化选项详细说明、实际应用及拓展内容
  • Qt实现可拖拽的矩形
  • CentOS:A服务器主动给B服务器推送(上传),B服务器下载A服务器文件(下载)
  • Oracle 执行计划查看方法汇总及优劣对比
  • TCL大数据面试题及参考答案
  • 九、FOC原理详解
  • vue页面成绩案例(for渲染表格/删除/添加/统计总分/平均分/不及格显红色/输入内容去首尾空格trim/输入内容转数字number)
  • STM32编程小工具FlyMcu和STLINK Utility 《通俗易懂》破解
  • Centos使用docker搭建Graylog日志平台
  • 自定义 Kafka 脚本 kf-use.sh 的解析与功能与应用示例
  • 【SQL】【数据库】语句翻译例题
  • linux基本命令2
  • Spring Boot项目集成Redisson 原始依赖与 Spring Boot Starter 的流程
  • Git命令使用与原理详解
  • Linux:自定义Shell
  • vue项目中中怎么获取环境变量
  • C#里怎么样使用正则表达式?
  • 《生成式 AI》课程 第5講:訓練不了人工智慧?你可以訓練你自己 (下)
  • Vue 动态给 data 添加新属性深度解析:问题、原理与解决方案
  • 【Pytest+Yaml+Allure】实现接口自动化测试框架
  • el-input绑定点击回车事件意外触发页面刷新
  • Golang的语言特性与鸭子类型
  • 如何在Linux系统中排查GPU上运行的程序
  • VSCode 新建 Python 包/模块 Pylance 无法解析
  • Unet++改进44:添加MogaBlock(2024最新改进模块)|在纯基于卷积神经网络的模型中进行判别视觉表示学习,具有良好的复杂性和性能权衡。
  • 计算机网络(14)ip地址超详解
  • 【C语言】野指针问题详解及防范方法