@RestControllerpublicclassTestServiceConsumer { @Reference(interfaceClass =DubboHealthService.class,cluster ="broadcast2")privateDubboHealthService dubboHealthService; @GetMapping("/health")publicStringbroadCast(){try{dubboHealthService.health(); }catch (Exception e){Map<String,String> m =RpcContext.getServerContext().getAttachments();returnm.toString()+"|"+"fail"; }Map<String,String> m =RpcContext.getServerContext().getAttachments();returnm.toString()+"|"+"success"; }}
provider demo
@ServicepublicclassDubboHealthServiceImplimplementsDubboHealthService { @OverridepublicStringhealth() {// int i = 1/0;return"i am provider2"; }}
执行结果
所有provider全部成功:
>curl http://localhost:8081/health
>{broadcast.results=[{"ip":"10.220.47.253","port":20880,"data":"i am provider1"},{"ip":"10.220.47.253","port":20881,"data":"i am provider2"}]}|success%
令其中一个provider执行除以零:
>curl http://localhost:8081/health
>{broadcast.results=[{"ip":"10.220.47.253","port":20880,"data":"i am provider1"},{"ip":"10.220.47.253","port":20881,"exceptionMsg":"/ by zero"}]}|success%