The author | mascherano

The **Dubbo Community has created a series of articles to review the development of the Apache Dubbo product and community, and look ahead to the future. This series of articles mainly covers three parts: Dubbo technology interpretation, community operation and application case analysis. This is the third article in a series.

preface

In the Java microservices ecosystem, Spring Cloud has become the preferred technology stack for developers. However, as the practice deepens and the application scale expands, people gradually realize the limitations of Spring Cloud.

In terms of service governance, Spring Cloud is immature compared to Dubbo. Unfortunately, Dubbo is often viewed by some developers as an RPC framework for service governance rather than a microservices infrastructure. Even those interested in migrating Spring Cloud to Dubbo may balk at the cost of migration and transformation.

Fortunately, the arrival of Dubbo 3.0 will bring an important change to this situation. In the future, Dubbo Spring Cloud will seamlessly connect with Dubbo 3.0 as the core component of Spring Cloud Alibaba. Fully embrace the Spring Cloud stack, seamlessly integrating Spring Cloud registries including Nacos, Eureka, Zookeeper, and Consul, Fully compatible with Spring Cloud Open Feign and @LoadBalanced RestTemplate, this article will discuss how Dubbo Spring Cloud revolutionized the Spring Cloud stack. Because of the breadth of features covered by the Spring Cloud technology stack, the scope of this article is limited to service governance.

As a preview of Dubbo 3.0, this article focuses on the current version of Dubbo Spring Cloud implementation, which is based on the application-level service discovery mentioned in one of our previews.

Application-level service discovery is an important part of Dubbo 3.0 planning and is the cornerstone of Dubbo’s integration with cloud native infrastructure to enable large-scale microservice clusters. Dubbo has been exploring application-level service discovery since version 2.7.5 in an attempt to optimize Dubbo’s service discovery model, so Dubbo Spring Cloud is based on Dubbo Spring Boot 2.7.x (since 2.7.0, Dubbo Spring Boot is version-consistent with Dubbo) and Spring Cloud 2.x, and this article will be based on the previous version of 2.7.x.

Regardless of whether a developer is a Dubbo user or a Spring Cloud user, Dubbo Spring Cloud can be easily navigated and applications can be migrated upward at near “zero” cost. Dubbo Spring Cloud aims to simplify Cloud Native development costs, improve r&d efficiency and improve application performance.

Versioning support

Since Spring Cloud Edgware(” E “version) will be discontinued after August 1, 2019, Dubbo Spring Cloud releases do not currently support” E “version, Developed only for “F” and “G” releases, Spring Cloud users are also advised and encouraged to upgrade to “F” or “G” releases.

Meanwhile, Dubbo Spring Cloud is developed based on Apache Dubbo Spring Boot 2.7.x (minimum Java version 1.8), Full Dubbo annotation driver, externalization configuration, and production-ready features are available here.

The following table illustrates the Dubbo Spring Cloud version mapping:

Spring Cloud Spring Cloud Alibaba Spring Boot Dubbo Spring Boot
Finchley 0.2.2. RELEASE X 2.0. 2.7.1
Greenwich 2.2.1. RELEASE X 2.1. 2.7.1
Edgware 0.1.2. RELEASE X 1.5. : X: Dubbo Spring Cloud does not support this version

features

Since Dubbo Spring Cloud is built on top of the native Spring Cloud, its service governance capabilities can be considered as Spring Cloud Plus, which not only fully covers Spring Cloud native features, but also provides a more stable and mature implementation. The feature comparison is shown in the following table:

Functional components Spring Cloud Dubbo Spring Cloud
Distributed Configuration Git, Zookeeper, Consul, JDBC Spring Cloud Distributed Configuration + Dubbo Configuration CenterDubbo 2.7 supports configuration center and can be customized)
Service Registration and Discovery Eureka, Zookeeper, Consul Spring Cloud Native Registry (in addition to Eureka, Zookeeper, Consul, and Nacos in Spring Cloud Alibaba) +Dubbo Native Registry
Load balancing Ribbon (Random, polling, etc.) Dubbo built-in implementation (random, polling algorithm + weight and other features)
Service Circuit Breakers Spring Cloud Hystrix Spring Cloud Hystrix +Alibaba Sentinel(such asSentinel has been accepted as a candidate implementation for the Circuit Breaker by the Spring Cloud project)
Service-to-service Calls The Open Feign, RestTemplate Spring Cloud service invocation + Dubbo@Reference
Link Tracing Spring Cloud Sleuth +Zipkin  Zipkin, opentracing, etc

Highlighting features

1) Dubbo uses Spring Cloud service for registration and discovery

