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

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

UTOOLS1565828973486.png

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

Hystrix 在服务中的使用 结合openfeign使用

在我们实际的项目当中,使用的最多的就是结合FeignClient#fallback和Hystrix一起来实现熔断,我们看一下我们在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个点:

@Target({ElementType.METHOD})表明当前的注解只能应用在方法上面。

可直接定义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) { ... } }

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/zzsdgp.html