<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0modelVersion>
<parent>
<groupId>com.liu.susugroupId>
<artifactId>dog-cloud-parentartifactId>
<version>1.0-SNAPSHOTversion>
parent>
<artifactId>dog-consumer-ribbon-hystrix-80artifactId>
<packaging>jarpackaging>
<name>dog-consumer-ribbon-hystrix-80name>
<url>http://maven.apache.orgurl>
<properties>
<project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
properties>
<dependencies>
<dependency>
<groupId>com.liu.susugroupId>
<artifactId>dog-poartifactId>
<version>${project.version}version>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-eurekaartifactId>
dependency>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-ribbonartifactId>
dependency>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-configartifactId>
dependency>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-hystrixartifactId>
dependency>
<dependency>
<groupId>junitgroupId>
<artifactId>junitartifactId>
<scope>testscope>
dependency>
dependencies>
project>
server:
port: 80
spring:
application:
name: dog-consumer-ribbon-hystrix
eureka:
client: # 客户端注册进eureka服务列表内
register-with-eureka: false # false表示不向注册中心注册自己
service-url:
defaultZone: http://IP1:2886/eureka/,http://IP2:2886/eureka,http://IP3:2886/eureka/
package com.liu.susu.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
/**
* @Description
* @Author susu
*/
@RestController
public class DogConsumerController {
private static final String REST_URL_PREFIX = "http://DOG-PROVIDER";
@Autowired
private RestTemplate restTemplate;
@RequestMapping(value = "/consumer/dog/hello")
public String hello(){
System.out.println("==============================");
String url = REST_URL_PREFIX + "/dog/hello";
return restTemplate.getForObject(url, String.class);
}
/**
* http://localhost:80/consumer/dog/getDogByNum/1
* http://localhost/consumer/dog/getDogByNum/1
*/
@RequestMapping("/consumer/dog/getDogByNum/{dogNum}")
public Object getDogByNum(@PathVariable("dogNum") Long dogNum){
String url = REST_URL_PREFIX + "/dog/getDogByNum/" + dogNum;
return restTemplate.getForObject(url, Object.class);
}
}
服务降级是指在服务出现故障或异常时,为了保证核心服务的稳定性,暂时关闭一些不太重要的服务,或者返回一些简单的缓存数据等,以保证核心服务的正常运行。服务降级能够有效地减少服务的响应时间和错误率,提高系统的可用性。
即:服务降级是针对系统整体资源的合理分配。区分核心服务和非核心服务。对某个服务的访问延迟时间、异常等情况做出预估并给出兜底方法。这是一种全局性的考量,从整体负荷考虑。
启动类
@EnableHystrix //开启Hystrix容错处理能力
controller里方法
@HystrixCommand(fallbackMethod = "errorHello")
/**
* @EnableHystrix 启动类上注解,开启Hystrix容错处理能力
* @HystrixCommand 代表当前方法是一个需要做容错处理的方法
* @EnableHystrix 结合 @HystrixCommand,默认地配置了一个远程服务超时配置,默认设置超时是1秒
*/
@RequestMapping(value = "/consumer/dog/hello")
@HystrixCommand(fallbackMethod = "errorHello")
public String hello(){
System.out.println("==============================");
//默认情况下,SpringCloud远程调用时,不管多久都会等
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
String url = REST_URL_PREFIX + "/dog/hello";
return restTemplate.getForObject(url, String.class);
}
/**
* 降级方法
* 1、返回类型要和对应的服务方法的返回类型一致
* 2、参数和对应的服务方法要一致
* 3、返回的内容:远程服务访问错误时(比如超时),返回的拖底数据
*/
public String errorHello(){
return "服务器忙,请稍后再试!";
}
如下:
@RequestMapping("/consumer/dog/getDogByNum/{dogNum}")
@HystrixCommand(fallbackMethod = "errorGetDogByNum",commandProperties = {
//是否开启熔断机制
@HystrixProperty(name = HystrixPropertiesManager.CIRCUIT_BREAKER_ENABLED,value = "true"),
//一个时间窗内,发生远程访问错误的次数阈值,达到开启熔断
@HystrixProperty(name = HystrixPropertiesManager.CIRCUIT_BREAKER_REQUEST_VOLUME_THRESHOLD,value = "3"),
//一个时间窗内,发生远程访问错误的百分比,达到则开启熔断
@HystrixProperty(name = HystrixPropertiesManager.CIRCUIT_BREAKER_ERROR_THRESHOLD_PERCENTAGE,value = "20"),
//开启熔断后,多少毫秒内,不发起远程服务访问
@HystrixProperty(name = HystrixPropertiesManager.CIRCUIT_BREAKER_SLEEP_WINDOW_IN_MILLISECONDS,value = "3000")
})
public Object getDogByNum(@PathVariable("dogNum") Long dogNum){
System.out.println("本地测试熔断....");
String url = REST_URL_PREFIX + "/dog/getDogByNum/" + dogNum;
return restTemplate.getForObject(url, Object.class);
}
public Object errorGetDogByNum(Long dogNum){
System.out.println("进入熔断,dogNum是:===>"+dogNum);
return "熔断——服务器忙,请稍后再试!";
}
@RequestMapping("/consumer/dog/getDogByNum/{dogNum}")
@HystrixCommand(fallbackMethod = "errorGetDogByNum",commandProperties = {
//是否开启熔断机制
@HystrixProperty(name = HystrixPropertiesManager.CIRCUIT_BREAKER_ENABLED,value = "true"),
//一个时间窗内,发生远程访问错误的次数阈值,达到开启熔断
@HystrixProperty(name = HystrixPropertiesManager.CIRCUIT_BREAKER_REQUEST_VOLUME_THRESHOLD,value = "3"),
//一个时间窗内,发生远程访问错误的百分比,达到则开启熔断
@HystrixProperty(name = HystrixPropertiesManager.CIRCUIT_BREAKER_ERROR_THRESHOLD_PERCENTAGE,value = "20"),
//开启熔断后,多少毫秒内,不发起远程服务访问
@HystrixProperty(name = HystrixPropertiesManager.CIRCUIT_BREAKER_SLEEP_WINDOW_IN_MILLISECONDS,value = "3000")
})
public Object getDogByNum(@PathVariable("dogNum") Long dogNum){
System.out.println("本地测试熔断....");
System.out.println(2 / dogNum);
String url = REST_URL_PREFIX + "/dog/getDogByNum/" + dogNum;
return restTemplate.getForObject(url, Object.class);
}
public Object errorGetDogByNum(Long dogNum){
System.out.println("进入熔断,dogNum是:===>"+dogNum);
return "熔断——服务器忙,请稍后再试!";
}
package com.liu.susu.api;
import com.liu.susu.pojo.Dog;
import feign.hystrix.FallbackFactory;
import org.springframework.stereotype.Component;
import java.util.List;
/**
* @Description
* @Author susu
*/
@Component
public class DogClientApiFallbackFactory implements FallbackFactory<DogClientApi> {
@Override
public DogClientApi create(Throwable throwable) {
return new DogClientApi() {
@Override
public String hello() {
System.out.println("进入 DogClientApiFallbackFactory 服务降级--->hello");
return "DogClientApiFallbackFactory 服务降级处理,请稍后再试";
}
@Override
public Object getDogByNum(Long dogNum) {
return null;
}
@Override
public List<Dog> getAllDog() {
return null;
}
};
}
}
修改注解,如下:
@FeignClient(value = "DOG-PROVIDER",fallbackFactory = DogClientApiFallbackFactory.class) //方式2