Dubbo Spring Cloud implements Dubbo service registration and discovery based on Spring Cloud Commons abstraction. An application can easily bridge to all native Spring Cloud registries by adding the externalized configuration property “dubo.registry. address = spring-cloud://localhost” : – nacos-Eureka – Zookeeper-Consul

Note: Dubbo Spring Cloud will support the coexistence of Spring Cloud registries and Dubbo registries in the next release, providing a dual registration mechanism for seamless migration.

2) Dubbo is invoked as a Spring Cloud service

By default, Spring Cloud Open Feign and @loadBalancedRestTemplate are the two service invocation methods of Spring Cloud. Dubbo Spring Cloud provides a third option where the Dubbo service appears as an equivalent citizen of the Spring Cloud service invocation, Applications can use Apache Dubbo annotations @service and @Reference to expose and Reference Dubbo services and realize communication between services over multiple protocols. At the same time, a service gateway can be easily implemented using the Dubbo generalization interface.

3) Dubbo service introspection

Dubbo Spring Cloud introduces a new Service governance feature, Service Introspection, designed to minimize registry load and de-centralize Dubbo registry metadata. Suppose a Spring Cloud application introduces the Dubbo Spring Boot Starter and exposes N Dubbo services. In the Case of the Dubbo Nacos registry, the current application will register N+1 Nacos applications. In addition to the Spring Cloud application itself, the other N applications are from the Dubbo service. The larger N is, the heavier the registry load is.

As a result, Dubbo Spring Cloud applications load registries N times as much as traditional Dubbo, theoretically increasing their cluster size by N times without increasing infrastructure investment. Of course, the future Dubbo will also offer the ability to service introspection.

4) Dubbo migrates Spring Cloud service calls

Although Dubbo Spring Cloud fully retains the native Spring Cloud service invocation features, Dubbo service governance capabilities are beyond the capabilities of Spring Cloud Open Feign in terms of high performance, high availability, and load balancing stability. Therefore, developers are advised to migrate the Spring Cloud Open Feign or @loadBalancedRestTemplate to the Dubbo service.

Considering that the migration process is not overnight, Dubbo Spring Cloud provides a solution, @dubborehabilitation annotation. This annotation can help the Spring Cloud Open Feign interface on the service consumer side and the @LoadBalanced RestTemplate Bean to make the underlying Dubbo call (toggle the Dubbo supported protocol). The service provider simply appends the Dubo@Servce annotation (interface extraction required) to the existing @RestController class, in other words, making the migration seamless without adjusting the Feign interface or the RestTemplate URL. If the migration time is sufficient, it is recommended to use the Dubbo service to refactor the definition of the native Spring Cloud service on your system.

A simple example

Developing a Dubbo Spring Cloud application is similar to a traditional Dubbo or Spring Cloud application. To fully implement a Dubbo service provider and consumer application, follow these steps. For complete sample code, visit resources:

  • Dubbo service provider application
  • Dubbo service consumer application

1. Define the Dubbo service interface

A Dubbo service interface is a remote communication contract between a service provider and a consumer. It is usually declared by a common Java interface, such as the EchoService interface:

public interface EchoService {


    String echo(String message);
}
Copy the code

To ensure contract consistency, it is recommended to package the Dubbo service interface into a second or third party artifact (JAR), such as the artifact Spring-cloud-Dubbo-sample-API.

For service providers, Dubbo service interfaces not only need to be introduced as dependencies, but also need to be implemented. The corresponding service consumer, again, needs to rely on the artifact and execute remote methods in the form of interface calls. Next, we discuss how to implement Dubbo service providers and consumers.

2. Realize Dubbo service provider

1) Initialize the Spring-Cloud-Dubo-server-sample Maven project

First, create the Maven project named By artifactId spring-cloud-dubbo-server-sample and add the necessary dependencies for Dubbo Spring Cloud to its POP.xml file:

<dependencies> <! -- Sample API --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dubbo-sample-api</artifactId> <version>${project.version}</version> </dependency> <! -- Spring Boot dependencies --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-actuator</artifactId> </dependency> <! -- Dubbo Spring Cloud Starter --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-dubbo</artifactId> </dependency> <! -- Spring Cloud Nacos Service Discovery --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> </dependency> </dependencieCopy the code

The above dependent artifact is described as follows:

  • Spring-cloud-dubbo-sample-api: artifact providing an EchoService interface;

  • Bing-boot-actuator: Spring boot production-ready artifact, importing bING-boot artifact indirectly;

  • Spring-cloud-starter-dubbo: Dubbo Spring cloud starter artifact, which indirectly introduces dubbo-spring-boot-starter and other artifacts;

  • Spring – Cloud -starter- Alibaba – Nacos – Discovery: NacOS Spring Cloud service registration and discovery artifact.

Note that the artifact does not specify a version, so

must also be explicitly declared:

