The perfect combination of Spring Cloud and Dubbo, "Spring Cloud Alibaba"

Keywords: Java Dubbo Spring Zookeeper xml

Earlier, when I started the Spring Cloud Basic tutorial, I wrote this article: Basic Framework Selection for Micro Services Architecture: Spring Cloud or Dubbo?" And may have been seen by many readers.Since then, there has been a question about how to choose these two frameworks. In fact, I have clearly mentioned in this paper that the comparison between Spring Cloud and Dubbo is not fair in itself. The main former is a more complete architecture scheme, while Dubbo is only a service governance and RPC implementation scheme.

Dubbo has a very large user base in China, but its peripheral facilities and components are relatively imperfect.Many developer users also want to enjoy the Spring Cloud ecosystem, so there are also some cases and methods that Spring Cloud uses with Dubbo, but most Spring Cloud integration with Dubbo has always been a bit awkward.This is mainly due to the use of ZooKeeper in Dubbod's registration center, which did not support ZooKeeper in the Spring Cloud system at the beginning, so there are two different registries in many scenarios. After that, even if Spring Cloud supports ZooKeeper, the granularity and storage of service information are not consistent.Therefore, for a long time, on the level of service governance, the two have been a perfect combination.

It was not until Spring Cloud Alibaba emerged that such a problem could be solved.In the previous tutorials, we have introduced using Nacos in Spring Cloud Alibaba as a service registry, where Ribbon or Feign can be used to consume services just like traditional Spring Cloud applications.In this post, let's continue with the additional RPC scheme supported by Spring Cloud Alibaba: Dubbo.

Getting Started Cases

Let's start with a simple example to visualize how Dubbo is used to implement both service providers and service consumers under the Nacos Service Registry.The installation and use of Nacos are omitted here. If you do not know Nacos, you can view the series Use Nacos to implement service registration and discovery Here's a step directly into using Dubbo.

Building service interfaces

Create a simple Java project and define an abstract interface below, such as:

public interface HelloService {

    String hello(String name);

}

Build Service Interface Provider

Step 1: Create a Spring Boot project that introduces the API packages built in step 1 and Spring Cloud Alibaba's dependency on Nacos and Dubbo in pom.xml, such as:

    <dependencies>
        <!-- Built in the first step API package -->
        <dependency>
            <groupId>com.didispace</groupId>
            <artifactId>alibaba-dubbo-api</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
        <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>
        <dependency>        
            <!--<groupId>com.alibaba.cloud</groupId>-->
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-dubbo</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>

        //...
    </dependencies>

Two things to note here:

  1. The spring-boot-starter-actuator package must be included or the startup will fail.
  2. The spring-cloud-starter-dubbo package requires attention to groupId, which is determined by the version dependency of the spring cloud alibaba used.
    • During project incubation, the groupId used is: org.springframework.cloud;
    • After the project is hatched, the groupId used is modified to com.alibaba.cloud, so users need to be aware that it is used correctly.Avoid failing to load the corresponding JAR package.

Step 2: Implement the Dubbo interface

@Service
public class HelloServiceImpl implements HelloService {

    @Override
    public String hello(String name) {
        return "hello " + name;
    }

}

Note: The @Service comment here is not Spring, but org.apache.dubbo.config.annotation.Service.

Step 3: Configure information about the Dubbo service, such as:

spring.application.name=alibaba-dubbo-server
server.port=8001

spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848

# Specify the scan benchmark package for the Dubbo service implementation class
dubbo.scan.base-packages=com.didispace.alibaba.dubbo.server
dubbo.protocol.name=dubbo
dubbo.protocol.port=-1
dubbo.registry.address=spring-cloud://localhost

The configuration instructions are as follows:

  • dubbo.scan.base-packages: Specifies the scan benchmark package for the Dubbo service implementation class
  • Dubbo.protocol: The protocol configuration exposed by the Dubbo service, where the sub-attribute name is the protocol name and the port is the protocol port (-1 stands for self-increasing port, starting from 208 80)
  • Dubbo.registry: The Dubbo Service Registry configuration, where the value of the child attribute address,'spring-cloud://localhost', indicates mounting to the Spring Cloud Registry

