欢迎您访问365答案网,请分享给你的朋友!
生活常识 学习资料

springboot快速使用dubbo

时间:2023-04-18

需求:商品下单,商城服务调用订单服务;

涉及到的服务:商城服务、订单服务、工具类服务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文件所需依赖

org.springframework.boot spring-boot-starter org.sg common org.apache.dubbo dubbo-spring-boot-starter org.apache.curator curator-framework org.apache.curator curator-recipes

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、引入相关依赖

org.springframework.boot spring-boot-starter org.sg common org.apache.dubbo dubbo-spring-boot-starter org.apache.curator curator-framework org.apache.curator curator-recipes

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); }}

流程图:

Copyright © 2016-2020 www.365daan.com All Rights Reserved. 365答案网 版权所有 备案号:

部分内容来自互联网,版权归原作者所有,如有冒犯请联系我们,我们将在三个工作时内妥善处理。