Note: stopping for further maintenance
Note: stopping for further maintenance
Note: stopping for further maintenance
Note: stopping for further maintenance
Note: stopping for further maintenance
Note: stopping for further maintenance
Say important things six times
Distributed problems:
Applications in complex distributed architecture have multiple dependencies, and each dependency will inevitably fail at some time, resulting in service avalanche
Official website information:
https://github.com/Netflix/Hystrix/wiki/How-To-Use
There are three main concepts of Hystrix:
Service degradation:
Conditions leading to degradation
1> Abnormal program operation: such as null pointer error, etc
2> Timeout
3> Service fuse triggers service degradation
4> Full thread pool / semaphore will also cause service degradation
Service fuse
When the maximum amount of access is reached, it directly denies access, and then calls the service degradation method to give friendly hints.
After the maximum number of accesses is reached, the service is degraded - > fusing - > recovering the calling link
Service current limiting
Second kill and other high concurrency operations are strictly prohibited. Overcrowding is prohibited. There are N in a second. Queue up and enter in an orderly manner
Service degradation control code construction
pom.xml import
<!--hystrix--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-hystrix</artifactId> </dependency>
Controller code
@HystrixCommand(fallbackMethod = "gets_TimeOutHandler",commandProperties = { @HystrixProperty(name="execution.isolation.thread.timeoutInMilliseconds",value = "3000") }) @RequestMapping(value = "gets",method = RequestMethod.GET) public String gets(){ String result=null; try { TimeUnit.SECONDS.sleep(5); //todo business logic processing result= payService.getAll(); } catch (InterruptedException e) { e.printStackTrace(); } catch (Exception e) { return e.toString(); } return result; } public String gets_TimeOutHandler(){ return "request timeout"; }
If there is an error in the operation of the logic code here, it will also go to gets_TimeOutHandler, such as int=10/0 in logic code, will also report request timeout when accessing
Note: if the devtools code is used to control service degradation, it will take effect only after the service is restarted. If the interface invoked by the service is configured with service degradation, it needs to be consistent
Application startup class activates automatic degradation configuration
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker; @EnableCircuitBreaker
It should be noted here that each configuration of service degradation will cause code expansion and redundancy, that is, if 100 api interfaces are to be degraded, 100 corresponding degradation statements need to be written, so we need to deal with service degradation in a unified manner (the general and exclusive (the above example) are separated to avoid code expansion)
@DefaultProperties(defaultFallback = "gets_TimeOutHandler")
@HystrixCommand
public String gets_TimeOutHandler(){ return "request timeout"; }
Unified degradation processing of openFeign service call
! [insert picture description here]( https://img-blog.csdnimg.cn/1b0032df09e048ab821f2e2e9654303b.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5LiA5ZCN6JC96a2E55qE56iL5bqP5ZGY,size_18,color_FFFFFF,t_70,g_se,x_16) ! [insert picture description here]( https://img-blog.csdnimg.cn/81dd0c559b31433db67e42e830ee4a72.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5LiA5ZCN6JC96a2E55qE56iL5bqP5ZGY,size_18,color_FFFFFF,t_70,g_se,x_16)Service (recovery call link)
Fuse opening parameter controller
If openfeign is used (openfeign integrates Hystrix), it can be configured in properties
#feign enable hystrix feign.hystrix.enabled=true #Enable httpclient feign.httpclient.enabled=true feign.httpclient.max-connections: 1000 #Maximum concurrent connections per service feign.httpclient.max-connections-per-route: 500 #Request and response GZIP compression support feign.compression.request.enabled=true feign.compression.response.enabled=false hystrix.threadpool.default.coreSize=500 hystrix.threadpool.default.maxQueueSize=2000 hystrix.threadpool.default.queueSizeRejectionThreshold=1500 hystrix.command.default.execution.isolation.strategy=THREAD hystrix.command.default.circuitBreaker.requestVolumeThreshold=1500 hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds=180000 hystrix.command.default.execution.timeout.enabled=true #Sets the maximum number of threads for fallback hystrix.command.default.fallback.isolation.semaphore.maxConcurrentRequests=500
Hystrix graphical monitoring
It is recommended to create a new service for monitoring and processing, or integrate it into the same service (it may be messy)
pom.xml dependency
<!-- hystrix-dashboard Graphical tools --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId> </dependency>
Annotate the Application startup class @ EnableHystrixDashboard
import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard; @EnableHystrixDashboard
Start service ip: service port / hystrix
Add a dependent POM to the microservices that need to be monitored to improve the monitoring information xml
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency>
Add annotations on the Application startup class in the microservice to be monitored
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker; @EnableCircuitBreaker
And add a Bean
/** * This configuration is for service monitoring and has nothing to do with the service fault tolerance itself. It is an upgrade of spring cloud * ServletRegistrationBean Because the default path of springboot is not / hystrix stream * Just configure the servlet of the context in your own project * @return */ @Bean public ServletRegistrationBean getServlet(){ HystrixMetricsStreamServlet streamServlet=new HystrixMetricsStreamServlet(); ServletRegistrationBean registrationBean=new ServletRegistrationBean(streamServlet); registrationBean.setLoadOnStartup(1); registrationBean.addUrlMappings("/hystrix.stream"); registrationBean.setName("HystrixMetricsStreamServlet"); return registrationBean; }
The api interface in the controller performs service degradation processing (what I do here is unique degradation processing)
Start project
Graphical monitoring interface (if you need to monitor, you can write which ip: port number / hystrix.stream)