[Spring cloud 一步步实现广告系统] 19. 监控Hystrix Dashboard
在之前的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ù)。
使用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個點:
以我們的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)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 模块的四种形式 模块的调用 循环导入问题
- 下一篇: spotlight监控linux性能