在以前的18次文章中,咱們實現了廣告系統的廣告投放
,廣告檢索
業務功能,中間使用到了 服務發現Eureka
,服務調用Feign
,網關路由Zuul
以及錯誤熔斷Hystrix
等Spring Cloud
組件。 簡單調用關係: java
可是系統每每都會報錯,咱們以前定義了一些容錯類和方法,可是隻是在控制檯能夠看到錯誤信息,咱們想要統計一些數據,怎麼才能更直觀的看到咱們的服務調用狀況呢,接下來,和你們討論一個新的熔斷監控組件Hystrix Dashboard
,顧名思義,從名字上咱們就能看出來,它是監控的圖形化界面。web
在咱們實際的項目當中,使用的最多的就是結合FeignClient#fallback
和Hystrix
一塊兒來實現熔斷,咱們看一下咱們在mscx-ad-feign-sdk
中的實現。spring
@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的實現類:springboot
@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
服務。app
@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
,咱們看一下這個類的源碼:ide
@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></p> * default => see {@link com.netflix.hystrix.contrib.javanica.command.GenericCommand#getFallback()} * * @return method name */ String fallbackMethod() default ""; ... }
咱們主要關注2個點:spring-boot
@Target({ElementType.METHOD})
代表當前的註解只能應用在方法上面。fallbackMethod
來保證容錯。這個方法有一個缺陷,就是必須和執行方法在同一個類文件中,這就會形成咱們的方法在實現的時候,顯得特別的冗餘和不夠優雅。以咱們的mscx-ad-search
中的廣告查詢爲例:fetch
@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方法來實現。ui
上述代碼咱們看了Hystrix實現熔斷的2種方式,接下來咱們來實現請求監控的圖形化界面,建立mscx-ad-dashboard
,Let's code. 依然聽從咱們springboot項目的三部曲:this
加依賴
<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 @EnableHystrixDashboard public class AdDashboardApplication { public static void main(String[] args) { SpringApplication.run(AdDashboardApplication.class, args); } }
改配置
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: "*"`
直接啓動,能夠看到以下頁面:
添加要監控的服務地址: </list<adplanvo></list<adplanvo>