dubbo框架

1 模块添加
- 新建三个模块,分别表示消息的提供者,消息的消费者和公共模块

- dubbo-parent的pom
说明:加入springboot的父依赖,作为版本控制
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.6.RELEASE</version>
</parent>
- service-provider的pom
<dependencies>
<dependency> <!-- 公共模块的依赖 -->
<groupId>com.yqj</groupId>
<artifactId>common</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency> <!-- dubbo的依赖 -->
<groupId>com.alibaba.boot</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>0.2.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
</dependencies>
- service-consumer的pom
<dependencies>
<dependency> <!-- 公共模块的依赖 -->
<groupId>com.yqj</groupId>
<artifactId>common</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency> <!-- dubbo的依赖 -->
<groupId>com.alibaba.boot</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>0.2.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
2 消息提供者模块
- 主启动类
@EnableDubbo //扫描dubbo的组件
@SpringBootApplication
public class UserServiceProviderApplication {
public static void main(String[] args) {
SpringApplication.run(UserServiceProviderApplication.class,args);
}
}
- 服务接口(定义在common公共模块里面)目的是可以让其他模块得到依赖
public interface UserService {
public String getUserAddress(String userId);
}
- 服务实现
@Service //dubbo的service注解,说明该类是一个资源
@Component
public class UserServiceImpl implements UserService {
@Override
public String getUserAddress(String userId) {
String url = "dubbo localhost " + userId;
return url;
}
}
- 配置文件
server:
port: 9001
dubbo:
application:
name: user-service-provider
registry:
protocol: zookeeper
address: localhost:2181
protocol:
name: dubbo
port: 20880
monitor:
protocol: registry
3 消息消费者
- 主启动类
@EnableDubbo
@SpringBootApplication
public class ServiceConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(ServiceConsumerApplication.class,args);
}
}
- 服务接口
public interface OrderService {
public String getRemoteUserAddress(String userId);
}
- 服务实现
@Service
public class OrderServiceImpl implements OrderService {
@Reference //远程注入
private UserService userService; //从公共模块中导入 UserService
@Override
public String getRemoteUserAddress(String userId) {
return userService.getUserAddress(userId);
}
}
- controller调用服务的方法
@RestController
@RequestMapping("/order")
public class OrderController {
@Autowired
private OrderService orderService;
@GetMapping("/{userId}")
public String getRemoteUserAddress(@PathVariable("userId") String userId) {
return orderService.getRemoteUserAddress(userId);
}
}
- 配置文件
server:
port: 8001
dubbo:
application:
name: order-service-consumer
registry:
address: zookeeper://localhost:2181
monitor:
protocol: registry
4 测试
- 启动zookeeper
- 访问网页测试
http://localhost:8001/order/12
网友评论