[Spring cloud 一步步实现广告系统] 19. 监控Hystrix Dashboard
在之前的18次文章中,我們實(shí)現(xiàn)了廣告系統(tǒng)的廣告投放,廣告檢索業(yè)務(wù)功能,中間使用到了 服務(wù)發(fā)現(xiàn)Eureka,服務(wù)調(diào)用Feign,網(wǎng)關(guān)路由Zuul以及錯(cuò)誤熔斷Hystrix等Spring Cloud組件。
簡(jiǎn)單調(diào)用關(guān)系:
但是系統(tǒng)往往都會(huì)報(bào)錯(cuò),我們之前定義了一些容錯(cuò)類和方法,但是只是在控制臺(tái)可以看到錯(cuò)誤信息,我們想要統(tǒng)計(jì)一些數(shù)據(jù),怎么才能更直觀的看到我們的服務(wù)調(diào)用情況呢,接下來(lái),和大家討論一個(gè)新的熔斷監(jiān)控組件Hystrix Dashboard,顧名思義,從名字上我們就能看出來(lái),它是監(jiān)控的圖形化界面。
Hystrix 在服務(wù)中的使用
結(jié)合openfeign使用
在我們實(shí)際的項(xiàng)目當(dāng)中,使用的最多的就是結(jié)合FeignClient#fallback和Hystrix一起來(lái)實(shí)現(xiàn)熔斷,我們看一下我們?cè)趍scx-ad-feign-sdk中的實(shí)現(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請(qǐng)求,必須在所有參數(shù)前添加{@link RequestParam},不能使用{@link Param}* 會(huì)被自動(dòng)轉(zhuǎn)發(fā)為POST請(qǐng)求。*/CommonResponse getUsers(@RequestParam(value = "username") String username); }在上述代碼中,我們自定義了一個(gè)feignclient,并且給了這個(gè)client一個(gè)fallback的實(shí)現(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.");} }這個(gè)fallback類實(shí)現(xiàn)了我們自定義的ISponsorFeignClient,那是因?yàn)閒allback的方法必須和原始執(zhí)行類的方法簽名保持一致,這樣在執(zhí)行失敗的時(shí)候,可以通過(guò)反射映射到響應(yīng)的降級(jí)方法/容錯(cuò)方法。
在mscx-ad-search服務(wù)中,我們通過(guò)注入ISponsorFeignClient來(lái)調(diào)用我們的mscz-ad-sponsor服務(wù)。
使用HystrixCommand
其實(shí)Hystrix本身提供了一種直接在方法中應(yīng)用的方式,就是使用@ com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand,我們看一下這個(gè)類的源碼:
@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個(gè)點(diǎn):
以我們的mscx-ad-search中的廣告查詢?yōu)槔?#xff1a;
@Service @Slf4j public class SearchImpl implements ISearch {/*** 查詢廣告容錯(cuò)方法** @param e 第二個(gè)參數(shù)可以不指定,如果需要跟蹤錯(cuò)誤,就指定上* @return 返回一個(gè)空map 對(duì)象*/public SearchResponse fetchAdsFallback(SearchRequest request, Throwable e) {System.out.println("查詢廣告失敗,進(jìn)入容錯(cuò)降級(jí) : %s" + e.getMessage());return new SearchResponse().builder().adSlotRelationAds(Collections.emptyMap()).build();}@HystrixCommand(fallbackMethod = "fetchAdsFallback")@Overridepublic SearchResponse fetchAds(SearchRequest request) {...} }在我們請(qǐng)求出錯(cuò)的時(shí)候,會(huì)轉(zhuǎn)到我們的fallback方法,這個(gè)實(shí)現(xiàn)是通過(guò)在應(yīng)用啟動(dòng)的時(shí)候,我們開始了@EnableCircuitBreaker注解,這個(gè)注解會(huì)通過(guò)AOP攔截所有的HystrixCommand方法,將HystrixCommand整合到springboot的容器中,并且將注解標(biāo)注的方法放入hystrix的線程中,一旦失敗,通過(guò)反射調(diào)用fallback方法來(lái)實(shí)現(xiàn)。
創(chuàng)建dashboard project
上述代碼我們看了Hystrix實(shí)現(xiàn)熔斷的2種方式,接下來(lái)我們來(lái)實(shí)現(xiàn)請(qǐng)求監(jiān)控的圖形化界面,創(chuàng)建mscx-ad-dashboard,Let's code.
依然遵從我們springboot項(xiàng)目的三部曲:
加依賴
<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 啟動(dòng)類** @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: "*"`直接啟動(dòng),可以看到如下頁(yè)面:
添加要監(jiān)控的服務(wù)地址:
轉(zhuǎn)載于:https://www.cnblogs.com/zhangpan1244/p/11361078.html
總結(jié)
以上是生活随笔為你收集整理的[Spring cloud 一步步实现广告系统] 19. 监控Hystrix Dashboard的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 模块的四种形式 模块的调用 循环导入问题
- 下一篇: spotlight监控linux性能