引入父类的依赖

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">

4.0.0

com.parent

springdloud-parent

pom

1.0-SNAPSHOT

hystrix-service

org.springframework.boot

spring-boot-starter-parent

2.1.6.RELEASE

org.springframework.cloud

spring-cloud-dependencies

Greenwich.RELEASE

pom

import

org.springframework.cloud

spring-cloud-starter-netflix-eureka-server

2.1.6.RELEASE

org.springframework.cloud

spring-cloud-starter-netflix-hystrix

2.2.9.RELEASE

org.springframework.boot

spring-boot-starter-web

2.5.4

引入子类依赖

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">

springdloud-parent

com.parent

1.0-SNAPSHOT

4.0.0

com.hystrix

hystrix-service

org.springframework.cloud

spring-cloud-starter-netflix-hystrix

org.springframework.boot

spring-boot-starter-web

 

 

 

引入完整代码

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

显示如下

 

这就代表熔断了 

 

相关阅读

评论可见,请评论后查看内容,谢谢!!!评论后请刷新页面。