spring-cloud服务降级之hystrix

1、在这里准备两个maven项目如下

  • eureka-provider  
  • hystrix-ribbon-fallback-consumer

 

2、 eureka-provider  下的 pom.xm配置

<dependency>
	<groupId>junit</groupId>
	<artifactId>junit</artifactId>
	<scope>test</scope>
</dependency>
<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>
	<version>2.1.0.RELEASE</version>
</dependency>
<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

3、eureka-provider 下的 application.yml配置

spring:
  application:
    name: eureka-provider

server: 
  port: 8081
  
eureka:
  client:
    serviceUrl: 
      defaultZone: http://user:123456@eureka1:8761/eureka/,http://user:123456@eureka2:8761/eureka/

3、eureka-provider 下提供一个Rest返回数据

@RestController
public class StudentController {

	@GetMapping("list")
	public List<Student> list() {
		List<Student> list = new ArrayList<Student>();
		list.add(new Student("A同学", 13));
		list.add(new Student("B同学", 16));
		list.add(new Student("C同学", 18));
		return list;
	}
}

4、eureka-provider 启动类

@EnableEurekaClient
@SpringBootApplication
public class YmSpringbootEurekaApplication {

	public static void main(String[] args) {
		SpringApplication.run(YmSpringbootEurekaApplication.class, args);
	}

}

 

5、hystrix-ribbon-fallback-consumer 下的pom.xml配置

<dependency>
	<groupId>junit</groupId>
	<artifactId>junit</artifactId>
	<version>3.8.1</version>
	<scope>test</scope>
</dependency>
<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>
	<version>2.1.0.RELEASE</version>
</dependency>
<dependency>
	<groupId>org.springframework.cloud</groupId>
	<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
	<version>2.1.0.RELEASE</version>
</dependency>

6、hystrix-ribbon-fallback-consumer 下的application.yml配置

spring:
  application:
    name: hystrix-ribbon-fallback-consumer

server: 
  port: 8082
  
eureka:
  client:
    serviceUrl: 
      defaultZone: http://user:123456@eureka1:8761/eureka/,http://user:123456@eureka2:8761/eureka/
  instance:
    prefer-ip-address: true  #默认是hostname注册,改成IP注册

7、hystrix-ribbon-fallback-consumer 下的service提供数据的方法上配置服务降级

@Service
public class StudentService {

	@Autowired
	private LoadBalancerClient loadBalancerClient;

	/**
	 * 配置当并发高的时候服务降级,并返回拖底数据,防止服务雪崩
	 * @return
	 */
	@HystrixCommand(fallbackMethod="fallback",
			commandProperties = {
					//如果10秒内并发达到15 ,请求会被 拒绝和抛出异常并且fallback不会被调用
					@HystrixProperty(name=HystrixPropertiesManager.FALLBACK_ISOLATION_SEMAPHORE_MAX_CONCURRENT_REQUESTS, value = "15")
			})
	public List<Student> studentList() {

		// 获取服务端
		ServiceInstance si = loadBalancerClient.choose("eureka-provider");//EUREKA-PROVIDER

		// 拼装服务端地址
		StringBuffer pUrl = new StringBuffer();
		pUrl.append("http://").append(si.getHost()).append(":").append(si.getPort()).append("/list");

		ParameterizedTypeReference<List<Student>> typeRef = new ParameterizedTypeReference<List<Student>>() {};

		//远程调用获取提供者的数据
		RestTemplate rt = new RestTemplate();
		ResponseEntity<List<Student>> resp = rt.exchange(pUrl.toString(), HttpMethod.GET, null, typeRef);
		List<Student> studentList = resp.getBody();
		return studentList;
	}
	
	/**
	 * fallback返回拖底数据
	 * 以下四种情况将触发getFallback调用
	 * 1、方法抛出非HystrixBadRequestException异常。
	 * 2、方法调用超时
	 * 3、熔断器开启拦截调用
	 * 4、线程池/队列/信号量是否跑满
	 * @return
	 */
	public  List<Student> fallback(){
		List<Student> list = new ArrayList<Student>();
		list.add(new Student("fallback",-1));
		return list;
	}
}

8、hystrix-ribbon-fallback-consumer 中调用service并提供rest接口访问测试

@RestController
public class StudentController {
	
	@Autowired
	private StudentService studentService;
	
	@GetMapping("list")
	public List<Student> list(){
		return studentService.studentList();
	}
}

9、hystrix-ribbon-fallback-consumer下的启动类,这里要开启服务降级断路器

@EnableCircuitBreaker     //开启服务降级断路器
@EnableEurekaClient
@SpringBootApplication
public class YmSpringbootEurekaApplication {

	public static void main(String[] args) {
		SpringApplication.run(YmSpringbootEurekaApplication.class, args);
	}

}

10、测试

正常访问:

关掉 eureka-provider

接口虽然断了,但是返回了拖底数据,不至于超时或报错,这样就不会引起整其它服务造成雪崩的情况发生

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值