Dubbo快速入门(二):第一个Dubbo程序(附源码)

文章目录

本博客配套源码:gitlab仓库

首先,在服务器上部署zookeeper并运行,可以参考我的另一篇教程:https://blog.csdn.net/Tracycoder/article/details/142792750

注意事项:

  • 生产者、消费者中服务的包路径一定要一致,不然会导致注册和消费失败!
  • 先启动生产者,再启动消费者!不然会启动失败!
  • 各依赖的版本一定要兼容,不然项目会启动失败!

一定要注意以上几点,踩了几个小时的坑,说多了都是泪!

一、生产者工程

0.目录结构

1.依赖

pom.xml

xml 复制代码
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.example</groupId>
    <artifactId>dubbo_study_provider</artifactId>
    <version>1.0-SNAPSHOT</version>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.5.4</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <dependencies>
        <!-- Spring Boot Starter -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <!-- Dubbo Starter -->
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo-spring-boot-starter</artifactId>
            <version>3.2.0</version>
        </dependency>

        <!-- Zookeeper Client -->
        <dependency>
            <groupId>org.apache.curator</groupId>
            <artifactId>curator-framework</artifactId>
            <version>5.4.0</version>
        </dependency>

        <dependency>
            <groupId>org.apache.curator</groupId>
            <artifactId>curator-recipes</artifactId>
            <version>5.4.0</version>
        </dependency>

        <dependency>
            <groupId>org.javassist</groupId>
            <artifactId>javassist</artifactId>
            <version>3.29.2-GA</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.apache.curator/curator-x-discovery-server -->
        <dependency>
            <groupId>org.apache.curator</groupId>
            <artifactId>curator-x-discovery-server</artifactId>
            <version>5.4.0</version>
        </dependency>


        <!-- Spring Boot Test Starter -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

</project>

2.配置文件

applicaiton.yml

yml 复制代码
server:
  port: 8080

# Dubbo
dubbo:
  application:
    name: dubbo_provider
  registry:
    address: zookeeper://你的zookeeperIP:2181
  protocol:
    name: dubbo
    port: 20880

3.启动类

java 复制代码
package tracy;

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

@SpringBootApplication
@EnableDubbo(scanBasePackages = "tracy.provider.service")
public class ProviderApplication {

    public static void main(String[] args) {
        System.setProperty("zookeeper.sasl.client", "false");
        SpringApplication.run(ProviderApplication.class, args);
        System.out.println("provider服务启动成功!!!");
    }
}

4.生产者服务

java 复制代码
package tracy.provider.service;

public interface HelloService {
    String sayHello(String name);
}
java 复制代码
package tracy.provider.service;

import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.stereotype.Component;


@DubboService(version = "1.0.0")
@Component
public class HelloServiceImpl implements HelloService {
    @Override
    public String sayHello(String name) {
        return "hello "+name+"!";
    }
}

二、消费者工程

0.目录结构

1.依赖

pom.xml

xml 复制代码
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.example</groupId>
    <artifactId>dubbo_study_consumer</artifactId>
    <version>1.0-SNAPSHOT</version>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.5.4</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <dependencies>
        <!-- Spring Boot Starter -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!-- Dubbo Starter -->
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo-spring-boot-starter</artifactId>
            <version>3.2.0</version>
        </dependency>

        <!-- Zookeeper Client -->
        <dependency>
            <groupId>org.apache.curator</groupId>
            <artifactId>curator-framework</artifactId>
            <version>5.4.0</version>
        </dependency>

        <dependency>
            <groupId>org.apache.curator</groupId>
            <artifactId>curator-recipes</artifactId>
            <version>5.4.0</version>
        </dependency>

        <dependency>
            <groupId>org.javassist</groupId>
            <artifactId>javassist</artifactId>
            <version>3.29.2-GA</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.apache.curator/curator-x-discovery-server -->
        <dependency>
            <groupId>org.apache.curator</groupId>
            <artifactId>curator-x-discovery-server</artifactId>
            <version>5.4.0</version>
        </dependency>


        <!-- Spring Boot Test Starter -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

</project>

2.配置文件

application.yml

yml 复制代码
server:
  port: 8081

# Dubbo
dubbo:
  application:
    name: dubbo_consumer
  registry:
    address: zookeeper://你的zookeeperIP:2181
  consumer:
    check: false  # 设置为 false,避免消费者启动时检查提供者状态

3.启动类

java 复制代码
package tracy;

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

@SpringBootApplication
@EnableDubbo
public class ConsumerApplication {

    public static void main(String[] args) {
        System.setProperty("zookeeper.sasl.client", "false");
        SpringApplication.run(ConsumerApplication.class, args);
        System.out.println("consumer服务启动成功!!!");
    }
}

4.服务接口

java 复制代码
package tracy.provider.service;

public interface HelloService {
    String sayHello(String name);
}

5.controller接口

java 复制代码
package tracy.controller;

import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import tracy.provider.service.HelloService;

@RestController
@RequestMapping("/hello")
public class HelloController {

    @DubboReference  // 使用 DubboReference 注解引用远程服务
    private HelloService helloService;

    @GetMapping("/sayHello")
    public String sayHello(@RequestParam String name) {
        return helloService.sayHello(name);
    }
}

三、测试代码

先启动zookeeper,然后启动provider,最后启动consumer。

然后在接口测试工具中访问接口:

访问成功!

相关推荐
珹洺1 天前
Bootstrap-HTML(二)深入探索容器,网格系统和排版
前端·css·bootstrap·html·dubbo
量子炒饭大师3 天前
【C++入门】—— 【什么时候需要用到深拷贝】C++的类中何时需要用到深拷贝?保姆级别带你罗列所有可能!
java·c++·dubbo·深拷贝
橘颂TA5 天前
【测试】自动化测试函数介绍——web 测试
python·功能测试·selenium·测试工具·dubbo
wc885 天前
dubbo本地直连一个程序既当提供者又当消费者问题
dubbo
小信丶7 天前
@Activate 注解详解:应用场景与实战示例
java·spring boot·后端·spring·spring cloud·微服务·dubbo
不像程序员的程序媛8 天前
nacos作为dubbo服务注册中心
dubbo
BraveHeart!10 天前
@DubboService 与 @DubboReference 初始化过程
dubbo
信创天地11 天前
从 “替代” 到 “超越”:信创系统架构师如何筑牢自主可控技术底座
运维·安全·系统架构·开源·dubbo·risc-v
wniuniu_11 天前
运维运维运维
java·运维·dubbo
量子炒饭大师12 天前
【C++入门】Cyber骇客构造器的核心六元组 —— 【类的默认成员函数】明明没写构造函数也能跑?保姆级带你掌握六大类的默认成员函数(上:函数篇)
开发语言·c++·dubbo·默认成员函数