[Spring cloud 一步步实现广告系统] 19. 监控Hystrix Dashboard

简介: 在之前的18次文章中,我们实现了广告系统的`广告投放`,`广告检索`业务功能,中间使用到了 `服务发现Eureka`,`服务调用Feign`,`网关路由Zuul`以及`错误熔断Hystrix`等`Spring Cloud`组件。 简单调用关系:

在之前的18次文章中,我们实现了广告系统的广告投放广告检索业务功能,中间使用到了 服务发现Eureka服务调用Feign,网关路由Zuul以及错误熔断HystrixSpring Cloud组件。
简单调用关系:
UTOOLS1565828973486.png

但是系统往往都会报错,我们之前定义了一些容错类和方法,但是只是在控制台可以看到错误信息,我们想要统计一些数据,怎么才能更直观的看到我们的服务调用情况呢,接下来,和大家讨论一个新的熔断监控组件Hystrix Dashboard,顾名思义,从名字上我们就能看出来,它是监控的图形化界面。

Hystrix 在服务中的使用

结合openfeign使用

在我们实际的项目当中,使用的最多的就是结合FeignClient#fallbackHystrix一起来实现熔断,我们看一下我们在mscx-ad-feign-sdk中的实现。

@FeignClient(value = "mscx-ad-sponsor", fallback = SponsorClientHystrix.class)
public interface ISponsorFeignClient {
    @RequestMapping(value = "/ad-sponsor/plan/get", method = RequestMethod.POST)
    CommonResponse<List<AdPlanVO>> getAdPlansUseFeign(@RequestBody AdPlanGetRequestVO requestVO);

    @RequestMapping(value = "/ad-sponsor/user/get", method = RequestMethod.GET)
    /**
     * Feign 埋坑之 如果是Get请求,必须在所有参数前添加{@link RequestParam},不能使用{@link Param}
     * 会被自动转发为POST请求。
     */
    CommonResponse getUsers(@RequestParam(value = "username") String username);
}

在上述代码中,我们自定义了一个feignclient,并且给了这个client一个fallback的实现类:

@Component
public class SponsorClientHystrix implements ISponsorFeignClient {
    @Override
    public CommonResponse<List<AdPlanVO>> getAdPlansUseFeign(AdPlanGetRequestVO requestVO) {
        return new CommonResponse<>(-1, "mscx-ad-sponsor feign & hystrix get plan error.");
    }

    @Override
    public CommonResponse getUsers(String username) {
        return new CommonResponse<>(-1, "mscx-ad-sponsor feign & hystrix get user error.");
    }
}

这个fallback类实现了我们自定义的ISponsorFeignClient,那是因为fallback的方法必须和原始执行类的方法签名保持一致,这样在执行失败的时候,可以通过反射映射到响应的降级方法/容错方法。
mscx-ad-search服务中,我们通过注入ISponsorFeignClient来调用我们的mscz-ad-sponsor服务。

@RestController
@Slf4j
@RequestMapping(path = "/search-feign")
public class SearchFeignController {

    /**
     * 注入我们自定义的FeignClient
     */
    private final ISponsorFeignClient sponsorFeignClient;
    @Autowired
    public SearchFeignController(ISponsorFeignClient sponsorFeignClient) {
        this.sponsorFeignClient = sponsorFeignClient;
    }

    @GetMapping(path = "/user/get")
    public CommonResponse getUsers(@Param(value = "username") String username) {
        log.info("ad-search::getUsersFeign -> {}", JSON.toJSONString(username));
        CommonResponse commonResponse = sponsorFeignClient.getUsers(username);
        return commonResponse;
    }
}
使用HystrixCommand

其实Hystrix本身提供了一种直接在方法中应用的方式,就是使用@ com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand,我们看一下这个类的源码:

@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Inherited
@Documented
public @interface HystrixCommand {
    ...

        /**
     * Specifies a method to process fallback logic.
     * A fallback method should be defined in the same class where is HystrixCommand.
     * Also a fallback method should have same signature to a method which was invoked as hystrix command.
     * for example:
     * <code>
     *      @HystrixCommand(fallbackMethod = "getByIdFallback")
     *      public String getById(String id) {...}
     *
     *      private String getByIdFallback(String id) {...}
     * </code>
     * Also a fallback method can be annotated with {@link HystrixCommand}
     * <p/>
     * default => see {@link com.netflix.hystrix.contrib.javanica.command.GenericCommand#getFallback()}
     *
     * @return method name
     */
    String fallbackMethod() default "";

    ...
}

我们主要关注2个点:

  1. @Target({ElementType.METHOD})表明当前的注解只能应用在方法上面。
  2. 可直接定义fallbackMethod来保证容错。这个方法有一个缺陷,就是必须和执行方法在同一个类文件中,这就会造成我们的方法在实现的时候,显得特别的冗余和不够优雅。

以我们的mscx-ad-search中的广告查询为例:

@Service
@Slf4j
public class SearchImpl implements ISearch {

    /**
     * 查询广告容错方法
     *
     * @param e 第二个参数可以不指定,如果需要跟踪错误,就指定上
     * @return 返回一个空map 对象
     */
    public SearchResponse fetchAdsFallback(SearchRequest request, Throwable e) {

        System.out.println("查询广告失败,进入容错降级 : %s" + e.getMessage());
        return new SearchResponse().builder().adSlotRelationAds(Collections.emptyMap()).build();
    }

