1, Hystrix clusters and monitor the turbine
2, integration of Feign, Hystrix service degradation service fuse completely decoupled
3, after a cluster timeout settings
Hystrix clusters and monitor the turbine
Dashboard demonstration in front of just a stand-alone service monitoring, real projects are basically clusters, so here is a cluster monitoring turbine.
turbine is based on the Dashboard.
Engage in a triple microservice-student-provider-hystrix
A copy of the code and configuration, and then modify several places;
1, yml configuration
--- server: port: 1004 context-path: / spring: datasource: type: com.alibaba.druid.pool.DruidDataSource driver-class-name: com.mysql.jdbc.Driver url: jdbc:mysql://localhost:3306/mybatis_ssm?useUnicode=true&characterEncoding=utf8 username: mybatis_ssm password: 123 jpa: hibernate: ddl-auto: update show-sql: true application: name: microservice-student profiles: provider-hystrix-1004 eureka: instance: hostname: localhost appname: microservice-student instance-id: microservice-student:1004 prefer-ip-address: true client: service-url: defaultZone: http://eureka2001.psy.com:2001/eureka/,http://eureka2002.psy.com:2002/eureka/,http://eureka2003.psy.com:2003/eureka/ info: groupId: com.psy.springcloud artifactId: microservice-student-provider-hystrix-1004 version: 1.0-SNAPSHOT userName: http://psy.com phone: 123456 hystrix: command: default: execution: isolation: thread: timeoutInMilliseconds: 1500 --- server: port: 1005 context-path: / spring: datasource: type: com.alibaba.druid.pool.DruidDataSource driver-class-name: com.mysql.jdbc.Driver url: jdbc:mysql://localhost:3306/mybatis_ssm?useUnicode=true&characterEncoding=utf8 username: mybatis_ssm password: 123 jpa: hibernate: ddl-auto: update show-sql: true application: name: microservice-student profiles: provider-hystrix-1005 eureka: instance: hostname: localhost appname: microservice-student instance-id: microservice-student:1005 prefer-ip-address: true client: service-url: defaultZone: http://eureka2001.psy.com:2001/eureka/,http://eureka2002.psy.com:2002/eureka/,http://eureka2003.psy.com:2003/eureka/ info: groupId: com.psy.springcloud artifactId: microservice-student-provider-hystrix-1005 version: 1.0-SNAPSHOT userName: http://psy.com phone: 123456 hystrix: command: default: execution: isolation: thread: timeoutInMilliseconds: 1500 --- server: port: 1006 context-path: / spring: datasource: type: com.alibaba.druid.pool.DruidDataSource driver-class-name: com.mysql.jdbc.Driver url: jdbc:mysql://localhost:3306/mybatis_ssm?useUnicode=true&characterEncoding=utf8 username: mybatis_ssm password: 123 jpa: hibernate: ddl-auto: update show-sql: true application: name: microservice-student profiles: provider-hystrix-1006 eureka: instance: hostname: localhost appname: microservice-student instance-id: microservice-student:1006 prefer-ip-address: true client: service-url: defaultZone: http://eureka2001.psy.com:2001/eureka/,http://eureka2002.psy.com:2002/eureka/,http://eureka2003.psy.com:2003/eureka/ info: groupId: com.psy.springcloud artifactId: microservice-student-provider-hystrix-1006 version: 1.0-SNAPSHOT userName: http://psy.com phone: 123456 hystrix: command: default: execution: isolation: thread: timeoutInMilliseconds: 1500
Start configuration class:
package com.psy.microservicestudentproviderhystrix; 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.psy.*.*", "cpm.psy.microservicecommon.entity"}) @EnableEurekaClient @SpringBootApplication public class MicroserviceStudentProviderHystrixApplication { public static void main(String[] args) { SpringApplication.run(MicroserviceStudentProviderHystrixApplication.class, args); } }
In this case there hystrix cluster services;
3, our new project microservice-student-consumer-hystrix-turbine-91
Plus lower dependence pom.xml
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-turbine</artifactId> </dependency>
application.yml
Server: Port: 91 context -path: / Eureka: Client: Service - url: defaultzone: HTTP: // eureka2001.psy.com:2001/eureka/, http://eureka2002.psy.com : 2002 / Eureka /, http://eureka2003.psy.com : 2003 / Eureka / Turbine: App -config: microservice- Student # designated to monitor the application name clusterNameExpression: " 'default' " # represents the name of the cluster to default the Spring: the application: name: turbine
Start categories:
@SpringBootApplication(exclude={DataSourceAutoConfiguration.class, HibernateJpaAutoConfiguration.class}) @EnableTurbine
Feign , Hystrix integration
The previous code, with @HystrixCommand fallbackMethod is very good, because the business code and the coupling is too high, is not conducive to maintenance, it is necessary to decouple, but we're talking about Feign Hystrix integration.
1, microservice-student-provider-hystrix project modifications
We do not have the set of the original. According to the normal logic to write;
StudentService plus a new interface method:
/ * * * Test Hystrix service degradation * @return * / public the Map <String, Object> hystrix ();
StudentServiceImpl write the specific implementation:
@Override public Map<String, Object> hystrix() { Map<String,Object> map=new HashMap<String,Object>(); map.put("code", 200); map.put("info","工号【"+port+"】正在为您服务"); return map; }
StudentProviderController normal call service method:
/** * 测试Hystrix服务降级 * @return * @throws InterruptedException */ @ResponseBody @GetMapping(value="/hystrix") @HystrixCommand(fallbackMethod="hystrixFallback") public Map<String,Object> hystrix() throws InterruptedException{ // Thread.sleep(2000); // Map<String,Object> map=new HashMap<String,Object>(); // map.put("code", 200); // map.put("info","工号【"+port+"】正在为您服务"); return studentService.hystrix(); }
2, microservice-common project New FallbackFactory class, decoupled service degradation service fuse
StudentClientService interface getInfo new method;
/ * * * Downgraded service fuse * @return * / @GetMapping (value = " / Student / Hystrix " ) public the Map <String, Object> Hystrix ();
StudentClientFallbackFactory new class that implements FallbackFactory <StudentClientService> Interface;
package cpm.psy.microservicecommon.service; import cpm.psy.microservicecommon.entity.Student; import feign.hystrix.FallbackFactory; import org.springframework.stereotype.Component; import java.util.HashMap; import java.util.List; import java.util.Map; @Component public class StudentClientFallbackFactory implements FallbackFactory<StudentClientService> { @Override public StudentClientService create(Throwable cause) { return new StudentClientService() { @Override public boolean save(Student student) { return false; } @Override public List<Student> list() { return null; } @Override public Map<String, Object> hystrix() { Map<String,Object> map=new HashMap<String,Object>(); map.put("code", 500); map.put("info", "系统繁忙,稍后重试"); return map; } @Override public Student get(Integer id) { return null; } @Override public boolean delete(Integer id) { return false; } @Override public String ribbon() { return null; } }; } }
StudentClientService interface @FeignClient notes plus next fallbackFactory property
@FeignClient(value="MICROSERVICE-STUDENT,fallbackFactory=StudentClientFallbackFactory.class")
We achieve such a downgrade method to achieve;
3, microservice-student-consumer-feign-80 modified to support Hystrix
StudentConsumerFeignController new method calls
/** * Feign整合Hystrix服务熔断降级 * @return * @throws InterruptedException */ @GetMapping(value="/hystrix") public Map<String,Object> hystrix() throws InterruptedException{ return studentClientService.hystrix(); }
4, microservice-common support of application.yml plus hystrix
feign: hystrix: enabled: true
测试开启三个eureka,以及带hystrix的provider,和带feign,hystrix的consummer。
测试的话,也是没问题的。0.9秒的话,返回正常信息;超过1秒的话,就返回错误提示;
集群后超时设置
上面错误是什么原因呢,咱们明明在Hystrix中的application.yml中设置了
hystrix: command: default: execution: isolation: thread: timeoutInMilliseconds: 1500
这里是因为还有一个feign 也有一个超时时间的设置,当然feign底层是 ribbon的封装,所以直接配置ribbon,ribbon默认超时也是1秒。
所以这里都是强制要求,ribbon的超时时间要大于hystrix的超时时间,否则 hystrix自定义的超时时间毫无意义。
所以还得microservice-student-consumer-feign-80上加个
ribbon超时时间设置
ribbon: ReadTimeout: 10000 ConnectTimeout: 9000
这样就完工了,可以自行测试。