Hystrix basic use (Demo)

1. Add dependence


<dependency>
	<groupId>org.springframework.cloud</groupId>
	<artifactId>spring-cloud-netflix-hystrix</artifactId>
</dependency>
<dependency>
	<groupId>com.netflix.hystrix</groupId>
	<artifactId>hystrix-javanica</artifactId>
</dependency>

2. Modify the startup class

Consumer side, start the class to add @EnableCircuitBreakerannotations

3. The method of adding annotations

consumerMedium mannerControllerFloor

@RestController
public class MyController {
	@Autowired
	private HelloService hs;

	@RequestMapping("/test")
	//fallbackMethod的参数为失败时的调用方法名(两个方法的参数相同)
	@HystrixCommand(fallbackMethod = "fallback")
	public String test(String name) {
		return hs.hello(name);
	}
	//失败时调用此方法
	public String fallback(String name) {
		return "失败了--->" + name;
	}
}

consumerMedium mannerServiceFloor

@FeignClient("eureka-provider")
public interface HelloService {
	@RequestMapping("/hello")
	public String hello(@RequestParam String name);
}

providerMedium mannerControllerFloor

@RequestMapping("/hello")
public String hello(String name) {
	return "提供者1号--->你好," + name;
}

4. Test

4.1 startup registry, providers, consumers

Here Insert Picture Description

4.2 Consumer access

Here Insert Picture Description

4.3 Close providers

Here Insert Picture Description

4.4 Consumer access again

Page displays the return value of access failure
Here Insert Picture Description

4.5 If the return is not configured Hystrix

Here Insert Picture Description

Guess you like

Origin blog.csdn.net/qq_36226997/article/details/95327262