    @HystrixCommand(fallbackMethod = "fetchAdsFallback")
    @Override
    public SearchResponse fetchAds(SearchRequest request) {
        ...
    }
}

在我们请求出错的时候,会转到我们的fallback方法,这个实现是通过在应用启动的时候,我们开始了@EnableCircuitBreaker注解,这个注解会通过AOP拦截所有的HystrixCommand方法,将HystrixCommand整合到springboot的容器中,并且将注解标注的方法放入hystrix的线程中,一旦失败,通过反射调用fallback方法来实现。

创建dashboard project

上述代码我们看了Hystrix实现熔断的2种方式,接下来我们来实现请求监控的图形化界面,创建mscx-ad-dashboard,Let's code.
依然遵从我们springboot项目的三部曲:

  1. 加依赖

    <dependencies>
     <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-starter-hystrix</artifactId>
      <version>1.2.7.RELEASE</version>
     </dependency>
     <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-starter-hystrix-dashboard</artifactId>
      <version>1.2.7.RELEASE</version>
     </dependency>
     <!--eureka client-->
     <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
     </dependency>
     <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-actuator</artifactId>
     </dependency>
     </dependencies>
  2. 加注解

    /**
    * AdDashboardApplication for Hystrix Dashboard 启动类
    *
    * @author <a href="mailto:magicianisaac@gmail.com">Isaac.Zhang | 若初</a>
    * @since 2019/8/15
    */
     @SpringBootApplication
     @EnableDiscoveryClient
     @EnableHystrixDashboard
     public class AdDashboardApplication {
    
     public static void main(String[] args) {
         SpringApplication.run(AdDashboardApplication.class, args);
     }
     }
  3. 改配置

     server:
     port: 1234
     spring:
     application:
         name: mscx-ad-dashboard
     eureka:
     client:
         service-url:
         defaultZone: http://server1:7777/eureka/,http://server2:8888/eureka/,http://server3:9999/eureka/
     management:
     endpoints:
         web:
         exposure:
             include: "*"`

直接启动,可以看到如下页面:
UTOOLS1565833851940.png

添加要监控的服务地址:
UTOOLS1565833920702.png

目录
相关文章
|
1月前
|
负载均衡 Java API
Spring Cloud 面试题及答案整理,最新面试题
Spring Cloud 面试题及答案整理,最新面试题
133 1
|
1月前
|
Java Nacos Sentinel
Spring Cloud Alibaba 面试题及答案整理,最新面试题
Spring Cloud Alibaba 面试题及答案整理,最新面试题
197 0
|
1月前
|
SpringCloudAlibaba Java 持续交付
【构建一套Spring Cloud项目的大概步骤】&【Springcloud Alibaba微服务分布式架构学习资料】
【构建一套Spring Cloud项目的大概步骤】&【Springcloud Alibaba微服务分布式架构学习资料】
141 0
|
1月前
|
SpringCloudAlibaba Java 网络架构
【Springcloud Alibaba微服务分布式架构 | Spring Cloud】之学习笔记(七)Spring Cloud Gateway服务网关
【Springcloud Alibaba微服务分布式架构 | Spring Cloud】之学习笔记(七)Spring Cloud Gateway服务网关
90 0
|
6天前
|
负载均衡 Java 开发者
细解微服务架构实践:如何使用Spring Cloud进行Java微服务治理
【4月更文挑战第17天】Spring Cloud是Java微服务治理的首选框架,整合了Eureka(服务发现)、Ribbon(客户端负载均衡)、Hystrix(熔断器)、Zuul(API网关)和Config Server(配置中心)。通过Eureka实现服务注册与发现,Ribbon提供负载均衡,Hystrix实现熔断保护,Zuul作为API网关,Config Server集中管理配置。理解并运用Spring Cloud进行微服务治理是现代Java开发者的关键技能。
|
6天前
|
Java API 对象存储
对象存储OSS产品常见问题之使用Spring Cloud Alibaba情况下文档添加水印如何解决
对象存储OSS是基于互联网的数据存储服务模式,让用户可以安全、可靠地存储大量非结构化数据,如图片、音频、视频、文档等任意类型文件,并通过简单的基于HTTP/HTTPS协议的RESTful API接口进行访问和管理。本帖梳理了用户在实际使用中可能遇到的各种常见问题,涵盖了基础操作、性能优化、安全设置、费用管理、数据备份与恢复、跨区域同步、API接口调用等多个方面。
23 2
|
21天前
|
负载均衡 网络协议 Java
构建高效可扩展的微服务架构:利用Spring Cloud实现服务发现与负载均衡
本文将探讨如何利用Spring Cloud技术实现微服务架构中的服务发现与负载均衡,通过注册中心来管理服务的注册与发现,并通过负载均衡策略实现请求的分发,从而构建高效可扩展的微服务系统。
|
21天前
|
开发框架 负载均衡 Java
Spring boot与Spring cloud之间的关系
总之,Spring Boot和Spring Cloud之间的关系是一种构建和扩展的关系,Spring Boot提供了基础,而Spring Cloud在此基础上提供了分布式系统和微服务架构所需的扩展和工具。
17 4
Spring boot与Spring cloud之间的关系
|
22天前
Springcloud-ribbon和hystrix配置
Springcloud-ribbon和hystrix配置
7 0
|
25天前
|
Prometheus 监控 Cloud Native
Spring Boot 应用可视化监控
Spring Boot 应用可视化监控
15 0