Dapr牵手.NET学习笔记:绑定

绑定有点像订阅发布,但又不一样,绑定更简单,绑定输出(调用方)-绑定输入(被调用方)。

本例是用docker compose编排,并且用rabbitMQ来支持,因为rabbitMQ支持输入和输出绑定。

demo的目录结构:

de5758ce2e6de1fc8251845a2ed30747.png

 binding.yaml,放在components目录下

apiVersion: dapr.io/v1alpha1
kind: Component
metadata:
  name: orderupdate
spec:
  type: bindings.rabbitmq
  version: v1
  metadata:
  - name: queueName
    value: bindQueue
  - name: host
    value: amqp://admin:!2021que@rabbitmq:5672
  - name: durable
    value: true
  - name: deleteWhenUnused
    value: false
  - name: ttlInSeconds
    value: 60
  - name: prefetchCount
    value: 0
  - name: exclusive
    value: false
  - name: maxPriority
    value: 5

B2C目录中的docker-compose.yml

version: '3.4'


services:
  #┌────────────────────────────────┐
  #│ ordersystem app + Dapr sidecar │
  #└────────────────────────────────┘
  ordersystem:
    image: ${DOCKER_REGISTRY-}ordersystem
    depends_on:
      - redis
      - placement    
    build:
      context: ../
      dockerfile: OrderSystem/Dockerfile
    ports:
      - "3500:3500"
    volumes:   
      - ../OrderSystem:/OrderSystem  
    networks:
      - b2c-dapr
  ordersystem-dapr:
     image: "daprio/daprd:latest"
     command: [ "./daprd", "-app-id", "order", "-app-port", "80","-placement-host-address", "placement:50006","-components-path","/components"]
     depends_on:
       - ordersystem
     network_mode: "service:ordersystem"
     volumes:   
      - ../components:/components  
  
  #┌───────────────────────────────────┐
  #│ paymentsystem1 app + Dapr sidecar │
  #└───────────────────────────────────┘  
  paymentsystem1:
    image: ${DOCKER_REGISTRY-}paymentsystem
    depends_on:
      - redis
      - placement 
      - rabbitmq      
    build:
      context: ../
      dockerfile: PaymentSystem/Dockerfile
    ports:
      - "3601:3500"
    volumes:   
      - ../PaymentSystem:/PaymentSystem      
    networks:
      - b2c-dapr      
  paymentsystem1-dapr:
     image: "daprio/daprd:latest"
     command: [ "./daprd", "-app-id", "pay", "-app-port", "80","-placement-host-address", "placement:50006","-components-path","/components" ]
     depends_on:
       - paymentsystem1
     network_mode: "service:paymentsystem1"
     volumes:   
      - ../components:/components 
 
  #┌───────────────────────────────────┐
  #│ paymentsystem2 app + Dapr sidecar │
  #└───────────────────────────────────┘   
  paymentsystem2:
    image: ${DOCKER_REGISTRY-}paymentsystem
    depends_on:
      - redis
      - placement   
      - rabbitmq      
    build:
      context: ../
      dockerfile: PaymentSystem/Dockerfile
    volumes:   
      - ../PaymentSystem:/PaymentSystem            
    ports:
      - "3602:3500"
    networks:
      - b2c-dapr      
  paymentsystem2-dapr:
     image: "daprio/daprd:latest"
     command: [ "./daprd", "-app-id", "pay", "-app-port", "80" ,"-placement-host-address", "placement:50006","-components-path","/components"]
     depends_on:
       - paymentsystem2
     network_mode: "service:paymentsystem2"
     volumes:   
      - ../components:/components       


  #┌───────────────────────────────────┐
  #│ noticesystem1 app + Dapr sidecar │
  #└───────────────────────────────────┘  
  noticesystem1:
    image: ${DOCKER_REGISTRY-}noticesystem
    depends_on:
      - redis
      - placement  
      - rabbitmq          
    build:
      context: ../
      dockerfile: NoticeSystem/Dockerfile
    ports:
      - "3701:3500"
    volumes:   
      - ../NoticeSystem:/NoticeSystem      
    networks:
      - b2c-dapr      
  noticesystem1-dapr:
     image: "daprio/daprd:latest"
     command: [ "./daprd", "-app-id", "notice", "-app-port", "80","-placement-host-address", "placement:50006","-components-path","/components" ]
     depends_on:
       - noticesystem1
     network_mode: "service:noticesystem1"
     volumes:   
      - ../components:/components 




  #┌───────────────────────────────────┐
  #│ noticesystem2 app + Dapr sidecar │
  #└───────────────────────────────────┘  
  noticesystem2:
    image: ${DOCKER_REGISTRY-}noticesystem
    depends_on:
      - redis
      - placement    
      - rabbitmq                
    build:
      context: ../
      dockerfile: NoticeSystem/Dockerfile
    ports:
      - "3702:3500"
    volumes:   
      - ../NoticeSystem:/NoticeSystem      
    networks:
      - b2c-dapr      
  noticesystem2-dapr:
     image: "daprio/daprd:latest"
     command: [ "./daprd", "-app-id", "notice", "-app-port", "80","-placement-host-address", "placement:50006","-components-path","/components" ]
     depends_on:
       - noticesystem2
     network_mode: "service:noticesystem2"
     volumes:   
      - ../components:/components 


  #┌────────────────────────┐
  #│ Dapr placement service │
  #└────────────────────────┘  
  placement:
    image: "daprio/dapr"
    command: ["./placement", "-port", "50006"]
    ports:
      - "50006:50006"
    networks:
      - b2c-dapr


  #┌───────────────────┐
  #│ Redis state store │
  #└───────────────────┘  
  redis:
    image: "redis:latest"
    ports:
      - "6380:6379"
    networks:
      - b2c-dapr


  #┌──────────────────────┐
  #│ RabbitMQ state store │
  #└──────────────────────┘  
  rabbitmq:
    image: "rabbitmq:management"
    ports:
      - "15672:15672"
      - "5672:5672" 
    environment:
      - RABBITMQ_DEFAULT_USER=admin
      - RABBITMQ_DEFAULT_PASS=!2021que 
    networks:
      - b2c-dapr  
      
