Getting started with Dubbo development

1. Generate project scaffolding through templates

Dubbo Initializer  can be used to quickly generate Java project scaffolding to help simplify microservice project construction, basic configuration, component dependency management, etc.

The Initializer is still being updated, and more Dubbo Feature support will be released one after another.

Select Dubbo version

Initializer will be used to  dubbo-spring-boot-starter create a Spring Boot project, so we first need to choose the version of Dubbo and Spring Boot.

initializer-choose-version

Enter the basic information of the project

Next, fill in the basic information of the project, including project coordinates, project name, package name, JDK version, etc.

initializer-project-info

Choose project structure

There are two project structures to choose from, namely  单模块 and  多模块, in this example we choose  单模块.

initializer-project-architecture

  • Single module, all component codes are stored in one module, characterized by simple structure.
  • Multi-module, the generated project has  APItwo Service modules, which  API are used to store Dubbo service definition, and Service are used to store service implementation or call logic. Usually multi-module is more conducive to separate management and release of service definition.

Select dependent components

We select the following dependent components for the template by default:

  • Dubbo components
    • Java Interface
    • registry, zookeeper
    • ProtocolTCP
  • Common microservice components
    • Web
    • My shoe
    • template engine

initializer-dependencies

Based on the above options, the generated project will use Zookeeper as the registration center, use the high-performance Dubbo2 TCP protocol as the RPC communication protocol, and add dependencies and examples for components such as Web and Mybatis.

Note: The Dubbo components selected above are also default options, that is, without manually adding any dependencies, click Code Generation directly after opening the page, and the generated code will contain the above Dubbo components.

If you manually add dependent components, please pay attention to the implicit combination relationship restrictions between Dubbo's dependent components, such as

  • If [Dubbo Service API]-[IDL] is selected, currently only the [HTTP/2] or [gRPC] protocol in [Dubbo Protocol] is supported.
  • Under the same dependency group, only one of the same type of dependencies can be selected. For example, under the [Dubbo Registry&Config&Metadata] group, only one of [Zookeeper] and [Nacos] can be selected from the perspective of the registration center. Modify the configuration manually in the code. But the registration center and the configuration center can be selected separately, for example, Zookeeper and Apollo can be selected at the same time.

Generate project template

  • Click "Browse Code" to browse the project structure and code online
  • Click "Get Code" to generate the project download URL

initializer-preview

After the project is downloaded to the local, after decompressing and importing into the IDE, you can develop custom Dubbo applications as needed.

2. Development Services

publish and call

Through a simple Springboot example code, it shows the release and invocation of Dubbo service

This article will demonstrate how to quickly build and deploy a microservice application based on Dubbo Samples. Code address: dubbo-samples-develop  The code is divided into three modules

  • api
  • develop-provider
  • develop-consumer

Prepare

This sample code is based on Springboot 3.0

1. First, an available registration center Zookeeper, Nacos, and Redis are all available.

2. Create a new maven project and add the following dependencies

        <!-- registry dependency -->
        <dependency>
            <groupId>com.alibaba.nacos</groupId>
            <artifactId>nacos-client</artifactId>
            <version>${nacos.version}</version>
        </dependency>

        <!-- dubbo dependency-->
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo-spring-boot-starter</artifactId>
            <version>${dubbo.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

The registration center used in this example is Nacos. To use ZK, please replace the nacos-client package with the corresponding version of the zk client package.

publish service

1. Define the service interface

public interface DevelopService {
    String invoke(String param);
}

2. Implementation of service interface

@DubboService(group = "group1",version = "1.0")
public class DevelopProviderServiceV1 implements DevelopService{
    @Override
    public String invoke(String param) {
        StringBuilder s = new StringBuilder();
        s.append("ServiceV1 param:").append(param);
        return s.toString();
    }
}

Using the @DubboService annotation, Dubbo will register the corresponding service to spring, call the corresponding service export method after spring starts, and register the service to the registration center, so that the consumer can discover the service we published and call it

3. Add Dubbo configuration

Add application.properties related configuration, or create a new dubbo.properties to save dubbo related configuration, the content is as follows:

dubbo.application.name=provider

# Enable token verification for each invocation
dubbo.provider.token=false

# Specify the registry address
# dubbo.registry.address=nacos://localhost:8848?username=nacos&password=nacos
dubbo.registry.address=nacos://${nacos.address:localhost}:8848?username=nacos&password=nacos

dubbo.protocol.name=dubbo
dubbo.protocol.port=20881

4. Start the service

To create a Springboot startup class, you need to add the @EnableDubbo annotation to enable the Dubbo automatic configuration function

@EnableDubbo
@SpringBootApplication
public class DevelopApplication {

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

After the startup is successful, you can see the corresponding service list in the registration center, as shown in the figure: ![serviceList](/imgs/v3/develop/develop-service-list.png)

call service

Create the DemoTask class, and introduce the services that need to be called through the @DubboReference annotation. The remote service can be called like a local method.

//实现CommandLineRunner 让Springboot启动后调用run方法
@Component
public class DemoTask implements CommandLineRunner {
    @DubboReference(group = "group1",version = "1.0")
    private DevelopService developService;

    @Override
    public void run(String... args) throws Exception {
        //调用DevelopService的group1分组实现
        System.out.println("Dubbo Remote Return ======> " + developService.invoke("1"));
    }
}

start service print

Dubbo Remote Return ======> ServiceV1 param:1

Indicates that the service call was successful

Guess you like

Origin blog.csdn.net/leesinbad/article/details/132443283