快速搭建基于注解的 Dubbo 项目

版本

  1. JDK 14
  2. Gradle 6.3
  3. Dubbo 2.7.7
  4. ZooKeeper 3.6.0

注册中心安装

首先确保你的环境中有 Docker,输入下列命令进行 Zookeeper 安装。

docker run -p 2181:2181 -d --name=zookeeper  zookeeper

可视化管理控制台搭建

首先确保你的环境有 GitMaven,然后依次输入下列命令。

git clone https://github.com/apache/dubbo-admin.git

cd dubbo-admin

mvn clean package -Dmaven.test.skip=true

cd dubbo-admin-distribution/target

java -jar dubbo-admin-0.2.0-SNAPSHOT.jar

成功运行之后访问 127.0.0.1:8080,出现以下界面说明管理控制台搭建成功。

创建接口工程

打开 IDEA 创建一个基础的 Gradle Module Project,创建服务接口类。

package com.example.api.service;

public interface HelloWorldService {
    public String sayHelloTo(String developer);
}

将接口工程项目打包成 Jar 包,一会儿提供给服务提供者和消费者使用。

创建服务提供者

  1. 创建一个基本的 SpringBoot 项目,引入 Spring Web 模块。
  2. 在项目目录下创建 libs 文件夹,将上面的接口工程 Jar 包放入 libs 文件夹。
  3. 打开 build.gradle 文件,写入下列配置。
repositories {
    mavenCentral()
    flatDir {
        dirs 'libs'
    }
}

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-web'
    testImplementation('org.springframework.boot:spring-boot-starter-test') {
        exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
    }

    implementation 'org.apache.dubbo:dubbo-spring-boot-starter:2.7.7'
    implementation 'org.apache.curator:curator-framework:4.3.0'
    implementation 'org.apache.curator:curator-recipes:4.3.0'

    implementation 'com.example:api:1.0.0'
}
  1. 创建 HelloWorldServiceImpl 实现 HelloWorldService 接口。
package com.example.provider.service.impl;

import com.example.api.service.HelloWorldService;
import org.apache.dubbo.config.annotation.DubboService;

@DubboService
public class HelloWorldServiceImpl implements HelloWorldService {
    @Override
    public String sayHelloTo(String developer) {
        return "Hello World " + developer;
    }
}
  1. 给启动类加上 Dubbo 注解。
package com.example.provider;

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

@SpringBootApplication
@EnableDubbo(scanBasePackages = "com.example.provider.service.impl")
public class ProviderApplication {

    public static void main(String[] args) {
        SpringApplication.run(ProviderApplication.class, args);
    }
}
  1. 配置 Dubbo 相关配置。
spring.application.name=com.example.provider
server.port=8082
dubbo.application.name=com.example.provider
dubbo.registry.address=zookeeper://127.0.0.1:2181

创建服务消费者

  1. 前三步与服务提供者相同。
  2. 创建 HelloWorldController 测试功能。
package com.example.consumer.controller;

import com.example.api.service.HelloWorldService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloWorldController {

    @DubboReference
    private HelloWorldService helloWorldService;

    @GetMapping("/hello")
    public String sayHello(String developer) {
        return helloWorldService.sayHelloTo(developer);
    }
}
  1. 配置 Dubbo 相关配置。
spring.application.name=com.example.consumer
server.port=8081
dubbo.application.name=com.example.consumer
dubbo.registry.address=zookeeper://127.0.0.1:2181
  1. 打开浏览器输入 127.0.0.1:8081/hello?developer=xxxx 进行访问~

控制台显示结果

到这里我们就完成了一个最基础的 Dubbo Demo Project!点击获取示例代码

本作品采用《CC 协议》,转载必须注明作者和本文链接
Hello。
讨论数量: 0
(= ̄ω ̄=)··· 暂无内容!

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!