SpringCloud 微服务架构(12)- 线程池隔离的方式处理请求积压

本文介绍如何使用Hystrix的线程池隔离策略处理请求积压,通过Order模块示例,展示了如何配置HystrixCommand进行服务分组、标识及线程池设置,包括线程池大小、存活时间及拒绝策略,以实现服务的高可用性和容错性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1 线程池隔离的方式处理请求积压

  • Order 模块
<dependency>
    <groupId>com.netflix.hystrix</groupId>
    <artifactId>hystrix-metrics-event-stream</artifactId>
    <version>1.5.12</version>
</dependency>
<dependency>
    <groupId>com.netflix.hystrix</groupId>
    <artifactId>hystrix-javanica</artifactId>
    <version>1.5.12</version>
</dependency>

在这里插入图片描述

package com.tzb.order.command;


import com.netflix.hystrix.*;
import com.tzb.order.entity.Product;
import org.springframework.web.client.RestTemplate;

public class OrderCommand extends HystrixCommand<Product> {

	private RestTemplate restTemplate;
	
	private Long id;

	public OrderCommand(RestTemplate restTemplate, Long id) {
		super(setter());
		this.restTemplate = restTemplate;
		this.id = id;
	}

	private static Setter setter() {

		// 服务分组
		HystrixCommandGroupKey groupKey = HystrixCommandGroupKey.Factory.asKey("order_product");
		// 服务标识
		HystrixCommandKey commandKey = HystrixCommandKey.Factory.asKey("product");
		// 线程池名称
		HystrixThreadPoolKey threadPoolKey = HystrixThreadPoolKey.Factory.asKey("order_product_pool");
		/**
		 * 线程池配置
		 *     withCoreSize :  线程池大小为10
		 *     withKeepAliveTimeMinutes:  线程存活时间15秒
		 *     withQueueSizeRejectionThreshold  :队列等待的阈值为100,超过100执行拒绝策略
		 */
		HystrixThreadPoolProperties.Setter threadPoolProperties = HystrixThreadPoolProperties.Setter().withCoreSize(50)
				.withKeepAliveTimeMinutes(15).withQueueSizeRejectionThreshold(100);

		// 命令属性配置Hystrix 开启超时
		HystrixCommandProperties.Setter commandProperties = HystrixCommandProperties.Setter()
				// 采用线程池方式实现服务隔离
				.withExecutionIsolationStrategy(HystrixCommandProperties.ExecutionIsolationStrategy.THREAD)
				// 禁止
				.withExecutionTimeoutEnabled(false);
		return Setter.withGroupKey(groupKey).andCommandKey(commandKey).andThreadPoolKey(threadPoolKey)
				.andThreadPoolPropertiesDefaults(threadPoolProperties).andCommandPropertiesDefaults(commandProperties);

	}

	@Override
	protected Product run() throws Exception {
		System.out.println(Thread.currentThread().getName());
		return restTemplate.getForObject("http://127.0.0.1/product/"+id, Product.class);
	}

	/**
	 * 降级方法
	 * @return
	 */
	@Override
	protected Product getFallback(){
		Product product = new Product();
		product.setProductName("出错啦!");
		return product;
	}
}


1. 微服务架构中的基础设施主要包括: - 服务注册与发现:通过注册中心实现服务的自动化注册与发现,常见的有Zookeeper、Eureka、Consul等。 - 负载均衡:实现多个服务实例之间的负载均衡,常见的有Ribbon、Nginx、HAProxy等。 - 配置管理:集中管理各个微服务所需的配置信息,常见的有Spring Cloud Config、Consul等。 - 服务网关:为外部客户端提供统一的API入口,常见的有Zuul、Spring Cloud Gateway等。 - 分布式追踪:跟踪请求微服务之间的传递路径,常见的有Zipkin、SkyWalking等。 2. 在微服务架构中,断路器的作用是保护系统免受服务故障的影响。当某个服务出现故障或响应时间变慢时,断路器会暂时中断对该服务的访问,避免请求积压导致整个系统崩溃。同时,断路器还可以提供服务降级、熔断等功能,保证系统的稳定性和可靠性。 3. 微服务架构中常用的负载均衡方式有两种: - 客户端负载均衡:客户端通过负载均衡算法从服务注册中心获取服务实例列表,并自行选择其中一台进行访问。常见的实现方式有Ribbon、Feign等。 - 服务端负载均衡:在服务网关和负载均衡器等组件中实现,将请求分发到各个服务实例中,常见的实现方式有Zuul、Spring Cloud Gateway等。 4. Spring Cloud提供了一系列注解来支持微服务架构的开发,其中一些关键的注解包括: - @EnableDiscoveryClient:启用服务注册与发现功能。 - @EnableCircuitBreaker:启用断路器功能。 - @LoadBalanced:启用客户端负载均衡。 - @FeignClient:定义服务接口及其实现。 - @EnableZuulProxy:启用Zuul服务网关。 - @RefreshScope:支持配置动态刷新。 Spring Cloud是基于Spring Boot的微服务框架,用于简化微服务架构的开发与部署,并提供了丰富的组件和工具支持。Spring Boot则是基于Spring框架的快速开发框架,用于简化企业级应用的开发。两者之间存在较强的关联性,Spring Cloud可以看作是在Spring Boot基础上提供了更多的微服务功能和支持。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值