springboot 快速使用dubbo + zookeeper

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

涉及到的服务:商城服务、订单服务、工具类服务common;

dubbo + zookeeper

公共api-common:

public interface OrderService {

    /**
     * 根据订单id查询订单详情
     * @param orderId
     * @return
     */
    OrderInfo queryOrderInfo(String orderId);

    /**
     * 商品下单
     * @param goodsId
     * @param uuid
     * @return
     */
    String goodsLowerOrder(String goodsId, String uuid);
}

服务提供者producer-order:

1、配置类application.yml

server:
  port: 8881
spring:
  application:
    name: order

dubbo:
  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
//@Service
public 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: 8883
spring:
  application:
    name: goods

dubbo:
  application:
    name: goods
  registry:
    address: zookeeper://192.168.9.221:2181
  consumer:   # 没有生产者取消报错 @Reference(check = false)
    check: false
  config-center:
    timeout: 10000

3、编写代码

@Controller
@ResponseBody
public 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);
    }

}

流程图:

springboot 快速使用dubbo + zookeeper_第1张图片

cap

c:  数据一致性

a:服务可用性

p:分区容错性

zookeeper 和 eureka 区别

1、zookeeper采用的是cp原则,数据一致性、分区容错性;

        zoopeeker任何时刻对Zookeeper的访问请求能得到一致的数据结果,同时系统对网络分割具备容错性;当我们zookeeper主机挂掉的话,zk会有选举的过程,这是我们的服务是不可用的状态; 因此他保证的是cp原则,即数据一致性;分区容错性。

2、eureka采用的是ap原则,服务可用性、分区容错性;

        而eureka无master/slave区分,每一个peer都是平等的,这种是保证服务的可用性;

你可能感兴趣的:(spring,boot,java,后端)