dubbo hello world
demo 采用的是spring boot 项目
- mven 依赖
<!-- Spring Boot Dubbo 依赖 -->
<dependency>
<groupId>io.dubbo.springboot</groupId>
<artifactId>spring-boot-starter-dubbo</artifactId>
<version>1.0.0</version>
</dependency>
- 服务提供者配置
## Dubbo 服务提供者配置
spring.dubbo.application.name=provider
spring.dubbo.registry.address=zookeeper://127.0.0.1:2181
spring.dubbo.protocol.name=dubbo
spring.dubbo.protocol.port=20880
spring.dubbo.scan=com.ldh.dubbo.service
服务提供者使用@Service注解(dubbo包下的),标记要注册的方法
@Service
public class UserServiceImpl implements UserService {
@Override
public String login() {
return "登录成功" + new Date();
}
}
服务消费者方
配置文件
## 避免和 server 工程端口冲突
server.port=8081
## Dubbo 服务消费者配置
spring.dubbo.application.name=consumer
spring.dubbo.registry.address=zookeeper://127.0.0.1:2181
spring.dubbo.scan=com.ldh.dubbo.service
服务消费者使用@Reference注解
@Component
public class MovieServiceImp implements MovieService {
@Reference
private UserService userService;
public void show(){
String res = userService.login();
System.out.println(res);
}
}