引入父类的依赖
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">
引入子类依赖
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">
引入完整代码
package com.hystrix;
import org.springframework.boot.SpringApplication;
import org.springframework.cloud.client.SpringCloudApplication;
@SpringCloudApplication //这个注解包含了多个注解
public class HystrixApplication {
public static void main(String[] args) {
SpringApplication.run(HystrixApplication.class,args);
}
}
package com.hystrix.controller.front;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HystrixController {
@GetMapping("/hystrixTest/{id}")
@HystrixCommand(
commandProperties = {
@HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds",value = "3000")
},
fallbackMethod = "returnDefaultResult"
)
public String hystrixTest(@PathVariable String id){
try {
Thread.sleep(4000);
} catch (InterruptedException e) {
e.printStackTrace();
}
return "返回值:"+id;
}
public String returnDefaultResult(@PathVariable String id){
return "恭喜您调用的服务去火星了,"+id+"找不到了";
}
}
本地熔断器超时设置的3秒钟,程序设置运行的是4秒钟
浏览器输入http://127.0.0.1:8080/hystrixTest/1992
显示如下
这就代表熔断了
相关阅读
发表评论