需求:商品下单,商城服务调用订单服务;
涉及到的服务:商城服务、订单服务、工具类服务common;
dubbo + zookeeper
公共api-common:
public interface OrderService { OrderInfo queryOrderInfo(String orderId); String goodsLowerOrder(String goodsId, String uuid);}
服务提供者producer-order:
1、配置类application.yml
server: port: 8881spring: application: name: orderdubbo: application: name: order registry: address: zookeeper://localhost:2181 config-center: timeout: 10000 #服务提供者 protocol: name: dubbo
2、 pom文件所需依赖
3、实现逻辑
import org.apache.dubbo.config.annotation.DubboService;import java.util.Date;@DubboService//@Servicepublic class OrderServiceImpl implements OrderService { @Override public OrderInfo queryOrderInfo(String orderId) { return new OrderInfo(3, "300.00", new Date(), new Date()); } @Override public String goodsLowerOrder(String goodsId, String uuid) { System.out.println("goodsId = " + goodsId); return uuid.substring(0, 6) + goodsId; }}
服务消费者concumer-goods:
1、引入相关依赖
2、配置yml信息
server: port: 8883spring: application: name: goodsdubbo: application: name: goods registry: address: zookeeper://192.168.9.221:2181 consumer: # 没有生产者取消报错 @Reference(check = false) check: false config-center: timeout: 10000
3、编写代码
@Controller@ResponseBodypublic class GoodsController { @Reference(check = false) private OrderService orderService; @RequestMapping("/queryOrderList") public OrderInfo queryOrderList() { return orderService.queryOrderInfo("111"); } @RequestMapping("/goodsLowerOrder/{goodsId}") public String goodsLowerOrder(@PathVariable("goodsId") String goodsId) { String uuid = UUID.randomUUID().toString(); return orderService.goodsLowerOrder(goodsId, uuid); }}
流程图: