Services avalanche effect
When a plurality of service requests dependent on:
Access under normal circumstances
However, it can not access, exception, overtime and other issues that appear when the requested service (I figure), then the user's request will be blocked.
If multiple users request, there can not access the service, then they will be plunged into a state of obstruction
Hystrix introduced, can solve this problem by fusing the service and service degradation.
Services Service downgraded fuse
Hystrix Profile breaker
hystrix corresponding Chinese name is "porcupine" porcupine whole body covered with thorns, to protect themselves from predators damage, represents a defense mechanism, which coincides with the hystrix own function, so the team Netflix framework named Hystrix, and the use of cartoon characters corresponding to do as a logo.
In a distributed system, many rely inevitably call fails, such as overtime, abnormal, how can we ensure that in the case of a dependency problem, and will not result in an overall service to fail, this is the Hystrix needs to be done. Hystrix provides a fuse, isolation, Fallback, cache, monitoring and other functions, to ensure that the system is still available at the time of issue one or more dependent simultaneously.
Hystrix Services blown service degradation @HystrixCommand fallbackMethod
A micro-fuse mechanism is to deal with service link protection mechanism avalanche effect.
When a service is unavailable or response time expires, the service will be downgraded, thus fusing the node service calls, to quickly return the error affect the page customized information.
We write a program to test the next;
We write a new fuse with the service provider's service project microservice-student-provider-hystrix- 1004
The configuration and code to copy this project;
Then modify;
1, pom.xml plus next hystrix:
<!--Hystrix相关依赖--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-hystrix</artifactId> </dependency>
Under 2.application.yml modify the port and instance name
server: port: 1004 servlet: context-path: / spring: datasource: type: com.alibaba.druid.pool.DruidDataSource driver-class-name: com.mysql.jdbc.Driver url: jdbc:mysql://localhost:3306/t224?useUnicode=true&characterEncoding=utf8 username: root password: 123 jpa: hibernate: ddl-auto: update show-sql: true application: name: microservice-student eureka: instance: hostname: localhost appname: microservice-student instance-id: microservice-student:1004 prefer-ip-address: true client: service-url: defaultZone: http://eureka2001.cjh.com:2001/eureka/,http://eureka2002.cjh.com:2002/eureka/,http://eureka2003.cjh.com:2003/eureka/ hystrix: command: default: execution: isolation: thread: timeoutInMilliseconds: 3000 info: groupId: com.javaxl.testSpringcloud artifactId: microservice-student-provider-hystrix-1004 version: 1.0-SNAPSHOT userName: http://javaxl.com phone: 123456
3, start the next class StudentProviderHystrixApplication_1004 add annotation support @EnableCircuitBreaker
package com.cjh.microservicestudentproviderhystrix1004; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.domain.EntityScan; import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; @EnableCircuitBreaker @EntityScan("com.cjh.*.*") @EnableEurekaClient @SpringBootApplication public class MicroserviceStudentProviderHystrix1004Application { public static void main(String[] args) { SpringApplication.run(MicroserviceStudentProviderHystrix1004Application.class, args); } }
4, service providers new 1004 controller
package com.cjh.microservicestudentproviderhystrix1004.controller; import com.cjh.microservicecommon.entity.Student; import com.cjh.microservicestudentproviderhystrix1004.service.StudentService; import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.web.bind.annotation.*; import java.util.HashMap; import java.util.List; import java.util.Map; @RestController @RequestMapping("/student") public class StudentProviderController { @Autowired private StudentService studentService; @Value("${server.port}") private String port; @PostMapping(value="/save") public boolean save(Student student){ try{ studentService.save(student); return true; }catch(Exception e){ return false; } } @GetMapping(value="/list") public List<Student> list(){ return studentService.list(); } @GetMapping(value="/get/{id}") public Student get(@PathVariable("id") Integer id){ return studentService.findById(id); } @GetMapping(value="/delete/{id}") public boolean delete(@PathVariable("id") Integer id){ try{ studentService.delete (ID); return to true ; } the catch (Exception E) { return to false ; } } @RequestMapping ( " / Ribbon " ) public String Ribbon () { return " job number [ " + Port + " ] is for you " ; } / * * * test Hystrix service degradation * @return * @throws InterruptedException * / @ResponseBody @GetMapping (value = "/hystrix") @HystrixCommand(fallbackMethod="hystrixFallback") public Map<String,Object> hystrix() throws InterruptedException{ Thread.sleep(1000); Map<String,Object> map=new HashMap<String,Object>(); map.put("code", 200); map.put("info","工号【"+port+"】正在为您服务"); return map; } public Map<String,Object> hystrixFallback() throws InterruptedException{ Map<String,Object> map=new HashMap<String,Object>(); map.put("code", 500); map.put("info", "系统【"+port+"】繁忙,稍后重试"); return map; } }
Here I return to the normal access to business data is 200 xxxxx
But we here Thread.sleep (2000) simulated timeout;
Here then we add @HystrixCommand comment and fallbackMethod
This way we show no abnormalities and no timeouts (hystrix default one second time out) situation, only to return to normal business data;
Otherwise, enter the local fallback method we specified, we are engaged in the 500 system error, try again later, effectively solve the avalanche effect, and returned to the user interface
Good error message;
============================
Add Method microservice-student-consumer-80 have corresponding item
/** * 测试Hystrix服务降级 * @return */ @GetMapping(value="/hystrix") @ResponseBody public Map<String,Object> hystrix(){ return restTemplate.getForObject(SERVER_IP_PORT+"/student/hystrix/", Map.class); }
5, then we have to test under
First start three eureka, then start with hystrix provider, and finally start the ordinary consumer;
Browser: HTTP: // localhost / Student / hystrix
return:
Since 1 Hystrix default time out, so that all 2 seconds to enter the sleep custom fallback method for preventing an avalanche service;
Here we change the sleep modified to 100 milliseconds;
Hystrix default timeout
Hystrix default timeout is 1 second, we can see through hystrix source,
Found HystrixCommandProperties class in packet hystrix-core.jar com.netflix.hystrix
default_executionTimeoutInMilliseconds attribute default timeout situation
1 second default 1000 ms
Our system if you want to customize the default settings of the time hystrix words;
application.yml profiles plus
hystrix: command: default: execution: isolation: thread: timeoutInMilliseconds: 3000
Then we changed the code 3 seconds to 2 seconds sleep modified test;
sleep modified to 4 seconds;
Hystrix Service Monitoring Dashboard
Hystrix Service Monitoring Dashboard Dashboard
Hystrix provides near real-time call monitoring service project Dashboard, real-time performance record request initiated by Hystrix,
It can be presented to the user to see through graphical form.
Our new projects: microservice-student-consumer-hystrix-dashboard-90
Plus dependence
<!--Hystrix服务监控Dashboard依赖--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-hystrix</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-hystrix-dashboard</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency>
application.yml Configuration
server: port: 90 servlet: servlet-path: /
New startup class: StudentConsumerDashBoardApplication_90
Annotated: @EnableHystrixDashboard
package com.cjh.microservicestudentconsumerhystrixdashboard90; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration; import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard; @EnableHystrixDashboard @SpringBootApplication(exclude={DataSourceAutoConfiguration.class, HibernateJpaAutoConfiguration.class}) public class MicroserviceStudentConsumerHystrixDashboard90Application { public static void main(String[] args) { SpringApplication.run(MicroserviceStudentConsumerHystrixDashboard90Application.class, args); } }
So get away.
We started this project;
Then enter the browser: HTTP: // localhost: 90 / hystrix
This appears to explain OK;
We then tested the next;
We started three eureka, then start microservice-student-provider-hystrix-1004
We request directly to http: // localhost: 1004 / student / hystrix
Return to normal business
We monitor words, http: // localhost: 1004 / hystrix.stream this path can be;
It has been ping, and return data transactions;
index: