日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > windows >内容正文

windows

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

發(fā)布時間:2025/6/17 windows 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 [Spring cloud 一步步实现广告系统] 19. 监控Hystrix Dashboard 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

在之前的18次文章中,我們實現(xiàn)了廣告系統(tǒng)的廣告投放,廣告檢索業(yè)務(wù)功能,中間使用到了 服務(wù)發(fā)現(xiàn)Eureka,服務(wù)調(diào)用Feign,網(wǎng)關(guān)路由Zuul以及錯誤熔斷Hystrix等Spring Cloud組件。
簡單調(diào)用關(guān)系:

但是系統(tǒng)往往都會報錯,我們之前定義了一些容錯類和方法,但是只是在控制臺可以看到錯誤信息,我們想要統(tǒng)計一些數(shù)據(jù),怎么才能更直觀的看到我們的服務(wù)調(diào)用情況呢,接下來,和大家討論一個新的熔斷監(jiān)控組件Hystrix Dashboard,顧名思義,從名字上我們就能看出來,它是監(jiān)控的圖形化界面。

Hystrix 在服務(wù)中的使用

結(jié)合openfeign使用

在我們實際的項目當中,使用的最多的就是結(jié)合FeignClient#fallback和Hystrix一起來實現(xiàn)熔斷,我們看一下我們在mscx-ad-feign-sdk中的實現(xiàn)。

@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請求,必須在所有參數(shù)前添加{@link RequestParam},不能使用{@link Param}* 會被自動轉(zhuǎn)發(fā)為POST請求。*/CommonResponse getUsers(@RequestParam(value = "username") String username); }

在上述代碼中,我們自定義了一個feignclient,并且給了這個client一個fallback的實現(xiàn)類:

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

這個fallback類實現(xiàn)了我們自定義的ISponsorFeignClient,那是因為fallback的方法必須和原始執(zhí)行類的方法簽名保持一致,這樣在執(zhí)行失敗的時候,可以通過反射映射到響應(yīng)的降級方法/容錯方法。
在mscx-ad-search服務(wù)中,我們通過注入ISponsorFeignClient來調(diào)用我們的mscz-ad-sponsor服務(wù)。

@RestController @Slf4j @RequestMapping(path = "/search-feign") public class SearchFeignController {/*** 注入我們自定義的FeignClient*/private final ISponsorFeignClient sponsorFeignClient;@Autowiredpublic 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本身提供了一種直接在方法中應(yīng)用的方式,就是使用@ 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 "";... }

我們主要關(guān)注2個點:

  • @Target({ElementType.METHOD})表明當前的注解只能應(yīng)用在方法上面。
  • 可直接定義fallbackMethod來保證容錯。這個方法有一個缺陷,就是必須和執(zhí)行方法在同一個類文件中,這就會造成我們的方法在實現(xiàn)的時候,顯得特別的冗余和不夠優(yōu)雅。
  • 以我們的mscx-ad-search中的廣告查詢?yōu)槔?#xff1a;

    @Service @Slf4j public class SearchImpl implements ISearch {/*** 查詢廣告容錯方法** @param e 第二個參數(shù)可以不指定,如果需要跟蹤錯誤,就指定上* @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")@Overridepublic SearchResponse fetchAds(SearchRequest request) {...} }

    在我們請求出錯的時候,會轉(zhuǎn)到我們的fallback方法,這個實現(xiàn)是通過在應(yīng)用啟動的時候,我們開始了@EnableCircuitBreaker注解,這個注解會通過AOP攔截所有的HystrixCommand方法,將HystrixCommand整合到springboot的容器中,并且將注解標注的方法放入hystrix的線程中,一旦失敗,通過反射調(diào)用fallback方法來實現(xiàn)。

    創(chuàng)建dashboard project

    上述代碼我們看了Hystrix實現(xiàn)熔斷的2種方式,接下來我們來實現(xiàn)請求監(jiān)控的圖形化界面,創(chuàng)建mscx-ad-dashboard,Let's code.
    依然遵從我們springboot項目的三部曲:

  • 加依賴

    <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>
  • 加注解

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

    server:port: 1234spring:application:name: mscx-ad-dashboardeureka:client:service-url:defaultZone: http://server1:7777/eureka/,http://server2:8888/eureka/,http://server3:9999/eureka/management:endpoints:web:exposure:include: "*"`
  • 直接啟動,可以看到如下頁面:

    添加要監(jiān)控的服務(wù)地址:

    轉(zhuǎn)載于:https://www.cnblogs.com/zhangpan1244/p/11361078.html

    總結(jié)

    以上是生活随笔為你收集整理的[Spring cloud 一步步实现广告系统] 19. 监控Hystrix Dashboard的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。