networks:
    b2c-dapr:

在PaymentSystem和NoticeSystem添加监听传入的事件

[HttpPost("/orderupdate")]
public async Task<IActionResult> OrderUpdate()
{
    _logger.LogInformation("NoticeSystem OrderUpdate……");
    using var reader = new StreamReader(Request.Body, System.Text.Encoding.UTF8);
    var content = await reader.ReadToEndAsync();
    _logger.LogInformation(content);
    return Ok();
}

启动 docker-compose up -d

会发现所有的挎斗都失败,如下图:

2d69d6159b3af773858e8e53c84d043b.png

查看日志,发现是rabbitMQ的5672没有连接成功,

ba654c7e378cc442d60d655469c41c00.png

绑过一段分析,配置和代码都没有问题 ,问题 出现在时差上,虽然在docker-compose中,配置了所有服务依赖rabbitMQ,但rabbitMQ的容器虽然启动了,但rabbitMQ服务还没有完全启动起来,这个时间挎斗连接时,就发生连接错误,可以等上一会,手动再次启动挎斗容器就可以了。(这块在生产上需要解决,不能每次都手动)

服务都启动后,可以测试了,调用绑定

http://localhost:3601/v1.0/bindings/orderupdate

需要用post一个json,来调用绑定,分别post了包含10个1 ,10个2,10个3,10个4的四个请求,四个请求会分别触发四个服务的orderupdate事件(这里orderupdate业务不太适合用绑定,应该用发布订阅,因为一个订单变是多,PaymentSystem和NoticeSystem都应该被通知到,但绑定事件不可以,这也是两者的区别)

8da50f5771eb97a11b6d8ff050894b79.png

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值