1. An overview of the

Dubbo is a Java RPC framework that has been open source since 2012. It has not been updated for four and a half years due to various reasons. Only a small version was released and a small bug was fixed. Unexpectedly, the update was restored in September 2017, which is not surprising.

Many people on the Internet compare Dubbo and Spring Cloud. Maybe in everyone’s mind, these two frameworks can be equated. Later, there is a very popular table on the Internet, which compares Spring Cloud and Dubbo in detail.

Dubbo SpringCloud
Service Registry Zookeeper Spring Cloud Netfix Eureka
Service invocation mode RPC REST API
Service monitoring Dubbo-monitor Spring Boot Admin
fuse imperfect Spring Cloud Netflix Hystrix
The service gateway There is no Spring Cloud Netflix Zuul
Distributed configuration There is no Spring Cloud Config
Service tracking There is no Spring Cloud Sleuth
The data flow There is no Spring Cloud Stream
The batch task There is no Spring Cloud Task
Information on the bus There is no Spring Cloud Bus

Some of the core components are listed above, but it should be noted that Dubbo does not mean that the components summarized as “none” in the table above cannot be implemented. It is just that the Dubbo framework itself does not provide it, and additional integration is needed to implement the corresponding functionality, so it does seem that Dubbo is more like a subset of Spring Cloud.

Dubbo has a huge user base in China. People hope to enjoy the ecology of Spring Cloud while using Dubbo. A variety of integration solutions appear, but because of the different service centers, various integration solutions are not so natural. It was not until the emergence of Spring Cloud Alibaba and the official provision of Nacos service registry that this problem was perfectly solved. It also provides a solution for the integration of Dubbo and Spring Cloud, named Dubbo Spring Cloud.

1.2 Overview of Dubbo Spring Cloud

Dubbo Spring Cloud is built on top of the native Spring Cloud, and its service governance capabilities can be considered Spring Cloud Plus, not only fully covering the native Features of Spring Cloud, but also providing a more stable and mature implementation. The characteristic 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 center
Service Registration and Discovery Eureka, Zookeeper, Consul Spring Cloud native registry + Dubbo native registry
Load balancing Ribbon (random, polling, etc.) Dubbo built-in implementation (random, polling and other algorithms + weights and other features)
Service Circuit Breakers Spring Cloud Hystrix Spring Cloud Hystrix + Alibaba Sentinel, etc
Service to Service calls The Open Feign, RestTemplate Spring Cloud service invocation + dubbo@Reference
Link Tracing Spring Cloud Sleuth + Zipkin Zipkin, opentracing, etc

The above comparison table is taken from the official Dubbo Spring Cloud documentation.

In addition, Dubbo Spring Cloud is based on Dubbo Spring Boot 2.7.1 and Spring Cloud 2.x, which makes it easy for developers to use both Dubbo and Spring Cloud. And migrate the application upwards at close to “zero” cost. Dubbo Spring Cloud aims to simplify the cost of Cloud native development in order to improve development efficiency and application performance.

1.3 Main features of Dubbo Spring Cloud

  • High performance RPC invocation for interface proxy: Provides high performance proxy-based remote invocation capability. The service is interface-grained and hides the low-level details of remote invocation.
  • Intelligent load balancing: Multiple built-in load balancing policies are intelligently aware of the health status of downstream nodes, significantly reducing call latency and improving system throughput.
  • Automatic service registration and discovery: Support a variety of registry services, service instances online and offline real-time awareness.
  • Highly scalable capabilities: Following the design principles of microkernel + plug-in, all core capabilities such as Protocol, Transport, Serialization are designed as extension points, treating built-in and third-party implementations equally.
  • Run-time traffic scheduling: Routing policies, such as built-in conditions and scripts, can be configured with different routing rules to easily implement functions such as grayscale advertising and same-room priority.
  • Visualized service governance and operation and maintenance (O&M) : Provides rich service governance and operation and maintenance tools: queries service metadata, service health status, and invocation statistics at any time, delivers routing policies in real time, and adjusts configuration parameters.

1.4 Why does Spring Cloud need RPC

