Applicable scenario: for any Dubbo consumer, broadcast calls multiple service providers. The consumer is able to collect responses from all of the providers.
2.7.12
or above.@Reference imports providers. Within the brackets, letting cluster = “broadcast2” represents doing one broadcast call that collects providers’ responses.
Broadcast calls all service providers one by one. Is able to return all service providers’ execution outcomes (success or exceptions) completely and stores providers’ responses in RpcContext.
@RestController
public class TestServiceConsumer {
@Reference(interfaceClass = DubboHealthService.class,cluster = "broadcast2")
private DubboHealthService dubboHealthService;
@GetMapping("/health")
public String broadCast(){
try{
dubboHealthService.health();
}catch (Exception e){
Map<String, String> m = RpcContext.getServerContext().getAttachments();
return m.toString()+"|"+"fail";
}
Map<String, String> m = RpcContext.getServerContext().getAttachments();
return m.toString()+"|"+"success";
}
}
@Service
public class DubboHealthServiceImpl implements DubboHealthService {
@Override
public String health() {
// int i = 1/0;
return "i am provider2";
}
}
All providers succeed:
>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%
Let one of the providers divide by zero:
>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%