Java 语言定义服务
示例使用 Spring XML 配置方式进行演示。
除此之外,Dubbo Java 还提供了包括注解、API、配置文件、spring boot等多种启动与接入方式,具体可参见配置章节具体描述。
下载示例代码
示例代码在 dubbo-samples 中
- 下载源码
$ git clone -b master https://github.com/apache/dubbo-samples.git
- 进入示例目录
$ cd dubbo-samples/dubbo-samples-basic
快速运行示例
在 dubbo-samples-basic 目录
- 编译 Provider
$ mvn clean compile -Pprovider
- 运行 Provider
$ java -jar ./target/provider.jar
- 编译 Consumer
$ mvn clean compile -Pconsumer
- 运行 consumer
$ java -jar ./target/consumer.jar
详细解释
定义服务接口
DemoService.java
package org.apache.dubbo.samples.basic.api;
public interface DemoService {
String sayHello(String name);
}
在服务提供方实现接口
DemoServiceImpl.java
public class DemoServiceImpl implements DemoService {
@Override
public String sayHello(String name) {
System.out.println("[" + new SimpleDateFormat("HH:mm:ss").format(new Date()) + "] Hello " + name +
", request from consumer: " + RpcContext.getContext().getRemoteAddress());
return "Hello " + name + ", response from provider: " + RpcContext.getContext().getLocalAddress();
}
}
用 Spring 配置声明暴露服务
provider.xml:
<bean id="demoService" class="org.apache.dubbo.samples.basic.impl.DemoServiceImpl"/>
<dubbo:service interface="org.apache.dubbo.samples.basic.api.DemoService" ref="demoService"/>
服务消费者
引用远程服务
consumer.xml:
<dubbo:reference id="demoService" check="true" interface="org.apache.dubbo.samples.basic.api.DemoService"/>
加载Spring配置,并调用远程服务
Consumer.java
public static void main(String[] args) {
...
DemoService demoService = (DemoService) context.getBean("demoService");
String hello = demoService.sayHello("world");
System.out.println(hello);
}
最后修改 September 21, 2021: Bug fix miss mialbox (#953) (57cf51b)