In the Spring Cloud microservice system, most developers use the official Feign component for internal service communication. This declarative HTTP client is very simple, convenient and elegant to use, but when using Feign to consume services, Compared to RPC frameworks like Dubbo, the performance is poor.

In the microservices architecture, though, there are microservices that are deployed independently by business and run in their own processes. Communication between microservices is more likely to use HTTP as a simple communication mechanism, and REST apis are used in most cases. This communication mode is simple and efficient, and is independent of the development platform and language. However, generally, THE KeepAlive function is not enabled in HTTP. That is, the current connection is a short one.

Providing REST API services externally is a good thing, but if internal calls are also made using HTTP, the performance is poor. By default, Spring Cloud uses Feign components to make internal service calls using HTTP. In this case, It would be a good choice to use RPC calls for internal services and REST apis for external services, and Dubbo Spring Cloud gives us a way to implement this choice.

2. The actual combat

This summary will provide a simple introduction to using Nacos as the service center and Dubbo as the service provider and service consumer.

The installation, deployment, configuration, and use of Nacos have been covered in previous chapters and will not be covered here. For those who are not clear, please refer to the previous Nacos series:

The Spring Cloud Alibaba | Nacos service centers’

The Spring Cloud Alibaba | Nacos service registration and discovery”

The Spring Cloud Alibaba | Nacos cluster deployment”

The Spring Cloud Alibaba | Nacos configuration management”

2.1 Create the parent project dubbo-spring-cloud-demo

The parent project relies on pom. XML as follows:

Code listing: Alibaba/dubbo-spring-cloud-demo/ POM.xml


<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>${spring-cloud.version}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-alibaba-dependencies</artifactId>
            <version>${spring-cloud-alibaba.version}</version>
            <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <! -- Dubbo Spring Cloud Starter --> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-dubbo</artifactId> </dependency> <! -- Spring Cloud Nacos Service Discovery --> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>
Copy the code

Note:

  1. The spring-boot-starter-actuator package must be included, otherwise an error will be reported when the actuator starts.

  2. The spring-cloud-starter- Dubbo package needs to pay attention to groupId, depending on which version of Spring Cloud Alibaba is being used.

    • If the incubation version is used, the groupId used is:org.springframework.cloud
    • If the graduated version is used, the groupId used is:com.alibaba.cloud

2.2 Create subproject dubbo_api

The API module, which holds the Dubbo service interface and model definition, is not necessary and created here only for better code reuse and interface and model specification control management.

Define the abstract interface helloService.java:

Code listing: Alibaba/dubbo – spring – cloud – demo/dubbo_api/SRC/main/Java/com/springcloud dubbo_api/service/HelloService. Java


public interface HelloService {
    String hello(String name);
}
Copy the code

2.3 Creating subproject dubbo_provider, Dubbo service provider

The project relies on pom. XML as follows:

Code listing: Alibaba/dubbo-spring-cloud-demo/dubbo_provider/ POM.xml


<! -- API --> <dependency> <groupId>com.springcloud.book</groupId> <artifactId>ch13_1_dubbo_api</artifactId> <version>${project.version}</version>
</dependency>
Copy the code

The public API module is introduced here.

Implement the Dubbo interface helloServicei.java as follows:

Code list: Alibaba/dubbo-spring-cloud-demo/dubbo_provider/src/main/java/com/springcloud/dubbo_provider/service/HelloServiceI.java


@Service
public class HelloServiceI implements HelloService {
    @Override
    public String hello(String name) {
        return "Hello "+ name; }}Copy the code

Note: Here @ Service annotations and not from Spring org. Springframework. Stereotype. The Service, but the Dubbo org. The apache. Dubbo. Config. The annotation. The Service, Never misquote. The @Service annotation here simply declares that the Java Service (local) implementation is a Dubbo Service.

The configuration file application.yml needs to configure the Java service (local) as the Dubbo service (remote) as follows:

Code listing: Alibaba/dubbo – spring – cloud – demo/dubbo_provider/SRC/main/resources/application. The yml


server:
port: 8000
dubbo:
    scan:
        base-packages: com.springcloud.book.ch13_1_dubbo_provider.service
protocol:
    name: dubbo
    port: -1
