Skip to content

Checking your reading list…

Retry verification

Stay curious. Build better.

Appearance

Choose how BestHub looks on this device.

Changes apply immediately.

Search articles

Open Discover with these filters

Suggested searches

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

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.

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.

About this article

For questions about attribution, corrections or content rights, please contactadmin@besthub.devand we will review it promptly.

Report an issue with this article
Javamicroservicesload balancingdubboroutingspring-cloudREST

Reader feedback

Was this useful?

Useful0

Discussion 0

Questions, practical examples and thoughtful responses.

Open full discussion