hystrix两种熔断方式
一、Riboon方式
启动Eureka_server \ Eureka_client \ eureka-ribbon-client
主要是这一段代码:
public String hi(@RequestParam(required = false,defaultValue = "com/forezp") String name){
return ribbonService.hi(name);
}
Eureka_client 配置
server:
port: 8002
eureka:
instance:
hostname: 127.0.0.1
prefer-ip-address: true # eureka服务端的实力名称
client:
serviceUrl:
defaultZone: http://127.0.0.1:7001/eureka/
#defaultZone: http://peer1:7001/eureka/ # 集群版, 会自动向http://peer2:7002/eureka/ 注册
register-with-eureka: false
fetch-registry: false
spring:
application:
name: Eureka-client # 设置与Eureka Server交互的地址查询服务和注册服务都需要依赖这个地址(单机版)
#defaultZone: http://peer1.com:7001/eureka/,http://peer2.com:7002/eureka/ # 集群版
eureka-ribbon-client 里面
就是这样
@Service
public class RibbonService {
@Autowired
RestTemplate restTemplate;
@HystrixCommand(fallbackMethod = "hiError")
public String hi(String name) {
return restTemplate.getForObject("http://Eureka_client/hi?name="+name,String.class); }
public String hiError(String name) {
return "hi,"+name+",sorry,error!";
}
}
然后这样就去调用了。
http://127.0.0.1:8001/hi?name=dgx
http://127.0.0.1:8765/hi?name=dg
二、与Feign结合 方式
Eureka_feign_client 需要加入依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
同时:
/**
* Created by fangzhipeng on 2017/6/21.
*/
@FeignClient(value = "Eureka-client",configuration = FeignConfig.class,fallback = HiHystrix.class)
public interface EurekaClientFeign {
@GetMapping(value = "/hi")
String sayHiFromClientEureka(@RequestParam(value = "name") String name);
}
开启两个Eureka_client 8001 和8002
然后和上面一样访问。可以关掉Eureka-client 然后hytrix起作用。这个测试成功。
三、hytrix监控
eureka_feign_client 这个工程
访问地址http://127.0.0.1:8765/hystrix
代码:https://gitee.com/dgx555/springcloud/tree/master/springcloud_Eureka_Ribbon_feign_hystrix_zuul