Spring Cloud(Hystrix)使用

    xiaoxiao2022-07-07  183

    熔断器:

    1.创建项目

    2.在pom.xml中加入依赖:

    <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-hystrix</artifactId> </dependency>

    3.配置yml文件:

    server: port: 8030 eureka: instance: #定义Eureka实例 hostname: localhost #Eureka实例所在的主机名 prefer-ip-address: true client: service-url: defaultZone: http://localhost:8761/eureka/ spring: application: name: microservice-user-hystrix #名字

    4.在启动类添加注解开启熔断器功能:@EnableCircuitBreaker

    package com.itheima.microserviceuserhystrix; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.cloud.client.loadbalancer.LoadBalanced; import org.springframework.context.annotation.Bean; import org.springframework.web.client.RestTemplate; @SpringBootApplication @EnableDiscoveryClient @EnableCircuitBreaker public class MicroserviceUserHystrixApplication { public static void main(String[] args) { SpringApplication.run(MicroserviceUserHystrixApplication.class, args); } @Bean @LoadBalanced public RestTemplate restTemplate() { return new RestTemplate(); } }

    5.在控制器类添加注解@@HystrixCommand(fallbackMethod = "fallbackinfo")指定回调函数,并添加回调函数:

    package com.itheima.microserviceuserhystrix.controller; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.client.RestTemplate; import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand; @RestController public class UserController { @Autowired private RestTemplate restTemplate; //根据用户查找订单 @GetMapping("/findOrderByUser/{id}") @HystrixCommand(fallbackMethod = "fallbackinfo") public String findOrderByUser(@PathVariable String id) { //假设用户只有一个订单,订单id为123 int oid = 123; return this.restTemplate.getForObject("http://microservice-order/1", String.class); } public String fallbackinfo(@PathVariable String id) { return "服务不可用,请稍后再试!"; } }

    6.启动eureka,order(7900),order(7901),user-hystrix,浏览器访问:

    成功显示数据

    失败显示回调内容

    Hystrix Dashboard使用:

    1.在pom.xml加入依赖:

    <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency>

    2.6.启动eureka,order(7900),order(7901),user-hystrix,浏览器访问:

    http://localhost:8030/findOrderByUser/1

    http://localhost:8030/hystrix.stream

    在未访问第一个url之前返回都是ping,访问后返回数据:

    虽然可以实时监控数据,但是可读性太差,Hystrix Dashboard是Hystrix的一个组件,Hystrix Dashboard提供了数据监控和友好的图形化界面。

    1.建立工程microservice-hystrix-dashboard

    2.pom.xml增加依赖

    <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-hystrix-dashboard</artifactId> </dependency>

    3.修改yml文件:

    server: port: 8031 spring: application: name: microservice-hystrix-dashboard #名字

    4.启动类添加注解:@@EnableHystrixDashboard

    package com.itheima.microservicehystrixdashboard; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard; @SpringBootApplication @EnableHystrixDashboard public class MicroserviceHystrixDashboardApplication { public static void main(String[] args) { SpringApplication.run(MicroserviceHystrixDashboardApplication.class, args); } }

    5.启动,并访问:http://localhost:8031/hystrix.stream

    6.增加监控

    点击Monitor Stream:

    最新回复(0)