加入依赖
<dependency>
<groupId>com.alibaba.spring.boot</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>1.0.0</version>
</dependency>
创建AP项目用来放接口和实体bean
创建调用服务提供者项目
application.properties
server.port=80
spring.dubbo.appname=spring-boot-starter-dubbo-provider-test
spring.dubbo.registry=redis://127.0.0.1:6379
spring.dubbo.protocol=dubbo
服务提供者启动类
@SpringBootApplication
@DubboComponentScan(basePackages = "com.rookie.*.impl")
public class ProviderApp {
public static void main(String[] args) {
SpringApplication.run(ProviderApp.class, args);
}
}
服务提供者接口实现类
@Service(interfaceClass = DemoService.class)
public class DemoServiceImpl implements DemoService {
@Override
public User getUser() {
return new User("1", "2");
}
}
创建服务消费者项目
application.properties
server.port=8080
spring.dubbo.appname=spring-boot-starter-dubbo-consumer-test
spring.dubbo.registry=redis://127.0.0.1:6379
spring.dubbo.protocol=dubbo
服务消费者启动类
@SpringBootApplication
@EnableDubboConfiguration
public class ConsumerApp {
public static void main(String[] args) {
SpringApplication.run(ConsumerApp.class, args);
}
}
服务消费者controller
@RestController
public class Demo {
@Reference(interfaceClass = DemoService.class)
private DemoService demoService;
@RequestMapping("/")
public void demo() {
System.out.println(demoService.getUser());
}
}