此文档已经不再维护。您当前查看的是快照版本。如果想要查看最新版本的文档,请参阅最新版本。
Dubbo 还提供了包括XML、API 等多种启动与接入方式,更多开发方式和配置细节可参见配置手册。
完整示例代码在 dubbo-samples 中
git clone -b master https://github.com/apache/dubbo-samples.git
cd dubbo-samples/dubbo-samples-spring-boot
ls # 查看目录结构
cd ./dubbo-samples-spring-boot-provider
mvn clean package
java -jar ./target/dubbo-samples-spring-boot-provider-1.0-SNAPSHOT.jar
cd ../dubbo-samples-spring-boot-consumer
mvn clean package
java -jar ./target/dubbo-samples-spring-boot-consumer-1.0-SNAPSHOT.jar
dubbo-samples-spring-boot-interface/DemoService.java
package org.apache.dubbo.samples.basic.api;
public interface DemoService {
String sayHello(String name);
}
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;
}
}
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
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
必须配置。
dubbo-samples-spring-boot-consumer/ConsumerApplication.java
public class ConsumerApplication {
@DubboReference
private DemoService demoService;
}
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
dubbo-samples-spring-boot-consumer/ConsumerApplication.java
@SpringBootApplication
@Service
@EnableDubbo
public class ConsumerApplication {
@DubboReference
private DemoService demoService;
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
必须配置。