registry:
    address: spring-cloud://192.168.44.129
spring:
application:
    name: dubbo-spring-cloud-provider
cloud:
    nacos:
    discovery:
        server-addr: 192.168.44.129:8848
main:
    allow-bean-definition-overriding: true
Copy the code

Note: In exposing the Dubbo service, it is recommended to use an externalized configuration that specifies the scan base package for the Java service implementation class.

Dubbo Spring Cloud inherits the externalized configuration features of Dubbo Spring Boot, and can also be annotated with @dubboComponentScan for benchmark package scanning.

  • dubbo.scan.base-packages: Specifies the scan base package for the Dubbo service implementation class
  • dubbo.protocol: The configuration of the protocol exposed by the Dubbo service. The sub-attribute name is the protocol name, and the sub-attribute port is the protocol port (-1 indicates the auto-adding port, starting from 20880).
  • dubbo.registry: Dubbo service registry configuration, where the value of the sub-property address is “spring-cloud://192.168.44.129”, indicating that the mount is to the Spring Cloud registry
  • spring.application.name: Spring application name for Spring Cloud service registration and discovery This value is considered under Dubbo Spring Cloud blessingdubbo.application.nameTherefore, there is no need to display the configurationdubbo.application.name.
  • spring.main.allow-bean-definition-overriding: Added this setting in Spring Boot 2.1 and later because Spring Boot adjusts the Bean definition override behavior by default.
  • spring.cloud.nacos.discovery: Nacos service discovery and registration configuration, where the sub-attribute server-addr specifies the Nacos server host and port.

Create an main class Ch131DubboProviderApplication. Java:

Code list: Alibaba/dubbo-spring-cloud-demo/dubbo_provider/src/main/java/com/springcloud/dubbo_provider/DubboProviderApplication.jav a


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

2.4 Create subproject dubbo_Consumer, service caller:

The project relies on pom. XML as follows:

Code listing: Alibaba/dubbo-spring-cloud-demo/dubbo_consumer/ POM.xml


<! -- API --> <dependency> <groupId>com.springcloud.book</groupId> <artifactId>ch13_1_dubbo_api</artifactId> <version>${project.version}</version>
</dependency>
Copy the code

Project configuration application.yml is as follows:

Code listing: Alibaba/dubbo – spring – cloud – demo/dubbo_consumer/SRC/main/resources/application. The yml


Server: port: 8080 dubbo: protocol: name: dubbo port: -1 Registry: address: spring-cloud://192.168.44.129 cloud: subscribed-services: dubbo-spring-cloud-provider spring: application: name: dubbo-spring-cloud-consumer cloud: nacos: Discovery: server-ADDR: 192.168.44.129:8848 main: Allow-bean -definition- Unifying:true
Copy the code
  • dubbo.cloud.subscribed-services: indicates the name of the service to be subscribed to. This parameter can be configuredThe '*', which means to subscribe to all services. It is not recommended. To subscribe to multiple applications, use a comma to split them.

Test the helloController.java interface as follows:

Code list: Alibaba/dubbo-spring-cloud-demo/dubbo_consumer/src/main/java/com/springcloud/dubbo_consumer/controller/HelloController.j ava


@RestController
public class HelloController {
    @Reference
    private HelloService helloService;

    @GetMapping("/hello")
    public String hello() {
        return helloService.hello("Dubbo!"); }}Copy the code

Note: @ Reference annotation is org. The apache. Dubbo. Config. The annotation. The Reference.

Start the main class Ch131DubboConsumerApplication. Java is as follows:

Code list: Alibaba/dubbo-spring-cloud-demo/dubbo_consumer/src/main/java/com/springcloud/dubbo_consumer/DubboConsumerApplication.jav a


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

2.5 test

Start the dubbo_provider and dubbo_Consumer sub-projects. After the start, we can access the Nacos console service list and see two services, as shown in the figure:

We open the browser visit: http://localhost:8080/hello, you can see the page display properly Hello Dubbo! , the test is successful, as shown in the figure:

3. Example code

Example code -Github

Example code -Gitee

4. Reference

Dubbo Spring Cloud official document