This project demonstrates how to use Spring Cloud Alibaba to complete RPC calls to Dubbo.

Spring Cloud and Dubbo

  • Spring Cloud is a complete set of microservices architecture solutions

  • Dubbo is a very popular service governance and RPC implementation scheme in China

Dubbo has a large user base in China, but its surrounding facilities and components are relatively incomplete (such as Feign, Ribbon, etc.). Many developers use Dubbo and want to enjoy the Spring Cloud ecosystem, so there will be some examples and methods of using Spring Cloud with Dubbo.

The emergence of Spring Cloud Alibaba has realized the perfect integration of Spring Cloud and Dubbo. In previous tutorials, we have described using Nacos in Spring Cloud Alibaba as a service registry, from which we can consume services using the Ribbon or Feign just like traditional Spring Cloud applications. In this article, we will continue to talk about the additional RPC scheme supported under Spring Cloud Alibaba: Dubbo

Code implementation

Let’s take a simple example of using Nacos as a service registry and Dubbo as a service provider and a service consumer. The installation and use of Nacos are omitted here, and the steps of using Dubbo are directly entered.

Define the Dubbo service interface

Create the Ali-nacos-Dubbo-API project

A Dubbo service interface is a remote communication contract between a service provider and a consumer, usually declared by a plain Java interface, such as HelloService:

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

To ensure contract consistency, it is recommended that the Dubbo service interface be packaged in a JAR package, such as ali-nacos-Dubbo-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.

Implement Dubbo service provider

Create ali-nacos-Dubbo-Provider, port: 9001 project

Pom. The XML configuration

<?xml version="1.0" encoding="UTF-8"? >
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>cloud-alibaba</artifactId>
        <groupId>com.easy</groupId>
        <version>1.0.0</version>
    </parent>

    <modelVersion>4.0.0</modelVersion>

    <artifactId>ali-nacos-dubbo-provider</artifactId>

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

        <! -- API -->
        <dependency>
            <groupId>com.easy</groupId>
            <artifactId>ali-nacos-dubbo-api</artifactId>
            <version>${project.version}</version>
        </dependency>

        <! --dubbo-->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-dubbo</artifactId>
        </dependency>

        <! --nacos-->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>
Copy the code

The bootstrap. Yaml configuration

dubbo:
  scan:
    Dubbo service scan benchmark
    base-packages: com.easy.andProvider.service

  #Dubbo service exposed protocol configuration, where subattribute name is protocol name, port is protocol port (-1 indicates automatic port, starting from 20880)
  protocol:
    name: dubbo
    port: - 1

  #Dubbo service registry configuration, where the value of the subattribute address is "spring-Cloud ://localhost", indicating mounting to the Spring Cloud registry
  registry:
    address: spring-cloud://localhost

spring:
  application:
    # Dubbo app name
    name: ali-nacos-dubbo-provider
  main:
    allow-bean-definition-overriding: true
  cloud:
    # Nacos service discovery and registration configuration
    nacos:
      discovery:
        server-addr: 127.0. 01.: 8848
Copy the code

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

Implement the Dubbo service

HelloService as the exposed Dubbo service interface, the service provider Ali-Nacos-Dubbo-provider needs to implement it:

package com.easy.andProvider.service;

import com.easy.and.api.service.HelloService;
import org.apache.dubbo.config.annotation.Service;

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

Import org. Apache. Dubbo. Config. The annotation. The Service is the dubbo Service annotations, declare the Java implementation to dubbo services

Paste the startup class code:

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

Realize Dubbo service consumer

Create ali-nacos-Dubo-consumer, port: 9103 project

Pom. XML dependency

<?xml version="1.0" encoding="UTF-8"? >
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>cloud-alibaba</artifactId>
        <groupId>com.easy</groupId>
        <version>1.0.0</version>
    </parent>

    <modelVersion>4.0.0</modelVersion>

    <artifactId>ali-nacos-dubbo-consumer</artifactId>

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

        <dependency>
            <groupId>com.easy</groupId>
            <artifactId>ali-nacos-dubbo-api</artifactId>
            <version>${project.version}</version>
        </dependency>

        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-dubbo</artifactId>
        </dependency>

        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>
Copy the code

Yaml configuration file

dubbo:
  registry:
    address: spring-cloud://localhost
  cloud:
    subscribed-services: ali-nacos-dubbo-provider

spring:
  application:
    name: ali-nacos-dubbo-consumer
  main:
    allow-bean-definition-overriding: true
  cloud:
    nacos:
      discovery:
        server-addr: 127.0. 01.: 8848
Copy the code

Realize Dubbo service consumer

HomeController.java

package com.easy.andConsumer;

import com.easy.and.api.service.HelloService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Slf4j
public class HomeController {

    @Reference
    HelloService helloService;

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

AndConsumerApplication. Java start class

package com.easy.andConsumer;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

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

Use the sample

Sample associated project

In this example, we created three project implementations

  • Ali-nacos-dubbo – API: Defines the Dubbo service interface project

  • Ali-nacos-dubbo-provider: The dubbo service provider registers the service with NACOS. The service name is Ali-nacos-Dubbo-Provider and the port is 9001

  • Ali-nacos-dubbo-consumer: Dubbo service consumer and register the service with NACOS, service name: Ali-Nacos-Dubbo-Consumer, port: 9103

Run the sample tests

The service registry NACOS, Ali-Nacos-Dubbo-Provider service and Ali-Nacos-Dubbo-Consumer service should be started first

  • Access the service consumer address: http://localhost:9103/hello

return

Hello yunCopy the code

Alternatively, you can use the curl command to perform HTTP GET

$curl http://127.0.0.1:9103/hello
Copy the code

The HTTP response is:

Hello yunCopy the code

The above results indicate that ali-nacos-Dubbo-consumer returns the contents of ali-nacos-Dubbo-provider calculation by consuming dubbo service.

Above, we have completed the introduction of Dubbo service providers and consumers. Please refer to the module directly for the source code:

  • ali-nacos-dubbo-provider
  • ali-nacos-dubbo-consumer

data

  • Spring Cloud Alibaba example source code
  • The original address