hystrix由来:服务器宕机或者依赖关系失败。

hystrix:

Hystrix是一个用于处理分布式系统的延迟和容错的开源库,在分布式系统里,许多依赖不可避免的会调用失败,比如超时、异常等,Hystrix能够保证在一个依赖出问题的情况下,不会导致整体服务失败,避免级联故障,以提高分布式系统的弹性。
“断路器”本身是一种开关装置,当某个服务单元发生故障之后,通过断路器的故障监控(类似熔断保险丝),向调用方返回一个符合预期的、可处理的备选响应(FallBack),而不是长时间的等待或者抛出调用方无法处理的异常,这样就保证了服务调用方的线程不会被长时间、不必要地占用,从而避免了故障在分布式系统中的蔓延,乃至雪崩。
雪崩:
服务器A调用服务器B,服务器B调用服务器C.......一台服务可能会调用多台服务器,一台服务器也可能会被多态服务器调用,如果一台服务器宕机,会对整个系统造成灾难性的后果。
 
在ribbon中使用hystrix:
1:修改pom文件(添加依赖):
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- 热部署 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
 
<!-- eureka客户端 -->
<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-ribbon</artifactId>
</dependency>
 
<!-- 断路器 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
</dependencies>
2:修改配置文件(application.yml):

server:
port: 6003
eureka:
client:
service-url:
defaultZone: http://localhost:9001/eureka
instance:
instance-id: consumer6003
prefer-ip-address: true
spring:
application:
name: consumer3

3:启动类(App):

@SpringBootApplication
@EnableEurekaClient
@Configuration
@EnableHystrix
public class App {

public static void main(String[] args) {

SpringApplication.run(App.class, args);

}

}

4:控制器(HelloController):

@SuppressWarnings("unchecked")
@RestController
public class HelloController {

@Autowired
private RestTemplate restTemplate;

@RequestMapping("/hi")
@HystrixCommand(fallbackMethod="helloFallback")
public Map<String,Object> hello(){
return restTemplate.getForObject("http://provider-one/hello", Map.class);
}

public Map<String,String> helloFallback(){
Map<String,String> map = new HashMap<String, String>();

map.put("info", "失败");
return map;
}

}

比较ribbo:

  在方法上添加注解@HystrixCommand(fallbackMethod="helloFallback")

  属性fallbackMethod的值是你要调用的方法

 
在feign中使用hystrix:
1:修改配置文件pom.xml(添加依赖):

<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-feign</artifactId>
<version>1.4.2.RELEASE</version>
</dependency>
<!-- 热部署 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
<!-- 断路器 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
</dependencies>

2:修改配置文件(application.yml):

server:
port: 6004
eureka:
client:
service-url:
defaultZone: http://localhost:9001/eureka
instance:
instance-id: consumer6004
prefer-ip-address: true
spring:
application:
name: consumer4

feign:
hystrix:
enabled: true

3:启动类(App):

@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients
@Configuration
public class App {

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

}

4:接口

@FeignClient(value = "provider-one",fallback=HelloServiceFallBach.class)

public interface HelloService {

@GetMapping("hello")
public Map<String,Object> h();

}

5:控制器

@RestController
public class HelloController {

@Autowired
private HelloService service;

@RequestMapping("jj")
public Map<String,Object> hello(){
return service.h();
}

}

6:错误返回控制器:

@Component
public class HelloServiceFallBach implements HelloService{

@Override
public Map<String, Object> h() {

Map<String, Object> map = new HashMap<String, Object>();
map.put("info", "错误");
return map;
}
}

@Component别忘了加

 

 
 
posted on 2018-09-28 00:03  聂鑫磊  阅读(197)  评论(0编辑  收藏  举报