此文档已经不再维护。您当前查看的是快照版本。如果想要查看最新版本的文档,请参阅最新版本。
示例使用 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 目录
$ mvn clean package -Pprovider
$ java -jar ./target/provider.jar
$ mvn clean package -Pconsumer
$ 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();
}
}
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"/>
Consumer.java
public static void main(String[] args) {
...
DemoService demoService = (DemoService) context.getBean("demoService");
String hello = demoService.sayHello("world");
System.out.println(hello);
}