如何在不破坏现有集成的情况下引入 webhook 负载的变化

How introduce a change in webhook payload with out breaking existing integration

在我们的应用程序资源中发生某些事件,例如 user/order/shipment ,我们会通过事件的高级详细信息通知订阅者事件的发生。可以通过 REST Web api.

对 user/order/shipment 等应用程序资源进行更改

现在由于业务需要,现有的 webhook 有效负载结构需要更改,引入该更改将破坏现有的集成。在不破坏任何现有集成的情况下引入 webhook 负载更改的任何解决方案?

现有 webhook 负载示例

{
  orderid : 123455,
  customerid : abc@domain.com,
  other properties ....
}

示例 Future webhook 负载

{
  orderid : 123455,
  customerid : 5435gd98, //modification
  other properties ....
}

理想情况下,在设计事件架构期间,您应该在事件中有一个 version 属性 来表示架构版本,如下所示。每当订阅者挂钩到您的事件时,他们将绑定到特定版本,直到他们需要切换到更新的版本。这样,每次转换到新模式时,您都会在一定的宽限期内继续维护新旧模式以实现向后兼容性,从而使现有订阅者能够在不中断的情况下进行更新。

{
  version : 1,
  orderid : 123455,
  customerid : abc@domain.com,
  other properties ....
}

{
  version : 2,
  orderid : 123455,
  customerid : 5435gd98, //modification
  other properties ....
}

但看起来您错过了现有事件模式(没有版本控制)中的火车。所以现在你至少在最初的某个时候需要一些肮脏的解决方法。但引入版本控制永远不会太迟。首先,您的活动如下所示。请注意,我已经介绍了 version 属性 以便任何新订阅者从现在开始都知道这一点,而您现有的订阅者不会同时中断,因为没有更改现有的事件属性。同时,您可以通知那些老订阅者过渡到新订阅者。将来,这样的更改会更容易,因为现在您有了版本控制。

{
  version : 2,
  orderid : 123455,
  customerid : abc@domain.com, // for V1 compatibility
  // name new customerid field something different, below is just for example
  customerid_v2 : 5435gd98, // V2
  other properties ....
}

或者它可以像(在嵌套 json 中移动客户详细信息):

{
  version : 2,
  orderid : 123455,
  customerid : abc@domain.com, // for V1 compatibility
  customer : { 
    id : 5435gd98,
    email : abc@domain.com
  }
  other properties ....
}