第一种--快速创建应用
可以直接下载示例项目,链接:https://github.com/apache/dubbo-samples/tree/master/11-quickstart
第二种--新建项目
新建 Java 空白 Maven 项目
- jdk17
之后我们还需要创建 dubbo-spring-boot-demo-interface
、dubbo-spring-boot-demo-provider
和 dubbo-spring-boot-demo-consumer
三个子模块。
在父项目的pom.xml
中 添加:
<groupId>org.example</groupId><artifactId>dubbo-spring-boot-demo</artifactId><version>1.0-SNAPSHOT</version><packaging>pom</packaging><modules><module>dubbo-spring-boot-demo-interface</module><module>dubbo-spring-boot-demo-provider</module><module>dubbo-spring-boot-demo-consumer</module></modules><properties><dubbo.version>3.2.0-beta.4</dubbo.version><spring-boot.version>2.7.8</spring-boot.version><maven.compiler.source>17</maven.compiler.source><maven.compiler.target>17</maven.compiler.target><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding></properties><dependencyManagement><dependencies><!-- Spring Boot --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-dependencies</artifactId><version>${spring-boot.version}</version><type>pom</type><scope>import</scope></dependency><!-- Dubbo --><dependency><groupId>org.apache.dubbo</groupId><artifactId>dubbo-bom</artifactId><version>${dubbo.version}</version><type>pom</type><scope>import</scope></dependency><dependency><groupId>org.apache.dubbo</groupId><artifactId>dubbo-dependencies-zookeeper-curator5</artifactId><version>${dubbo.version}</version><type>pom</type></dependency></dependencies></dependencyManagement><build><pluginManagement><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId><version>${spring-boot.version}</version></plugin></plugins></pluginManagement></build>
在consumer
和provider
模块的pom.xml
中 添加:
<dependencies><dependency><groupId>org.example</groupId><artifactId>dubbo-spring-boot-demo-interface</artifactId><version>${project.parent.version}</version></dependency><!-- dubbo --><dependency><groupId>org.apache.dubbo</groupId><artifactId>dubbo-spring-boot-starter</artifactId></dependency><dependency><groupId>org.apache.dubbo</groupId><artifactId>dubbo-dependencies-zookeeper-curator5</artifactId><type>pom</type><exclusions><exclusion><artifactId>slf4j-reload4j</artifactId><groupId>org.slf4j</groupId></exclusion></exclusions></dependency><!-- spring boot starter --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter</artifactId></dependency></dependencies>
在这份配置中,定义了 dubbo 和 zookeeper(以及对应的连接器 curator)的依赖。
添加了上述的配置以后,先通过mvn clean install
构建interface
模块,之后 可以通过 Mvn - Reload All Maven Projects
刷新依赖。
定义服务接口
服务接口 Dubbo 中沟通消费端和服务端的桥梁。
在 dubbo-spring-boot-demo-interface
模块的 org.apache.dubbo.samples.api
下建立 DemoService
接口
package org.apache.dubbo.springboot.demo;public interface DemoService {String sayHello(String name);
}
在 DemoService
中,定义了 sayHello
这个方法。后续服务端发布的服务通过实现 DemoService
接口,消费端订阅的服务也是围绕这个接口的。
实现接口服务
定义了服务接口之后,可以在服务端这一侧定义对应的实现,这部分的实现相对于消费端来说是远端的实现,本地没有相关的信息。
在DemoServiceImpl
类中添加了 @DubboService
注解,通过这个配置可以基于 Spring Boot 去发布 Dubbo 服务。
配置YAML文件
通过 Spring Boot 的方式配置 Dubbo 的一些基础信息。例如这个例子:定义了 Dubbo 的应用名、Dubbo 协议信息、Dubbo 使用的注册中心地址。
在服务端和消费端分别创建application.yml
文件
# 服务端
dubbo:application:name: dubbo-springboot-demo-providerprotocol:name: dubboport: -1registry:address: zookeeper://${zookeeper.address:127.0.0.1}:2181
------------------------------------------------------------------
# 消费端
dubbo:application:name: dubbo-springboot-demo-consumerprotocol:name: dubboport: -1registry:address: zookeeper://${zookeeper.address:127.0.0.1}:2181
创建Spring启动类
基于 Spring 配置服务端启动类
package org.apache.dubbo.springboot.demo.provider;import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication
@EnableDubbo
public class ProviderApplication {public static void main(String[] args) {SpringApplication.run(ProviderApplication.class, args);}
}
这个服务端,消费端同理。在这启动类里,配置了ProviderApplication
去读取刚才的YAML
配置文件并启动应用。
写一个消费端的服务调用
package org.apache.dubbo.springboot.demo.consumer;import java.util.Date;import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.springboot.demo.DemoService;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;@Component
public class Task implements CommandLineRunner {@DubboReferenceprivate DemoService demoService;@Overridepublic void run(String... args) throws Exception {String result = demoService.sayHello("xf");System.out.println("Receive result ======> " + result);new Thread(()-> {while (true) {try {Thread.sleep(1000);System.out.println(new Date() + " Receive result ======> " + demoService.sayHello("world"));} catch (InterruptedException e) {e.printStackTrace();Thread.currentThread().interrupt();}}}).start();}
}
在这个类中,通过@DubboReference
从 Dubbo
获取了一个 RPC
订阅,这个 demoService
可以像本地调用一样直接调用。在 run方法中创建了一个线程进行调用。
启动应用
- 在启动应用前,需要启动zookeeper当注册中心。
在zookeeper的bin目录下使用命令行启动./zkServer.sh start
- 启动
org.apache.dubbo.samples.provider.Application
,等待一会出现如下图所示的日志(Current Spring Boot Application is await
)即代表服务提供者启动完毕,标志着该服务提供者可以对外提供服务了。
- 启动
org.apache.dubbo.samples.client.Application
,等待一会出现如下图所示的日志(Hello world
)即代表服务消费端启动完毕并调用到服务端成功获取结果。