first step:
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-client</artifactId> </dependency>
Step two:
@EnableCircuitBreaker
third step:
package com.example.order.controller; import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties; import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.client.RestTemplate; /** * @Title: HystrixController * @ProjectName order * @date 2019/12/1911:34 */ @RestController @DefaultProperties(defaultFallback = "defaultFallback") // 默认调用的方法名 public class HystrixController { // 超时配置 // @HystrixCommand (commandProperties @HystrixProperty = {(name = "execution.isolation.thread.timeoutInMilliseconds", value = "3000")}) @GetMapping ( "/ getProductInfoList") public String getProductInfoList () { RestTemplate RestTemplate = new new RestTemplate (); return restTemplate.postForObject ( "http://127.0.0.1:8080/product/list", null, String.class); // a RuntimeException the throw new new ( "transmission anomaly"); } / ** * default method names, 8080 if the service fails, it will call * @return * / Private String defaultFallback () { return "default prompt: too crowded, please try again later ~"; } }
yml configuration (configuration does not require a separate)
Hystrix: Command: # Set the timeout default: Execution: Isolation: Thread: timeoutInMilliseconds: 1000 getProductInfoList: # Name Method added separately to the timeout Execution: Isolation: Thread: timeoutInMilliseconds: 3000