
dubbo框架
image.png 1 模块添加说明:加入springboot的父依赖,作为版本控制
org.springframework.boot spring-boot-starter-parent2.1.6.RELEASE
com.yqj common1.0-SNAPSHOT com.alibaba.boot dubbo-spring-boot-starter0.2.0 org.springframework.boot spring-boot-starterorg.springframework.boot spring-boot-starter-test
2 消息提供者模块com.yqj common1.0-SNAPSHOT com.alibaba.boot dubbo-spring-boot-starter0.2.0 org.springframework.boot spring-boot-starterorg.springframework.boot spring-boot-starter-web
@EnableDubbo //扫描dubbo的组件
@SpringBootApplication
public class UserServiceProviderApplication {
public static void main(String[] args) {
SpringApplication.run(UserServiceProviderApplication.class,args);
}
}
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);
}
}
@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 测试