hystrix是netflix开源的服务熔断组件,在Spring Cloud中整合进来,形成Spring Cloud的熔断降级体系。服务降级主要是为了服务雪崩,造成下游的服务不可用,可用性是分布式服务的必须要求。
1.pom文件修改:(增添依赖)
<dependency>
<groupId>com.netflix.hystrix</groupId>
<artifactId>hystrix-javanica</artifactId>
<version>RELEASE</version>
</dependency>
2.要开启服务,需要在main类上注上@EnableCircuitBreaker
@SpringBootApplication
@EnableDiscoveryClient
@EnableCircuitBreaker
public class OrderApplication {
public static void main(String[] args) {
SpringApplication.run(OrderApplication.class, args);
}
}
3.同样,熔断后需要如何处理,需要调用方写处理逻辑。需要在服务方法上加上@HystrixCommand注解
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import java.util.Arrays;
@RestController
public class HystrixController {
@HystrixCommand(fallbackMethod = "fallback")
@GetMapping("/getProductInfoList")
public String getProductInfoList(){
RestTemplate restTemplate=new RestTemplate();
//调用product服务的listForOrder接口,大家可以根据项目需求自己coding
return restTemplate.postForObject("http://127.0.0.1:8083/product/listForOrder", Arrays.asList("157875196366160022"),String.class);
}
private String fallback(){
return "太拥挤了,请稍后重试";
}
}
4.正常访问,效果如图
5.停掉product服务,就会走fallback函数,如图所示:
6.超时设置(可以避免被调用方访问时间较长而调用方访问不到)
import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import java.util.Arrays;
@RestController
@DefaultProperties(defaultFallback = "defaultFallback")
public class HystrixController {
@HystrixCommand(commandProperties = {
@HystrixProperty(name="execution.isolation.thread.timeoutInMilliseconds",value = "3000")
})
@GetMapping("/getProductInfoList")
public String getProductInfoList(){
RestTemplate restTemplate=new RestTemplate();
return restTemplate.postForObject("http://127.0.0.1:8083/product/listForOrder", Arrays.asList("157875196366160022"),String.class);
}
private String fallback(){
return "太拥挤了,请稍后重试";
}
private String defaultFallback(){
return "默认提示:太拥挤了,请稍后重试";
}
}
- Hystrix实现服务熔断
import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import java.util.Arrays;
@RestController
@DefaultProperties(defaultFallback = "defaultFallback")
public class HystrixController {
//超时配置
// @HystrixCommand(commandProperties = {
// @HystrixProperty(name="execution.isolation.thread.timeoutInMilliseconds",value = "3000")
// })
//服务熔断
@HystrixCommand(commandProperties = {
@HystrixProperty(name="circuitBreaker.enabled",value = "true"), //设置熔断
@HystrixProperty(name="circuitBreaker.requestVolumeThreshold",value = "10"), // 滚动时间窗口中断路器最小请求数
@HystrixProperty(name="circuitBreaker.sleepWindowInMilliseconds",value = "10000"), //休眠时间
@HystrixProperty(name="circuitBreaker.errorThresholdPercentage",value = "60") //错误率
})
@GetMapping("/getProductInfoList")
public String getProductInfoList(@RequestParam("number") Integer number){
if(number%2==0){
return "success";
}
RestTemplate restTemplate=new RestTemplate();
return restTemplate.postForObject("http://127.0.0.1:8083/product/listForOrder", Arrays.asList("157875196366160022"),String.class);
}
private String fallback(){
return "太拥挤了,请稍后重试";
}
private String defaultFallback(){
return "默认提示:太拥挤了,请稍后重试";
}
}
8.对熔断可视化的组件Hystrix-dashboard
8.1pom文件的修改:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-netflix-hystrix-dashboard</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
8.2在启动类上新增@EnableHystrixDashboard注解
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.SpringCloudApplication;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.ComponentScan;
@EnableFeignClients(basePackages = "com.imooc.product.client")
//@SpringBootApplication
//@EnableDiscoveryClient
//@EnableCircuitBreaker
@SpringCloudApplication
@ComponentScan(basePackages = "com.imooc")
@EnableHystrixDashboard //新增可视化组件
public class OrderApplication {
public static void main(String[] args) {
SpringApplication.run(OrderApplication.class, args);
}
}
8.3在application.yml文件新增配置:
management:
server:
servlet:
context-path: /
endpoints:
web:
exposure:
include: ["health","info","hystrix.stream"]
8.4启动界面
8.5不停访问后类似如下界面:
小伙伴们有木有学到呢?赶紧动手尝试一波把!