Example Analysis of Open-Feign Integration hystrix degraded fuse
This article mainly introduces the example analysis of Open-Feign integration hystrix downgrade fuse, the article is very detailed, has a certain reference value, interested friends must read it!
1. Server 1. Configuration file
Application.yml
Name of server: port: 9000spring: application: name: my-test2 # service 2, control layer @ RestControllerpublic class ShoppingController {@ RequestMapping ("/ myTestBuy2") public String myTestBuy2 () {/ / used to simulate service timeout try {Thread.sleep (6000);} catch (InterruptedException e) {e.printStackTrace () } return "purchased successfully-time:" + new SimpleDateFormat ("yyyy-MM-dd HH:mm:ss") .format (new Date ()) }} II. Client 1, dependent org.springframework.cloud spring-cloud-starter-openfeign org.springframework.cloud spring-cloud-starter-netflix-hystrix 2, Configuration file server: port: 8000spring: application: name: my-test1 # Service name # allows the service to downgrade the configuration feign: hystrix: enabled: true# Custom ribbon timeout setting is larger than the hystrix-timeoutInMilliseconds timeout ribbon: # refers to the time it takes to read from the server to available resources after establishing a connection. ReadTimeout: 10000 # refers to the time it takes to establish a connection. When the network condition is normal, the time it takes for both ends to connect and the timeout for processing requests is 5 seconds by default. ConnectTimeout: 10000hystrix: command: default: execution: isolation: thread: # feign Integration hystrix Optical Settings Hystrix timeout useless to cooperate with ribbon timeout timeoutInMilliseconds: 5000my-test2: url: http://127.0.0.1:90003, Startup class @ SpringBootApplication@EnableFeignClients// enables open-feign@EnableHystrix// to enable degraded circuit breaker service public class MyTestApplication1 {public static void main (String [] args) {SpringApplication.run (MyTestApplication1.class) Args) 4. Call @ RestControllerpublic class TestController1 {@ Autowired TestService1 testService1; @ RequestMapping ("/ myTestBuy1") public String myTestBuy2 () {return testService1.myTestBuy2 () in the control layer }} 5. Create a class to implement the service FeignClient interface @ Componentpublic class MyHystrix1 implements TestService1 {@ Override public String myTestBuy2 () {return "failed, and the service was fused-- time:" + new SimpleDateFormat ("yyyy-MM-dd HH:mm:ss") .format (new Date ()) }} 6. Configure the FallBack implementation class @ FeignClient (name = "my-test2", url = "${my-test2.url}", fallback = MyHystrix1.class) public interface TestService1 {@ RequestMapping ("/ myTestBuy2") String myTestBuy2 () on the service FeignClient interface.
2. Scenario 2 when the called service stops running
If you only give two seconds, the circuit breaker will be started automatically.
3. Scenario 3 when the calling service times out
Fuse time according to the time set by hystrix, what I set here is 5 seconds.
4. Other
The above is all the contents of this article "the example analysis of Open-Feign integrating hystrix downgrade circuit breaker". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!