对网站建设课程的心得体会天津seo排名效果好
目录
- 资源隔离
- 使用资源隔离的好处
- 基于Hystrix实现微服务中资源隔离
- 基于Hystrix线程池隔离实现资源隔离
- 利用 HystrixCommand 获取单条数据
- 利用 HystrixObservableCommand 批量获取数据
- 基于 Hystrix 信号量机制实现资源隔离
资源隔离
资源隔离是什么?
资源隔离是指把对某一个依赖服务的所有调用请求,全部隔离在同一份资源池内,不会去用其它资源了。哪怕对这个依赖服务,比如说商品服务,现在同时发起的调用量已经到了 1000,但是分配给商品服务线程池内就 10 个线程,最多就只会用这 10 个线程去执行。不会因为对商品服务调用的延迟,将 Tomcat 内部所有的线程资源全部耗尽。
使用资源隔离的好处
- 合理分配资源,把给资源分配的控制权交给用户。
- 多个微服务之间进行调用的时候,假设服务A调用服务B和服务C,服务B和服务C又调用其他微服务,这就是所谓的"扇出",如果扇出的链路上某个微服务的调用响应时间过长,如线程阻塞了,容器中的线程数量就则会持续增加,这个时候服务A占用的线程资源越来越多,进而引起系统奔溃,这就是“雪崩效应”,资源隔离能够在一定程度上防止微服务的雪崩效应。
- 对依赖调用进行封装有利于对调用的监控和分析,类似于hystrix-dashboard的使用。
基于Hystrix实现微服务中资源隔离
Hystrix是Netflix开源的一款容错框架,包含常用的容错方法:线程隔离、信号量隔离、服务降级、服务熔断。
Hystrix有一项比较核心的功能,就是所谓的资源隔离,资源隔离要解决的核心问题,就是将多个依赖服务的调用分别隔离到各自的资源池中,避免某一个依赖服务的调用因为依赖服务的接口调用的延迟或者失败,导致所有的线程资源全部耗费在这个服务的接口调用上,一旦某个服务的线程资源全部耗尽,就可能导致雪崩效应。
Hystrix实现资源隔离,主要有两种隔离方式:
- 线程池隔离
- 信号量隔离
基于Hystrix线程池隔离实现资源隔离
Hystrix进行资源隔离,其实是提供了一个抽象,叫做Command。这个也是Hystrix最基本的资源隔离技术。
利用 HystrixCommand 获取单条数据
我们通过将调用商品的服务操作封装在HystrixCommand中,并限定一个key,比如下面的GetProductInfoCommandGroup,在这里,我们可以简单认为是一个线程池,每次调用商品服务,就只会用该线程池中的资源,不会再去用其他的资源了。
public class GetProductInfoCommand extends HystrixCommand<ProductInfo> {private Long productId;public GetProductInfoCommand(Long productId) {super(HystrixCommandGroupKey.Factory.asKey("GetProductInfoCommandGroup"));this.productId = productId;}@Overrideprotected ProductInfo run() {String url = "http://localhost:8081/getProductInfo?productId=" + productId;// 调用商品服务接口String response = HttpClientUtils.sendGetRequest(url);return JSONObject.parseObject(response, ProductInfo.class);}
}
我们在接口中,根据 productId 创建 Command 并执行,获取到商品数据。
@RequestMapping("/getProductInfo")
@ResponseBody
public String getProductInfo(Long productId) {HystrixCommand<ProductInfo> getProductInfoCommand = new GetProductInfoCommand(productId);// 通过command执行,获取最新商品数据ProductInfo productInfo = getProductInfoCommand.execute();System.out.println(productInfo);return "success";
}
上面执行的方法是execute()方法,这个方法是同步的。
我们也可以调用command.queue()方法,它将command放入线程池的一个等待队列中,然后理解返回,后面可以继续做其他一些事情,然后过一段时间对Future调用get()方法获取数据,这是异步的。
利用 HystrixObservableCommand 批量获取数据
只要是获取商品数据,全部都绑定到同一个线程池里面去,我们通过 HystrixObservableCommand 的一个线程去执行,而在这个线程里面,批量把多个 productId 的 productInfo 拉回来。
public class GetProductInfosCommand extends HystrixObservableCommand<ProductInfo> {private String[] productIds;public GetProductInfosCommand(String[] productIds) {// 还是绑定在同一个线程池super(HystrixCommandGroupKey.Factory.asKey("GetProductInfoGroup"));this.productIds = productIds;}@Overrideprotected Observable<ProductInfo> construct() {return Observable.unsafeCreate((Observable.OnSubscribe<ProductInfo>) subscriber -> {for (String productId : productIds) {// 批量获取商品数据String url = "http://localhost:8081/getProductInfo?productId=" + productId;String response = HttpClientUtils.sendGetRequest(url);ProductInfo productInfo = JSONObject.parseObject(response, ProductInfo.class);subscriber.onNext(productInfo);}subscriber.onCompleted();}).subscribeOn(Schedulers.io());}
}
在缓存服务接口中,根据传来的 id 列表,比如是以 , 分隔的 id 串,通过上面的 HystrixObservableCommand,执行 Hystrix 的一些 API 方法,获取到所有商品数据。
public String getProductInfos(String productIds) {String[] productIdArray = productIds.split(",");HystrixObservableCommand<ProductInfo> getProductInfosCommand = new GetProductInfosCommand(productIdArray);Observable<ProductInfo> observable = getProductInfosCommand.observe();observable.subscribe(new Observer<ProductInfo>() {@Overridepublic void onCompleted() {System.out.println("获取完了所有的商品数据");}@Overridepublic void onError(Throwable e) {e.printStackTrace();}/*** 获取完一条数据,就回调一次这个方法* @param productInfo*/@Overridepublic void onNext(ProductInfo productInfo) {System.out.println(productInfo);}});return "success";
}
基于 Hystrix 信号量机制实现资源隔离
信号量的资源隔离只是起到一个开关的作用,比如,服务 A 的信号量大小为 10,那么就是说它同时只允许有 10 个 tomcat 线程来访问服务 A,其它的请求都会被拒绝,从而达到资源隔离和限流保护的作用。
线程池隔离技术,是用 Hystrix 自己的线程去执行调用,并不是去控制tomcat容器的线程,Hystrix线程池满后,Tomcat的线程不会因为依赖服务的接口调用延迟或者故障而被阻塞,不会卡死在哪里,可以去做其他事情。
Hystrix信号量隔离,是控制Tomcat容器中的线程数,信号量有多少,就允许多少个Tomcat线程通过它,然后去执行。
信号量的是什么场景呢?
举个栗子。一般我们在获取到商品数据之后,都要去获取商品是属于哪个地理位置、省、市、卖家等,可能在自己的纯内存中,比如就一个 Map 去获取。对于这种直接访问本地内存的逻辑,比较适合用信号量做一下简单的隔离。
优点在于,不用自己管理线程池啦,不用 care timeout 超时啦,也不需要进行线程的上下文切换啦。信号量做隔离的话,性能相对来说会高一些。
假如这是本地缓存,我们可以通过 cityId,拿到 cityName。
public class LocationCache {private static Map<Long, String> cityMap = new HashMap<>();static {cityMap.put(1L, "北京");}/*** 通过cityId 获取 cityName** @param cityId 城市id* @return 城市名*/public static String getCityName(Long cityId) {return cityMap.get(cityId);}
}
写一个 GetCityNameCommand,策略设置为信号量。run () 方法中获取本地缓存。我们目的就是对获取本地缓存的代码进行资源隔离。
public class GetCityNameCommand extends HystrixCommand<String> {private Long cityId;public GetCityNameCommand(Long cityId) {// 设置信号量隔离策略super(Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey("GetCityNameGroup")).andCommandPropertiesDefaults(HystrixCommandProperties.Setter().withExecutionIsolationStrategy(HystrixCommandProperties.ExecutionIsolationStrategy.SEMAPHORE)));this.cityId = cityId;}@Overrideprotected String run() {// 需要进行信号量隔离的代码return LocationCache.getCityName(cityId);}
}
在接口层,通过创建 GetCityNameCommand,传入 cityId,执行 execute () 方法,那么获取本地 cityName 缓存的代码将会进行信号量的资源隔离。
@RequestMapping("/getProductInfo")
@ResponseBody
public String getProductInfo(Long productId) {HystrixCommand<ProductInfo> getProductInfoCommand = new GetProductInfoCommand(productId);// 通过command执行,获取最新商品数据ProductInfo productInfo = getProductInfoCommand.execute();Long cityId = productInfo.getCityId();GetCityNameCommand getCityNameCommand = new GetCityNameCommand(cityId);// 获取本地内存(cityName)的代码会被信号量进行资源隔离String cityName = getCityNameCommand.execute();productInfo.setCityName(cityName);System.out.println(productInfo);return "success";
}