Note: If you use Spring Boot 2.1 or later, you need to add the configuration spring.main.allow-bean-definition-overriding=true

Step 4: Create an application main class, such as:

@EnableDiscoveryClient
@SpringBootApplication
public class DubboServerApplication {

    public static void main(String[] args) {
        SpringApplication.run(DubboServerApplication.class, args);
    }

}

Building Service Interface Consumers

Step 1: Create a Spring Boot project that introduces the API packages built in step 1 and Spring Cloud Alibaba's dependence on Nacos and Dubbo in pom.xml, consistent with the service provider:

    <dependencies>
        <!-- Built in the first step API package -->
        <dependency>
            <groupId>com.didispace</groupId>
            <artifactId>alibaba-dubbo-api</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
        <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>
        <dependency>        
            <!--<groupId>com.alibaba.cloud</groupId>-->
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-dubbo</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>

        //...
    </dependencies>

Step 2: Configure information about the Dubbo service, such as:

spring.application.name=alibaba-dubbo-client
server.port=8002

spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848

dubbo.protocol.name=dubbo
dubbo.protocol.port=-1
dubbo.registry.address=spring-cloud://localhost
dubbo.cloud.subscribed-services=alibaba-dubbo-server

Be careful:

  1. The dubbo.cloud.subscribed-services parameter is added here to indicate the name of the service to which the service will be subscribed. The alibaba-dubbo-server configured here corresponds to the value of spring.application.name of the service provider in the previous section, which is the application name of the service provider.
  2. If you are using Spring Boot version 2.1 and later, you need to add the configuration spring.main.allow-bean-definition-overriding=true.

Step 3: Create an application main class and implement an interface in which the Dubbo service is invoked, such as:

@EnableDiscoveryClient
@SpringBootApplication
public class DubboClientApplication {

    public static void main(String[] args) {
        SpringApplication.run(DubboClientApplication.class, args);
    }

    @Slf4j
    @RestController
    static class TestController {

        @Reference
        HelloService helloService;

        @GetMapping("/test")
        public String test() {
            return helloService.hello("didispace.com");
        }
    }

}

Note: The @Reference comment here is org.apache.dubbo.config.annotation.Reference

Test Validation

After all the above developments have been completed, we can start Nacos, service providers, and service consumers in turn.After the startup is complete, we can see two services defined above in the list of services in the Nacos console, such as:

Next, we can trigger the consumption of dubbo services by calling the / test interface defined in the service consumer.If everything goes smoothly, you should get the following results:

$ curl localhost:8002/test
hello didispace.com

Summary

With the example above, if you've played Spring Cloud and Dubbo at the same time, you'll feel good about it.Instead of worrying about both Eureka and Zookeeper configurations and the health of both middleware, you just need to focus on and maintain one of Nacos.For the configuration and use of Dubbo, the configuration is fairly simple, and the coding is no different from previous Dubbos.With the integration of Spring Cloud Alibaba, Dubbo users can enjoy both the performance benefits of RPC and the benefits of Spring Cloud, while Spring Cloud users have a good option in terms of service governance.It can be said that this integration really makes these two large user groups get a good integration and play a role of mutual achievement.More about the Spring Cloud and Spring Cloud Alibaba tutorials are available Click to view.

Reference material: Official Documents

Code Samples

This article introduces client code for content that can be viewed by example readers by looking at the alibaba-dubbo-api, alibaba-dubbo-server, alibaba-dubbo-client projects in the following repositories:

If you are interested in these, you are welcome to support star, Follow, Collection, Forwarding!>Welcome to my public number, Programmed Ape DD, for exclusive learning resources and daily dry delivery.If you are interested in my topic, you can also follow my blog: didispace.com

Posted by RecoilUK on Sun, 18 Aug 2019 19:42:39 -0700