preface

Recently, I decided to learn about the RPC framework, so I started to learn about Dubbo and made a simple demo (check out the official documentation for Dubbo). The entire demo code was posted on GitHub. Next, each module of the project (modeled after the official Dubbo demo) is built using Maven.

demo-dubbo

The parent project, which unified all dependent versions, poM content is as follows:


      
<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">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>Against 2.4.1</version>
        <relativePath/> <! -- lookup parent from repository -->
    </parent>
    <groupId>cn.tx.demo</groupId>
    <artifactId>dubbo</artifactId>
    <version>1.0.0</version>
    <name>dubbo</name>
    <description>Demo project for Dubbo</description>
    <packaging>pom</packaging>

    <properties>
        <java.version>11</java.version>
        <dubbo.version>2.7.8</dubbo.version>
        <jedis.version>2.9.0</jedis.version>
    </properties>
    
    <modules>
        <module>api</module>
        <module>provider</module>
        <module>consumer</module>
    </modules>

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

            <dependency>
                <groupId>redis.clients</groupId>
                <artifactId>jedis</artifactId>
                <version>${jedis.version}</version>
            </dependency>
        </dependencies>
    </dependencyManagement>

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

</project>
Copy the code
  • Spring Boot: version against 2.4.1
  • Dubbo: version 2.7.8
  • Jedis:RedisJava client, version 2.9.0. In this demo, useRedisAs a registry, and in this versionDubbo,JedisThe version cannot be too high or it will not be compatible.

demo-dubbo-api

This module, as I understand it, exposes the relevant interfaces that provide the service. The POM content is as follows:


      
<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">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>cn.tx.demo</groupId>
        <artifactId>dubbo</artifactId>
        <version>1.0.0</version>
        <relativePath/> <! -- lookup parent from repository -->
    </parent>
    <groupId>cn.tx.demo.dubbo</groupId>
    <artifactId>api</artifactId>
    <version>${parent.version}</version>
    <name>api</name>
    <description>Demo project for Dubbo</description>
</project>
Copy the code

A demo interface is defined to provide services externally:

package cn.tx.demo.dubbo.api;

/ * * *@author rookie-tx
 * @version1.0.0 2021/1/13 * /
public interface DemoService {

    /**
     * demo
     * @return hello dubbo
     */
    String demo(a);
}
Copy the code

demo-dubbo-provider

Service provider, implements DemoService interface, poM content is as follows:

<? 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">
    <modelVersion>4.0. 0</modelVersion>
    <parent>
        <groupId>cn.tx.demo</groupId>
        <artifactId>dubbo</artifactId>
        <version>1.0. 0</version> <relativePath/> <! -- lookup parent from repository --> </parent> <groupId>cn.tx.demo.dubbo</groupId> <artifactId>provider</artifactId> <version>${parent.version}</version> <name>provider</name> <description>Demo projectfor Dubbo</description>

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

        <dependency>
            <groupId>cn.tx.demo.dubbo</groupId>
            <artifactId>api</artifactId>
            <version>${parent.version}</version>
        </dependency>

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

        <dependency>
            <groupId>redis.clients</groupId>
            <artifactId>jedis</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
        </dependency>
    </dependencies>
</project>
Copy the code

The implementation class is as follows:

package cn.tx.demo.dubbo.provider.service.impl;

import cn.tx.demo.dubbo.api.DemoService;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.stereotype.Service;

/ * * *@author rookie-tx
 * @version1.0.0 2021/1/13 * /
@Service
@DubboService
public class ProviderServiceImpl implements DemoService {

    @Override
    public String demo(a) {
        return "hello dubbo"; }}Copy the code

The startup classes are as follows:

package cn.tx.demo.dubbo.provider;

import org.apache.dubbo.config.spring.context.annotation.DubboComponentScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/ * * *@author rookie-tx
 * @version1.0.0 2021/1/13 * /
@DubboComponentScan("cn.tx.demo.dubbo.provider")
@SpringBootApplication
public class ProviderApplication {

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

Add the DubboComponentScan annotation and specify the package location where the service provider class resides. The configuration file is as follows:

dubbo:
  application:
    name: demo-dubbo-provider
  registry:
    address: Redis: / / 127.0.0.1:6379
server:
  port: 8080
Copy the code

In the current version of Dubbo, the registry either doesn’t have a username or password, or both, whereas Redis doesn’t have a username, so Redis doesn’t have a password, or you can change the source code and repackage it yourself.

demo-dubbo-consumer

Consumer POM is as follows:


      
<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">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>cn.tx.demo</groupId>
        <artifactId>dubbo</artifactId>
        <version>1.0.0</version>
        <relativePath/> <! -- lookup parent from repository -->
    </parent>
    <groupId>cn.tx.demo.dubbo</groupId>
    <artifactId>consumer</artifactId>
    <version>${parent.version}</version>
    <name>consumer</name>
    <description>Demo project for Dubbo</description>

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

        <dependency>
            <groupId>cn.tx.demo.dubbo</groupId>
            <artifactId>api</artifactId>
            <version>${parent.version}</version>
        </dependency>

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

        <dependency>
            <groupId>redis.clients</groupId>
            <artifactId>jedis</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
</project>
Copy the code

And defines a ConsumerService to consume the service as follows:

package cn.tx.demo.dubbo.consumer.service.impl;

import cn.tx.demo.dubbo.api.DemoService;
import cn.tx.demo.dubbo.consumer.service.ConsumerService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;

/ * * *@author rookie-tx
 * @version1.0.0 2021/1/13 * /
@Service
public class ConsumerServiceImpl implements ConsumerService {

    @DubboReference
    private DemoService demoService;

    @Override
    public String consume(a) {
        returndemoService.demo(); }}Copy the code

Finally, we define an interface that can be accessed via HTTP (or directly as a unit test) :

package cn.tx.demo.dubbo.consumer.controller;

import cn.tx.demo.dubbo.consumer.service.ConsumerService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/ * * *@author rookie-tx
 * @version1.0.0 2021/1/13 * /
@RestController
@RequestMapping("/api/v1/consumer")
public class ConsumerController {

    private final ConsumerService consumerService;

    public ConsumerController(ConsumerService consumerService) {
        this.consumerService = consumerService;
    }

    @GetMapping("/dubbo/test")
    public String testDubbo(a) {
        returnconsumerService.consume(); }}Copy the code

run

First startRedisAnd then startProviderApplicationAfter the startup, it will be foundRedisSome more content in:

The service provider has been successfully registeredRedis, then startConsumerApplicationAfter the startup, it will be foundRedisHere are some more:

Next access to the test interface returns the following:

The demo is successfully built.