Bridging Dubbo and Spring Cloud: Routing, Load Balancing, and REST Integration

This article explains how Dubbo Spring Cloud enables Spring Cloud clients to invoke Dubbo services, covering metadata handling, code examples, configuration steps, and a comparison of routing and load‑balancing mechanisms between Dubbo and Spring Cloud.

Programmer DD
Programmer DD
Programmer DD
Bridging Dubbo and Spring Cloud: Routing, Load Balancing, and REST Integration

Dubbo Spring Cloud is a module provided by the Spring Cloud Alibaba project that allows Spring Cloud clients (RestTemplate or OpenFeign) to call Dubbo services.

Apache Dubbo and Spring Cloud are fundamentally different frameworks: Dubbo exposes services at the interface level, while Spring Cloud exposes services at the application level via HTTP, requiring a way to associate HTTP request details with Dubbo services.

Dubbo Spring Cloud introduces an application‑level registration mechanism and defines a DubboMetadataService to store Dubbo service metadata. The interface is:

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 = URL JSON)
    Map<String, String> getAllExportedURLs();
    // Get 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:

@Service
@RestController
public class SpringRestService implements RestService {
    @Override
    @GetMapping("/param")
    public String param(@RequestParam String param) {
        return param;
    }
}

The corresponding RestMethodMetadata (simplified) looks like:

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}]}, headers=[], consumes=[], produces=[]}, ...}

Dubbo services can also expose REST via JAX‑RS annotations. Example:

@Service(version = "1.0.0", protocol = {"dubbo", "rest"})
@Path("/")
public class StandardRestService implements RestService {
    @Override
    @Path("param")
    @GET
    public String param(@QueryParam("param") String param) {
        return param;
    }
}

Using the same metadata extraction, the JAX‑RS annotated service yields identical RestMethodMetadata as the Spring MVC example.

Calling Dubbo Services – Development Steps

(1) Add the Spring Cloud Dubbo starter dependency:

<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-starter-dubbo</artifactId>
</dependency>

Configure the registry to use Spring Cloud: dubbo.registry.address=spring-cloud://localhost (2) On the provider side, annotate the service with Spring MVC annotations or expose it via JAX‑RS.

@Service(version = "1.0.0")
@RestController
public class SpringRestService implements RestService {
    @Override
    @GetMapping("/param")
    public String param(@RequestParam String param) { return param; }

    @Override
    @PostMapping("/params")
    public String params(@RequestParam int a, @RequestParam String b) { return a + b; }
}

Or using JAX‑RS:

@Service(version = "1.0.0", protocol = {"dubbo", "rest"})
@Path("/")
public class StandardRestService implements RestService {
    @Override
    @Path("param")
    @GET
    public String param(@QueryParam("param") String param) { return param; }

    @Override
    @Path("params")
    @POST
    public String params(@QueryParam("a") int a, @QueryParam("b") String b) { return a + b; }
}

(3) On the consumer side, add the @DubboTransported annotation to enable HTTP‑based transport.

@Bean
@LoadBalanced
@DubboTransported
public RestTemplate restTemplate() {
    return new RestTemplate();
}
@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 b, @RequestParam("a") int a);
}

Invoke the Dubbo service:

restTemplate.getForEntity("http://dubbo-provider-service/param?param=deepinspringcloud", String.class);

dubboFeignRestService.param("deepinspringcloud");

Routing and Load Balancing

Dubbo defines a Router interface that filters an Invoker list based on the current Invocation:

public interface Router extends Comparable<Router> {
    <T> List<Invoker<T>> route(List<Invoker<T>> invokers, URL url, Invocation invocation) throws RpcException;
}

The RouterChain applies a chain of routers sequentially:

public class RouterChain<T> {
    public List<Invoker<T>> route(URL url, Invocation invocation) {
        List<Invoker<T>> finalInvokers = invokers;
        for (Router router : routers) {
            finalInvokers = router.route(finalInvokers, url, invocation);
        }
        return finalInvokers;
    }
}

Developers can add custom routers via SPI. Compared with Spring Cloud’s Ribbon, Dubbo’s router is tightly bound to the Invocation (the request), allowing routing decisions to be made directly on request attributes without extra ThreadLocal context.

Spring Cloud’s default ZoneAvoidanceRule relies on ServerStats to filter unhealthy instances, while many other IRule implementations lack this capability unless developers explicitly integrate ServerStats.

Overall, Dubbo’s routing mechanism offers a more elegant and request‑aware approach than the decoupled Ribbon design in Spring Cloud.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

JavaMicroservicesload balancingDubboroutingSpring Cloudrest
Programmer DD
Written by

Programmer DD

A tinkering programmer and author of "Spring Cloud Microservices in Action"

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.