<dependencyManagement> <dependencies> <! -- Spring Cloud Alibaba dependencies --> <dependency> <groupId>org.springframework.cloud</groupId> < artifactId > spring - the cloud - alibaba - dependencies < / artifactId > < version > 2.2.1. RELEASE < / version > < type > pom < type > <scope>import</scope> </dependency> </dependencies> </dependencyManagement>Copy the code

Note: Refer to the spring-cloud-dubbo-server-sample pom.xml file for complete Maven dependency configuration above.

After completing the above steps, the next step is to implement the Dubbo service.

2) Implement Dubbo service

EchoService as the exposed Dubbo service interface, spring-Cloud-Dubbo-server-SAMPLE service provider needs to implement it:

@org.apache.dubbo.config.annotation.Service class EchoServiceImpl implements EchoService { @Override public String echo(String message) { return "[echo] Hello, " + message; }}Copy the code

Among them, the @ org. Apache. Dubbo. Config. The annotation. Service is the dubbo Service annotations, declare the Java Service serve dubbo (local) implementation. Therefore, the next step is to configure it with the Dubbo service (remote).

3) Configure the Dubbo service provider

In terms of exposing the Dubbo service, it is recommended that the developer externalize the configuration by specifying the scan base pack for the Java service implementation class.

Note: Dubbo Spring Cloud inherits the externalized configuration features of Dubbo Spring Boot and can also be used for baseline scanning by annotating @DubboComponentScan.

Meanwhile, the Dubbo remote service needs to expose the network port and set up the communication protocol. The complete YAML configuration is as follows:

Dubbo: scan: # dubbo scan service benchmark package base - packages: org. Springframework. Cloud. Alibaba. Dubbo. The bootstrap protocol: Port: -1 Spring: application: # dubbo name: dubbo # dubbo port: -1 Spring: application: # dubbo name: Discovery: serveraddr: 127.0.0.1:8848Copy the code

The above YAML content, the top half of the Dubbo configuration:

  • Dubo.scan. Base-packages: specifies the scan base packages for dubbo service implementation classes.

  • Protocol: indicates the protocol configuration exposed by the Dubbo service. Subattribute Name is the protocol name, and port is the protocol port (-1 indicates the self-added port starting from 20880).

  • Dubbo. registry: Dubbo service registry configuration, where the value of the subproperty address is “spring-cloud://localhost”, indicating that it is mounted to the Spring Cloud registry.

The lower part is the Spring Cloud configuration:

  • Spring.application. name: The name of the Spring application used for spring Cloud service registration and discovery. > This value is treated as dubbo.application.name under Dubbo Spring Cloud, so dubbo.application.name is no longer explicitly configured;

  • Spring. Cloud. Nacos. Discovery: nacos service discovery and register configuration, including child property server – addr specified nacos server host and port.

For the complete YAML configuration file above, refer to the spring-Cloud-dubo-server-samplebootstrap. YAML file.

After completing the above steps, you need to write a Dubbo Spring Cloud boot class.

4) Boot Dubbo Spring Cloud service provider applications

The Dubbo Spring Cloud boot class is no different from a normal Spring Cloud application, as shown below:

@EnableDiscoveryClient @EnableAutoConfiguration public class DubboSpringCloudServerBootstrap { public static void main(String[] args) { SpringApplication.run(DubboSpringCloudServerBootstrap.class); }}Copy the code

Before guide DubboSpringCloudServerBootstrap, please start Nacos server in advance. When DubboSpringCloudServerBootstrap starts, apply a spring – the cloud – dubbo – server – sample will appear in the Nacos console interface.

When the Dubbo service provider is started, the next step is to implement a Dubbo service consumer.

3. Realize Dubbo service consumer

The Java service for EchoService, spring-Cloud-Dubbo-server-sample and Nacos server are all ready. The Dubbo service consumer only needs to initialize Maven spring-Cloud-Dubbo-client-sample and consume the Dubbo service.

1) Initialize the Spring-Cloud-Dubo-client-sample Maven project

Maven dependencies need to be added to the service provider Maven project class:

<dependencyManagement> <dependencies> <! -- Spring Cloud Alibaba dependencies --> <dependency> <groupId>org.springframework.cloud</groupId> < artifactId > spring - the cloud - alibaba - dependencies < / artifactId > < version > 2.2.1. RELEASE < / version > < type > pom < type > <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <dependencies> <! -- Sample API --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dubbo-sample-api</artifactId> <version>${project.version}</version> </dependency> <! -- Spring Boot dependencies --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-actuator</artifactId> </dependency> <! -- Dubbo Spring Cloud Starter --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-dubbo</artifactId> </dependency> <! -- Spring Cloud Nacos Service Discovery --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> </dependency> </dependencCopy the code

