SpringCloud微服务 之hystrix(二)
前言
上一小节为我们从概念上认识了Hystrix,本小节我们将通过实例来学习一下Hystrix的熔断机制以及Hystrux的断路器。
案例
-
Eureka Server端编写(参考前例)
-
Eureka Client端服务提供方编写。
-
项目结构
-
CoreCode
@SpringBootApplication @EnableDiscoveryClient @EnableCircuitBreaker //开启Hystrix熔断机制 public class MicroserviceDealBrokerHystrixApplication { @Bean @LoadBalanced /** * 添加 @LoadBalanced 使该RestTemplate具备Ribbon均衡负载算法功能 * @return */ public RestTemplate restTemplate() { return new RestTemplate(); } public static void main(String[] args) { SpringApplication.run(MicroserviceDealBrokerHystrixApplication.class, args); } }
/** * SpringCould 2.0+需要为HystrixMricsStreamServlet手动注入到ServletRegistrationBean中 * @author Administrator * */ @Component public class HystrtixStreamServlet { @Bean public ServletRegistrationBean<Servlet> getSerlvet(){ HystrixMetricsStreamServlet streamServlet = new HystrixMetricsStreamServlet(); ServletRegistrationBean<Servlet> registrationBean = new ServletRegistrationBean<>(streamServlet); registrationBean.setLoadOnStartup(1); registrationBean.addUrlMappings("/hystrix.stream"); //registrationBean.addUrlMappings("/hystrix"); registrationBean.setName("HystrixMricsStreamServlet"); return registrationBean; } }
@RestController public class BrokerController { @Autowired private RestTemplate restTemplate; @Value("${server.port}") private String port; // 注入普通字符串 @GetMapping("/deal/{id}") @HystrixCommand(fallbackMethod = "findByIdFallBack") public Deal findById(@PathVariable Long id) { return this.restTemplate.getForObject("http://microservice-deal-cloud/deal/" + id, Deal.class); } /** * findById fallBack方法 * 主方法与fallback方法应保持一致(入参、返回值) * @param id * @return */ public Deal findByIdFallBack(Long id){ Deal deal = new Deal(); deal.setId(id.intValue()); deal.setName("Dustyone"); deal.setPort(port); return deal; } }
server: port: 8082 spring: application: name: microservice-deal-broker-cloud-hystrix eureka: client: serviceUrl: defaultZone: http://localhost:8080/eureka/ instance: prefer-ip-address: true #开放所有的监控节点 management: endpoints: web: exposure: include: '*' #设置feign的hystrix响应超时时间(必须) hystrix: command: default: execution: isolation: thread: timeoutInMilliseconds: 5000
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <artifactId>microservice-deal-broker-cloud-hystrix</artifactId> <packaging>jar</packaging> <name>microservice-deal-broker-cloud-hystrix</name> <description>Demo project for Spring Boot</description> <parent> <groupId>com.example</groupId> <artifactId>microservice-deal-parent</artifactId> <version>0.0.1-SNAPSHOT</version> </parent> <dependencies> <!-- Eureka CLient核心组件 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <!-- Ribbon核心组件 可声明此依赖也可以不生命,因为Eureka Client已依赖了Ribbon --> <!-- <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-ribbon</artifactId> </dependency> --> <!-- pringCloud Hystrix依赖 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-hystrix</artifactId> </dependency> </dependencies> </project>
-
-
Eureka Client端服务提消费编写(单实例) (参考前例)。
-
访问:http://localhost:8082/deal/2
如上图所示,服务消费者和服务提供者正常时我们能通过消费者请求到提供者。 -
现在我们把服务提供者手动shutdown掉,
-
然后我们访问:http://localhost:8082/hystrix.stream
此时我们可以看到Hystrix 会一直再ping 降级服务(即服务提供者),这就是所谓的断路器半开状态,一旦降级服务宕机,服务消费者就会一直去尝试访问降级服务。 -
然后我们重新将降级服务启动:
此时所有服务又都正常了,能正常访问了,Hystrix 断路器关闭。
小结
-
要使用Hystrix功能先加入Hystrix依赖:spring-cloud-starter-netflix-hystrix(V 2.0+)然后再主类上开起Hystrix熔断机制:@EnableCircuitBreaker
-
对指定的方法添加FallBack方法:@HystrixCommand(fallbackMethod = “findByIdFallBack”),需要注意的是:主方法与fallback方法应保持一致(入参、返回值)。
-
对于SpringCloud V2.0+ 当我们使用Hystrix.stream来对查看断路器监控信息是需要自定义一个HystrixMetricsStreamServlet,并将该servlet加入到ServletRegistrationBean中。并尝试着开放所有的监控节点(建议),当然如果你想对一些特定监控接口要求授权之后才能访问,可以自定义监控端点。
-
本小节用的案例:microservice-deal-eureka、microservice-deal-cloud、microservice-deal-broker-cloud-hystrix