Dubbo初体验

262 阅读2分钟

前言

最近打算学习RPC框架,于是就开始学习Dubbo,并做了一个简单的demo(关于Dubbo介绍,大家可以去看看官方文档),demo的全部代码放到了GitHub上。接下来依次介绍工程的各个模块(工程结构模仿了Dubbo官方demo),采用Maven构建。

demo-dubbo

父工程,统一各个依赖的版本,pom内容如下:

<?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>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>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>
  • Spring Boot:版本2.4.1
  • Dubbo:版本2.7.8
  • JedisRedisJava客户端,版本2.9.0。在这个demo中,采用Redis作为注册中心,并且在这个版本的Dubbo中,Jedis版本不能太高,否则不兼容。

demo-dubbo-api

这个模块,我的理解是暴露提供服务的相关接口,pom内容如下:

<?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>api</artifactId>
    <version>${parent.version}</version>
    <name>api</name>
    <description>Demo project for Dubbo</description>
</project>

定义了一个demo接口对外提供服务:

package cn.tx.demo.dubbo.api;

/**
 * @author rookie-tx
 * @version 1.0.0 2021/1/13
 */
public interface DemoService {

    /**
     * demo
     * @return hello dubbo
     */
    String demo();
}

demo-dubbo-provider

服务提供者,实现了DemoService接口,pom内容如下:

<?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 project for 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>

实现类如下:

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
 * @version 1.0.0 2021/1/13
 */
@Service
@DubboService
public class ProviderServiceImpl implements DemoService {

    @Override
    public String demo() {
        return "hello dubbo";
    }
}

启动类如下:

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
 * @version 1.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);
    }
}

要添加DubboComponentScan注解,并指定服务提供类所在的包位置。配置文件如下:

dubbo:
  application:
    name: demo-dubbo-provider
  registry:
    address: redis://127.0.0.1:6379
server:
  port: 8080

在当前版本的Dubbo中,注册中心要么没用户名和密码,要么两者都有,而Redis没有用户名,所以Redis不设置密码,或者你可以自己改一下源码并重新打包。

demo-dubbo-consumer

消费者pom如下:

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

并定义了一个ConsumerService来消费服务,代码如下:

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
 * @version 1.0.0 2021/1/13
 */
@Service
public class ConsumerServiceImpl implements ConsumerService {

    @DubboReference
    private DemoService demoService;

    @Override
    public String consume() {
        return demoService.demo();
    }
}

最后我们定义一个接口,能通过http形式去访问(也可以直接用单元测试的形式):

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
 * @version 1.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() {
        return consumerService.consume();
    }
}

运行

首先启动Redis,然后启动ProviderApplication,启动之后,会发现Redis中多了一些内容:

说明成功的将服务提供者注册到了Redis中,接下来启动ConsumerApplication,启动之后,会发现Redis中又多了一些内容:

接下来访问测试接口返回如下:

至此,demo搭建成功。