Spring Boot Development Services
Dubbo also provides a variety of startup and access including XML, API For more development methods and configuration details, please refer to Configuration Manual.
Download sample code
The complete sample code is in dubbo-samples.
- Download the source code
git clone -b master https://github.com/apache/dubbo-samples.git
- Enter the example directory
cd dubbo-samples/1-basic/dubbo-samples-spring-boot
ls # view directory structure
Quick run example
- Compile Provider Execute the maven command in the dubbo-samples-spring-boot directory
mvn clean package
- Run Provider Enter the dubbo-samples-spring-boot-provider/target directory and start the java process
cd ./dubbo-samples-spring-boot-provider
java -jar ./target/dubbo-samples-spring-boot-provider-1.0-SNAPSHOT.jar
- Run the consumer Enter the dubbo-samples-spring-boot-consumer directory and start the java process
java -jar ./target/dubbo-samples-spring-boot-consumer-1.0-SNAPSHOT.jar
- View the results The following information will be output on the consumer side:
result: hello world
On the provider side, the following information will be output:
Hello World, request from consumer: xxx.xxx.xxx.xxx
So far, the basic functions of Dubbo have been realized, and more development can be carried out on the basis of Dubbo.
Example core process
For a more detailed interpretation of the examples, please refer to Annotation Configuration
1. Define service interface
dubbo-samples-spring-boot-interface/DemoService.java
package org.apache.dubbo.samples.basic.api;
public interface DemoService {
String sayHello(String name);
}
2. The provider implements the interface and exposes the service
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. Configure the application.yml file
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. Define Spring Boot main function
dubbo-samples-spring-boot-provider/ProviderApplication.java
@SpringBootApplication
@EnableDubbo
public class ProviderApplication {
public static void main(String[] args) throws Exception {
new Embedded ZooKeeper(2181, false).start();
SpringApplication.run(ProviderApplication.class, args);
System.out.println("dubbo service started");
new CountDownLatch(1). await();
}
}
Among them, @EnableDubbo
must be configured.
5. Reference remote service
dubbo-samples-spring-boot-consumer/ConsumerApplication.java
public class ConsumerApplication {
@DubboReference
private DemoService demoService;
}
6. Define 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. Load Spring configuration and call remote service
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);
}
}
Among them, @EnableDubbo
must be configured.
Feedback
Was this page helpful?
Glad to hear it! Please tell us how we can improve.
Sorry to hear that. Please tell us how we can improve.