Different from the spring-cloud-dubo-server-sample application, the current application relies on spring-boot-starter-web, indicating that it belongs to the Web Servlet application.

Note: Refer to the spring-cloud-dubbo-client-sample pom.xml file for the complete Maven dependency configuration above.

2) Configure Dubbo service consumers

The configuration of Dubbo service consumer is similar to that of service provider. Currently, spring-cloud-Dubbo-client-sample is a pure service consumer. Therefore, the required externalization configuration is more simplified:

Subscribed -services: Spring-cloud-alibaba-dubbo -server Spring: Application: # dubbo: subscribed-services: Spring-cloud-alibaba-dubbo -server Spring: Application: # Discovery: server-addr: 127.0.0.1:8848Copy the code

Comparison of spring-cloud-dubbo-server-sample applications, except for the difference of application name spring.application.name, The spring-cloud-dubo-client-sample attribute was added to the new dubo.cloud. Subscribed -services setting. And this value applies to “spring-cloud-dubbo-server-sample” for the service provider.

Dubo.cloud. subscribed-services: a list of application names used by service consumers to subscribe to service providers. In case of subscribing to multiple applications, use a “,” split. The default value of “*” is not recommended, it will subscribe to all applications.

When the application uses the default value of dubo.cloud. subscribed-services, a warning is printed in the log:

Current application will subscribe all services(size:x) in registry, a lot of memory and CPU cycles may be used,

thus it’s strongly recommend you using the externalized property ‘dubbo.cloud.subscribed-services’ to specify the services

Because the current application belongs to the Web application, it uses 8080 as the Web service port by default. If you need to customize it, you can adjust it by using the property server.port.

Note: For the complete YAML configuration file above, please refer to the spring-Cloud-dubo-client-sample ‘ ‘bootstrap. YAML file.

3) Bootstrap Dubbo Spring Cloud service consumer applications

To reduce implementation steps, the following bootstrap classes combine Dubbo service consumption and bootstrap functionality into one:


@EnableDiscoveryClient
@EnableAutoConfiguration
@RestController
public class DubboSpringCloudClientBootstrap {


    @Reference
    private EchoService echoService;


    @GetMapping("/echo")
    public String echo(String message) {
        return echoService.echo(message);
    }


    public static void main(String[] args) {
        SpringApplication.run(DubboSpringCloudClientBootstrap.class);
    }
Copy the code

Not only that, DubboSpringCloudClientBootstrap as REST Endpoint, through exposed/echo Web services, consumer Dubbo the EchoService service. Curl curl curl curl curl curl curl curl curl

$ curl http://127.0.0.1:8080/echo?message=%E5%B0%8F%E9%A9%AC%E5%93%A5%EF%BC%88mercyblitz%EF%BC%89
Copy the code

The HTTP response is:

[echo] Hello, MercyblitzCopy the code

The above results show that spring-cloud-Dubbo-client-sample is used to return the content of spring-cloud-Dubbo-server-sample calculation by the service provider by consuming the Dubbo service.

High order sample

If you need more details on how to use the Dubbo Spring Cloud, please refer to the official Samples.

The sub-modules are described as follows:

  • Spring-cloud-dubbo-sample-api: API module, storing dubbo service interface and model definition;

  • Spring-cloud-dubbo-provider-web-sample: dubbo Spring Cloud service provider example (Web application);

  • Spring-cloud-dubbo-provider-sample: dubbo Spring Cloud service provider example (non-Web application);

  • Spring-cloud-dubbo-consumer-sample: Dubbo Spring Cloud service consumer example;

  • Spring-cloud-dubbo-servlet-gateway-sample: simple implementation example of dubbo Spring Cloud servlet gateway.

The problem of feedback

If you have any problems using the Dubbo Spring Cloud, please report them here.

Feedback link: github.com/alibaba/spr…

In order to read

For more Dubbo Spring Cloud features and design details, check out:

  • Spring Cloud Alibaba Wiki: github.com/alibaba/spr…
  • Dubbo’s blog: dubbo.apache.org/zh-cn/blog/…

Series of articles:

  • Dubbo Cloud Native Path: ASF graduation anniversary, 3.0 available
  • Dubbo takes an important step in cloud native – application level service discovery parsing

Author’s brief introduction

Mercyblitz, Java persuader, Apache Dubbo PMC, Spring Cloud Alibaba project architect. Currently, I am mainly responsible for ali Group’s middleware open source project, micro-service technology implementation, architecture evolution, infrastructure construction, etc.

“Alibaba Cloud originator focuses on micro-service, Serverless, container, Service Mesh and other technical fields, focuses on the trend of cloud native popular technology, large-scale implementation of cloud native practice, and becomes the public account that most understands cloud native developers.”