乐趣区

关于后端:如何基于-Spring-Boot-快速开发一个-Dubbo-微服务应用

Dubbo 还提供了包含 XML、API 等多种启动与接入形式,更多开发方式和配置细节可参见配置手册。

下载示例代码

残缺示例代码在 dubbo-samples 中。

  1. 下载源码

    git clone -b master https://github.com/apache/dubbo-samples.git
  2. 进入示例目录

    cd dubbo-samples/1-basic/dubbo-samples-spring-boot
    ls # 查看目录构造

疾速运行示例

  1. 编译 Provider
    在 dubbo-samples-spring-boot 目录执行 maven 命令

    mvn clean package
  2. 运行 Provider
    进入 dubbo-samples-spring-boot-provider/target 目录并启动 java 过程

    cd ./dubbo-samples-spring-boot-provider
    java -jar ./target/dubbo-samples-spring-boot-provider-1.0-SNAPSHOT.jar
  3. 运行 consumer
    进入 dubbo-samples-spring-boot-consumer 目录并启动 java 过程

    java -jar ./target/dubbo-samples-spring-boot-consumer-1.0-SNAPSHOT.jar
  4. 查看后果
    在 consumer 侧会输入如下信息:

    result: Hello World

    在 provider 侧会输入如下信息:

    Hello World, request from consumer: xxx.xxx.xxx.xxx

至此就把 Dubbo 的基本功能都实现了,能够在 Dubbo 的根底上进行更多的开发。

示例外围流程

对于示例的更具体解读可参见 注解配置

1. 定义服务接口

dubbo-samples-spring-boot-interface/DemoService.java

package org.apache.dubbo.samples.basic.api;

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

2. 提供方实现接口并裸露服务

dubbo-samples-spring-boot-provider/DemoServiceImpl.java

@DubboService
public class DemoServiceImpl implements DemoService {
    @Override
    public String sayHello(String name) {System.out.println("Hello" + name + ", request from consumer:" + RpcContext.getContext().getRemoteAddress());
        return "Hello" + name;
    }
}

3. 配置 application.yml 文件

dubbo-samples-spring-boot-provider/resources/application.yml

dubbo:
  application:
    name: dubbo-springboot-demo-provider
  protocol:
    name: dubbo
    port: -1
  registry:
    id: zk-registry
    address: zookeeper://127.0.0.1:2181
  config-center:
    address: zookeeper://127.0.0.1:2181
  metadata-report:
    address: zookeeper://127.0.0.1:2181

4. 定义 Spring Boot 主函数

dubbo-samples-spring-boot-provider/ProviderApplication.java

@SpringBootApplication
@EnableDubbo
public class ProviderApplication {public static void main(String[] args) throws Exception {new EmbeddedZooKeeper(2181, false).start();

        SpringApplication.run(ProviderApplication.class, args);
        System.out.println("dubbo service started");
        new CountDownLatch(1).await();}
}

其中,@EnableDubbo 必须配置。

5. 援用近程服务

dubbo-samples-spring-boot-consumer/ConsumerApplication.java

public class ConsumerApplication {
    @DubboReference
    private DemoService demoService;
}

6. 定义 application.yml

dubbo-samples-spring-boot-consumer/application.yml

dubbo:
  application:
    name: dubbo-springboot-demo-consumer
  protocol:
    name: dubbo
    port: -1
  registry:
    id: zk-registry
    address: zookeeper://127.0.0.1:2181
  config-center:
    address: zookeeper://127.0.0.1:2181
  metadata-report:
    address: zookeeper://127.0.0.1:2181

7. 加载 Spring 配置,并调用近程服务

dubbo-samples-spring-boot-consumer/ConsumerApplication.java

@SpringBootApplication
@Service
@EnableDubbo
public class ConsumerApplication {
    @DubboReference
    private DemoService demoService;
    
    public String doSayHello(String name) {return demoService.sayHello(name);
    }

    public static void main(String[] args) {ConfigurableApplicationContext context = SpringApplication.run(ConsumerApplication.class, args);
        ConsumerApplication application = context.getBean(ConsumerApplication.class);
        String result = application.doSayHello("world");
        System.out.println("result:" + result);
    }
}

其中,@EnableDubbo 必须配置。

原文首于 Dubbo 官网:https://cn.dubbo.apache.org/z…
欢送在 https://github.com/apache/dubbo 给 Dubbo Star。
搜寻关注官网微信公众号:Apache Dubbo,理解更多业界最新动静,把握大厂面试必备 Dubbo 技能

退出移动版