一)线程隔离
什么是线程隔离,如下图Customer通过线程池,访问服务接口,接口A面对的10次的请求量,接口B是10次的访问量,比例是1:10000,此时接口A和接口B的连接是在同一个的线程池中,如果接口A因为的访问量过大的原因出现问题,势必影响线程池的效率,而线程池中的其他线程也会受到影响,从而造成雪崩效应。
那么我们该怎么解决线程池内独立线程在运行的运行的时候不会影响到的其他的线程呢?那就要用到线程的的隔离技术了。把可能出现问题的服务独立的运行在一个独立的线程池中。
线程池的隔离如下图中两个线程池我们将服务量大的请求单独的运行在一个的独立的线程池中,两个线程池相互之间是不影响的。可以使用线程池隔离的技术将的线程池内的可能预估出现问题的线程和其他的线程的隔开运行在一个独立的线程池中,一旦此线程出现问题,不会影响到其他线程的运行,解决雪崩效应的产生。
线程池的优缺点:
优点:
1.使用线程隔离可以完成隔离依赖的服务(如图中的A,B,C服务),请求线程可以快速的放回线程池中。
2.当线程池出现问题的时候,线程池内隔离的线程是独立的,不会影响其他的服务和接口
3.当失败的服务在此变的可用的时候,线程池将清理并可立即恢复,而不需要长时间的等待恢复。
4.独立的线程池提高的了并发性。
缺点:
线程池的隔离的主要缺点是就是对CPU的开销会很大,每个命令涉及到指令,排毒,调度和上下文的切换是一个的单独的线程运行的。
二)创建项目测试的线程池的隔离
1.pom文件
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.7.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.sxt</groupId>
<artifactId>21-hystrix-threadpool</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>21-hystrix-threadpool</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Greenwich.SR2</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
2.修改全局配置文件
spring.application.name=21-hystrix-threadpool
server.port=9096
eureka.client.service-url.defaultZone=http://peer1:8081/eureka/,http://peer2:8082/eureka/,http://peer3:8083/eureka/
spring.profiles.active=
启动类
@SpringBootApplication
@EnableEurekaClient
@EnableCircuitBreaker
public class Application
{
public static void main(String[] args)
{
SpringApplication.run(Application.class, args);
}
}
3.修改ProductService增加线程隔离
package com.sxt.service;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCollapser;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import com.netflix.hystrix.contrib.javanica.conf.HystrixPropertiesManager;
import com.sxt.pojo.Product;
import org.springframework.stereotype.Service;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.Future;
/**
* @project_name:springcloud
* @date:2019/9/3:17:26
* @author:shinelon
* @Describe:
*/
@Service
public class ProductService
{
/**
* 线程池隔离:
* @注解:@HystrixCommand:熔断设定
* groupKey:给当前线程池起一个的组ID,拥有的相同组ID的会在同一个的线程池中运行
* commandKey:当前被隔离的方法名字
* threadPoolKey:给当前线程池起一个的名字,没有的设定默认为线程的groupKey的值
*
* @param flag 根据传递的参数进行异常的抛出
* @return
*/
@HystrixCommand(groupKey = "ego-product-provider", commandKey = "getUsers", threadPoolKey = "ego-product-provider", threadPoolProperties = {
@HystrixProperty(name = "coreSize", value = "30"),//线程池大小 容纳的线程个数
@HystrixProperty(name = "maxQueueSize", value = "100"),//最大队列长度
@HystrixProperty(name = "keepAliveTimeMinutes", value = "2"),//线程存活时间 默认为1分钟
@HystrixProperty(name = "queueSizeRejectionThreshold", value = "15")//拒绝请求 当长度为15的时候拒绝请求
}, fallbackMethod = "fallbackMethod") //托底数据的方法
public Product getProduct(int flag)
{
System.out.println(Thread.currentThread().getName());
if (flag == 1)
{
throw new RuntimeException();
}
return new Product("熔断器", flag);
}
/**
* @param
* @description: 托底方法 在使用线程隔离的时候,托底方法的数据隔离的方法在一个的线程池中
* @return:
* @author: shinelon
* @time: 2019/9/3:19:53
*/
public Product fallbackMethod(int flag)
{
System.out.println(Thread.currentThread().getName());
return new Product("托底数据", flag);
}
/**
*区别的线程池
*/
public void testPool(){
System.out.println(Thread.currentThread().getName());
}
}
4.修改Controller
@Controller
public class ProductController
{
@Autowired
private ProductService productService;
/**
* 模拟测试请求的合并
* @throws ExecutionException
* @throws InterruptedException
*/
@GetMapping(value = "/customer")
@ResponseBody
public Product setProductService(int id) throws Exception
{
return this.productService.getProduct(id);
}
@GetMapping("/testPool")
public String testPool(){
this.productService.testPool();
return "";
}
}
注意线程的名字
明显被隔离的线程不在同一个的线程池中。