How Dubbo Spring Cloud Bridges RPC and HTTP: Routing, Load Balancing, and Integration Guide
This article explains what Dubbo Spring Cloud is, how it registers Dubbo services as REST metadata, the core DubboMetadataService API, step‑by‑step integration with Spring Cloud clients, and the differences in routing and load‑balancing compared to traditional Spring Cloud Ribbon.
What Is Dubbo Spring Cloud
Dubbo Spring Cloud is a module provided by the Spring Cloud Alibaba project that allows Spring Cloud clients (RestTemplate or OpenFeign) to invoke Dubbo services.
Apache Dubbo and Spring Cloud are two completely different frameworks. Dubbo exposes services at the interface level, while Spring Cloud exposes services at the application level using HTTP (URL path, query parameters, headers). Dubbo Spring Cloud introduces an application‑level registration mechanism and a metadata service (DubboMetadataService) to store REST metadata for Dubbo services.
DubboMetadataService Interface
public interface DubboMetadataService {
// Get REST metadata of a Dubbo service
String getServiceRestMetadata();
// Get all service keys
Set<String> getAllServiceKeys();
// Get all exported URLs (key = ServiceKey, value = JSON URL)
Map<String, String> getAllExportedURLs();
// Get exported URLs for a specific interface, group and version
String getExportedURLs(String serviceInterface, String group, String version);
}The core method getServiceRestMetadata returns REST metadata when a Dubbo service is also annotated with Spring MVC annotations. Example of a Dubbo service with Spring MVC annotations:
@Service
@RestController
public class SpringRestService implements RestService {
@Override
@GetMapping("/param")
public String param(@RequestParam String param) {
return param;
}
}Corresponding RestMethodMetadata (simplified):
RestMethodMetadata{method=MethodMetadata{name='param', returnType='java.lang.String', params=[MethodParameterMetadata{index=0, name='param', type='java.lang.String'}], request=RequestMetadata{method='GET', path='/param', params={param=[{param}]}}}When a Dubbo service is exposed via JAX‑RS, the same REST metadata is generated. Example:
@Service(protocol = {"dubbo","rest"})
@Path("/")
public class StandardRestService implements RestService {
@Override
@Path("param")
@GET
public String param(@QueryParam("param") String param) {
return param;
}
}Both Spring MVC and JAX‑RS produce identical RestMethodMetadata, which enables RestTemplate or OpenFeign to match HTTP requests with Dubbo services.
Calling Dubbo Services with Dubbo Spring Cloud
Steps:
Obtain DubboMetadataService from the registry. Use getServiceRestMetadata to retrieve REST metadata for the target Dubbo service. Construct a GenericService based on the metadata. Invoke the service via the GenericService.
Development Procedure
Add spring-cloud-starter-dubbo dependency.
Configure the registry as dubbo.registry.address=spring-cloud://localhost.
On the provider side, annotate interfaces with Spring MVC annotations or expose them via JAX‑RS.
On the consumer side, add @DubboTransported to RestTemplate or OpenFeign beans.
Use RestTemplate or OpenFeign to call the Dubbo service.
Example RestTemplate bean:
@Bean
@LoadBalanced
@DubboTransported
public RestTemplate restTemplate() {
return new RestTemplate();
}Example OpenFeign client:
@FeignClient("nacos-provider-lb")
@DubboTransported(protocol = "dubbo")
public interface DubboFeignRestService {
@GetMapping("/param")
String param(@RequestParam("param") String param);
@PostMapping("/params")
String params(@RequestParam("b") String paramB, @RequestParam("a") int paramA);
}Calling examples:
restTemplate.getForEntity("http://dubbo-provider-service/param?param=deepinspringcloud", String.class);
dubboFeignRestService.param("deepinspringcloud");Routing and Load Balancing
Dubbo’s Router interface filters an invoker list based on the current invocation. The RouterChain applies a list of routers sequentially.
public interface Router extends Comparable<Router> {
<T> List<Invoker<T>> route(List<Invoker<T>> invokers, URL url, Invocation invocation) throws RpcException;
}In Spring Cloud, load balancing is typically based on Netflix Ribbon’s ILoadBalancer and IRule. Dubbo’s router is tightly coupled with the invocation, allowing routing decisions to be made directly from request attributes without extra thread‑local context.
Dubbo also provides a built‑in ZoneAvoidanceRule that removes unhealthy servers using ServerStats. Custom IRule implementations need to incorporate ServerStats manually to achieve similar behavior.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Programmer DD
A tinkering programmer and author of "Spring Cloud Microservices in Action"
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
