一个项目中发布的服务往往会有很多服务去调用。一个服务挂掉会导致涌入的请求全部阻塞。这时候断路器就出现了。断路器就是一个很好的处理的一个方法。对特定的服务的调用的不可用达到一个阀值(Hystric 是5秒20次) 断路器将会被打开。断路打开后,可用避免连锁故障,fallback方法可以直接返回一个固定值。
【在ribbon中使用断路器】
1.引入依赖
2.在启动类中加@EnableHystrix,启动Hystrix
3.在service层可能出错的接口加@HystrixCommand注解,创建熔断功能,通过fallback()方法转向指定的方法(自行创建)输出。
步骤一:ribbon中没有继承断路器,所以要引入依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix</artifactId>
</dependency>步骤二:在启动类中加@EnableHystrix,启动Hystrix
package com.cloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.hystrix.EnableHystrix;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
@SpringBootApplication
@EnableDiscoveryClient
@EnableHystrix
public class ServiceRibbonApplication {
public static void main(String[] args) {
SpringApplication.run(ServiceRibbonApplication.class, args);
System.out.println("******ribbon已启动********");
}
@Bean
@LoadBalanced
//通过该@LoadBalanced注解进行负载均衡
RestTemplate restTemplate() {
return new RestTemplate();
}
}步骤三:在service层可能出错的接口加@HystrixCommand注解,创建熔断功能,通过fallback()方法转向指定的方法(自行创建)输出。
package com.cloud.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
@Service
public class HelloService {
@Autowired
RestTemplate restTemplate;
//通过该注解转移到另一个接口上
@HystrixCommand(fallbackMethod="hiError")
public String helloService(String name) {
return restTemplate.getForObject("http://service-hello/hello?name="+name, String.class);
}
public String hiError(String name) {
return "hi,"+name+"sorry,error";
}
}

【在Feign中使用断路器】
1.在配置文件中开启断路器 feign.hystrix.enabled:true
2.建立特定的类实现service层中类,其中包括出错输出的指定方法,通过@Component注解注入容器
3.在消费服务方 的注解中加上fallback , @FeignClient(value="",fallback=?.class)指向指定的方法
步骤一:在配置文件中开启断路器 feign.hystrix.enabled:true
eureka:
client:
serviceUrl:
defaultZone: http://localhost:10001/eureka/
server:
port: 8085
spring:
application:
name: service-feign
feign:
hystrix:
enabled: true步骤二:建立特定的类实现service层中类,其中包括出错输出的指定方法,通过@Component注解注入容器
package com.cloud.interfac;
import org.springframework.stereotype.Component;
@Component
public class SchedualServiceHelloHyStric implements SchedualServiceHello{
@Override
public String sayHelloFromClientOne(String name) {
// TODO Auto-generated method stub
return "sorry "+name;
}
}步骤三:在消费服务方 的注解中加上fallback , @FeignClient(value="",fallback=?.class)指向指定的方法
package com.cloud.interfac;
import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
//调用了service-hello服务的“/hello”接口
@FeignClient(value="service-hello",fallback=SchedualServiceHelloHyStric.class)
public interface SchedualServiceHello {
@RequestMapping(value="/hello",method=RequestMethod.GET)
String sayHelloFromClientOne(@RequestParam(value="name")String name);
}

本文介绍了在Spring Cloud中如何使用断路器来防止服务雪崩。当服务调用不可用达到一定阈值时,断路器会打开,避免连锁故障。在Ribbon中,需要引入依赖、启用Hystrix并在Service层使用@HystrixCommand注解配合fallback方法。而在Feign中,需在配置文件中开启断路器,创建特定的类实现Service层并使用@FeignClient的fallback属性指向错误处理方法。
670

被折叠的 条评论
为什么被折叠?



