Spring Cloud Alibaba With Dubbo

Spring Cloud Alibaba With Dubbo

Spring Cloud Alibaba

Spring Cloud Alibaba is Alibaba Cloud’s version of Spring Cloud. It consists of several Alibaba’s open source projects, i.e.  Nacos, Sentinel and RocketMQ, as well as several Alibaba cloud-native commercial products to enhance user’s experience on Alibaba Cloud. The new version of Spring Cloud Alibaba will also offer Dubbo as an RPC choice.

Dubbo is a battle-hardened RPC framework. In another article, I have demonstrated how to use it with annotation. That example is using Spring Boot. Dubbo is closely integrated with Spring Boot. Putting Dubbo on Spring Cloud Alibaba seems like a natural fit.

In this article, we’ll use a simple echo example to illustrate the steps to use Dubbo on Spring Cloud Alibaba.

Define Dubbo Interface

Let’s define an interface first:

public interface EchoService {

    String echo(String message);
}

This interface will be exposed to the remote clients. A tip here is to package this interface into a second- or third-party artifact (jar), so that the jar will be available to  spring-cloud-dubbo-sample-api  .

Implement a Dubbo Service

Create a Project in Maven

Let’s create a Maven project with artifactId spring-cloud-dubbo-server-sample. Then we’ll add the dependencies in the pom.xml.

<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>
</dependencies>

In the above pom.xml:

  •  spring-cloud-dubbo-sample-api: This is the artifact ID
  •  spring-boot-actuator: This is the Spring Boot production-ready artifact
  •  spring-cloud-starter-dubbo: This is the Dubbo Spring Cloud Starter artifact
  •  spring-cloud-starter-alibaba-nacos-discovery: This is the Nacos Spring Cloud service registry artifact

Before we move on, we need to add a version to the definitions: 

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

The Service Code Implementation

This is the Java code of implementation:

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

The  @org.apache.dubbo.config.annotation.Service annotation declares this as a Dubbo service.

Configure Dubbo Service

The recommended way of exposing the service is to use the @DubboComponentScanannotation.

There are two parts involved in the configuration: Dubbo and Spring Cloud.

dubbo:
  scan:
    base-packages: org.springframework.cloud.alibaba.dubbo.bootstrap
  protocol:
    name: dubbo
    # -1 means self-defined
    port: -1
  registry:
    address: spring-cloud://localhost


spring:
  application:
    name: spring-cloud-alibaba-dubbo-server
  main:
    allow-bean-definition-overriding: true
  cloud:
    nacos:
      # Nacos
      discovery:
        server-addr: 127.0.0.1:8848

Spring Boot Application class

This is the same as the other Spring Boot application class:

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

The only thing worth mentioning here is that we should start the Nacos service before this, so that the service can be discovered by Nacos.

Implement the Dubbo Client

Create spring-cloud-dubbo-client-sample Maven Project

Similar to the service side configuration, we need to specify dependencies:

<dependencyManagement>
    <dependencies>
        <!-- Spring Cloud Alibaba dependencies -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-alibaba-dependencies</artifactId>
            <version>0.9.0.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>
</dependencies>

The main difference from the service side is the client will be a Web Servlet application using spring-boot-starter-web .

Configure the Client

Similar to the service side, there are two parts of configuration: Dubbo and Spring.

dubbo:
  registry:
    address: spring-cloud://localhost
  cloud:
    subscribed-services: spring-cloud-alibaba-dubbo-server
spring:
  application:
    # Dubbo
    name: spring-cloud-alibaba-dubbo-client
  main:
    # Spring Boot 2.1
    allow-bean-definition-overriding: true
  cloud:
    nacos:
      # Nacos
      discovery:
        server-addr: 127.0.0.1:8848

here we specify the service to consume by binding dubbo.cloud.subscribed-services to  spring-cloud-dubbo-server-sample.

  •  dubbo.cloud.subscribed-services  : To subscribe to multiple service, use “,”as delimiter.

Since it is a Web application, the default port is  8080 . That can be changed by modifying the server.port attribute.

Spring Boot Application Class and Implement Java Code 

Two steps in 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);
    }
}

Since this is a web client, we can use curl to try it out. If we run

curl http://127.0.0.1:8080/echo?message=yourmessage

Or we can run the Java client to get the same result.

We should see the result [echo] Hello, yourmessage

Conclusion

Spring Cloud Alibaba now has two releases that have included Dubbo. They are release 0.2.2, which is compatible with Spring Cloud Finchley, and release 0.9.0, which is compatible with Spring Cloud Greenwich. Whether you are a Dubbo user trying to adapt to Spring Cloud Alibaba or vice versa, the experience should be seamless.

from DZone Cloud Zone

Sharing is caring!

Comments are closed.