跳转到主要内容
Chinese, Simplified

启用您的消费者驱动的合同工作流程http://pactflow.io

Pact Broker是一个用于共享消费者驱动的合同和验证结果的应用程序。它被优化用于“pacts”(由Pact框架创建的契约),但可以用于任何类型的可以序列化为JSON的契约。

我为什么需要一个?

合同测试是传统集成测试的另一种方法,它为您提供更快速执行的测试,并且可以更大规模地维护。该方法的一个缺点是在集成测试套件执行结束时可以在一个地方获得的重要信息(即,一起测试的所有应用程序的版本号,以及测试是否通过或失败的)现在分散在许多不同的版本中。 Pact Broker是一个工具,可以将所有这些信息重新组合在一起,并允许您安全地部署。

它:

  • 通过独立部署服务并避免集成测试的瓶颈,您可以快速,自信地发布客户价值
  • 解决了如何在消费者和提供者项目之间共享合同和验证结果的问题
  • 告诉您可以安全地一起部署哪些版本的应用程序
  • 自动修改合同
  • 允许您确保多个消费者和提供者版本之间的向后兼容性(例如,在移动或多租户环境中)
  • 提供您的应用程序的API文档,保证是最新的
  • 向您展示服务如何交互的真实示例
  • 允许您可视化服务之间的关系

特征:

  • 用于发布和检索协议的RESTful API。
  • 用于导航API的嵌入式API浏览器。
  • 每个协议的自动生成文档。
  • 动态生成的网络图,以便您可以可视化您的微服务网络。
  • 显示提供程序验证结果,以便您知道是否可以安全部署。
  • 提供兼容的消费者和提供商版本的“矩阵”,以便您了解哪些版本可以安全地一起部署。
  • 提供徽章以在README中显示协议验证状态。
  • 允许标记应用程序版本(即“prod”,“feat / customer-preferences”)以允许类似于存储库的工作流程。
  • 提供webhook以在pacts更改时触发操作,例如。运行提供程序构建,通知Slack通道。
  • 查看Pact版本之间的差异,以便了解哪些期望已发生变化。
  • Docker Pact Broker
  • 用于将Pact工作流程加载到持续集成过程的CLI。

我如何使用Pact Broker?


步骤1.消费者CI构建

 

  1. 使用者项目使用Pact库运行其测试以提供模拟服务。
  2. 测试运行时,模拟服务将请求和预期响应写入JSON“pact”文件 - 这是消费者合同。
  3. 然后将生成的协议发布到Pact Broker。大多数Pact库都会为您提供一个可以轻松完成此任务的任务,但是,最简单的是,它是指定消费者名称和应用程序版本以及提供者名称的资源的PUT。例如:http://my-pact-broker/pacts/provider/Animal%20Service/consumer/Zoo%20App/version/1.0.0(请注意,您在URL中指定了消费者应用程序版本,而不是协议版本。当内容发生变化时,代理将负责在幕后对该协议进行版本控制。预计消费者应用程序版本将随着每个CI构建而增加。)
  4. 发布协议时,如果协议内容自上一版本以来发生更改,则Pact Broker中的webhook将启动提供程序项目的构建。


步骤2.提供者CI构建

 

  1. 提供程序具有验证任务,该任务使用URL进行配置,以检索自身与使用者之间的最新协议。例如http:// my-pact-broker / pacts / provider / Animal%20Service / consumer / Zoo%20App / latest。
  2. 提供者构建运行pact验证任务,该任务从Pact Broker检索pact,对提供者重放每个请求,并检查响应是否与预期响应匹配。
  3. 如果协议验证失败,则构建失败。 Pact Broker CI Nerf Gun神奇地确定是谁导致验证失败,然后射击它们。
  4. 验证结果由契约验证工具发布回Pact Broker,因此消费者团队将知道他们编写的代码是否可以在现实生活中使用。

如果您没有Pact Broker CI Nerf Gun,那么当消费者和提供者由不同的团队编写时,您可能想要阅读有关使用pact的信息。

步骤3.返回Consumer CI构建


以下功能是测试版。您的反馈意见将不胜感激。

  1. 消费者CI通过运行pact-broker can-i-deploy --pacticipant CONSUMER_NAME --version CONSUMER_VERSION ...来确定协议是否已经过验证(请参阅此处的文档)
  2. 如果已验证协议,则可以继续部署。

在Wiki页面的概述中阅读有关如何使用Pact Broker的更多信息。

文档

有关Pact Broker的文档,请参阅Wiki。请首先阅读概述页面,以了解代理中的HTTP资源以及它们之间的相互关系。

截图

Index

自动生成的文档
将pact URL粘贴到浏览器中以查看该协议的HTML版本。

Network diagram

HAL浏览器
使用嵌入式HAL浏览器导航API。

HAL文档
在浏览时使用HAL浏览器查看文档。

 

 

用法


在本地计算机上玩游戏

  • Install ruby 2.2.0 or later and bundler >= 1.12.0
    • Windows users: get a Rails/Ruby installer from RailsInstaller and run it
    • unix users just use your package manager
  • Run git clone git@github.com:pact-foundation/pact_broker.git && cd pact_broker/example
  • Run bundle install
  • Run bundle exec rackup -p 8080 (this will use a Sqlite database. If you want to try it out with a Postgres database, see the README in the example directory.)
  • Open http://localhost:8080 and you should see a list containing the pact between the Zoo App and the Animal Service.
  • Click on the arrow to see the generated HTML documentation.
  • Click on either service to see an autogenerated network diagram.
  • Click on the HAL Browser link to have a poke around the API.
  • Click on the book icon under "docs" to view documentation related to a given relation.


真的

Hosted

In a hurry? Hate having to run your own infrastructure? Check out the Hosted Pact Broker - it's fast, it's secure and it's free!

Container solutions

You can use the Pact Broker Docker image or Terraform on AWS. See the wiki for instructions on using a reverse proxy with SSL.

Rolling your own

  • Are you sure you don't just want to use the Pact Broker Docker image? No Docker at your company yet? Ah well, keep reading.
  • Create a PostgreSQL (recommended) or MySQL (not as recommended because of @bethesque's personal prejudices, but still fully supported) database.
  • To ensure you're on a supported version of the database that you choose, check the travis.yml file to see which versions we're currently running our tests against.
  • If you're using PostgreSQL (did we mention this was recommended!) you'll find the database creation script in the example/config.ru.
  • Install ruby 2.4 or later and the latest version of bundler (if you've come this far, I'm assuming you know how to do both of these. Did I mention there was a Docker image?)
  • Copy the pact_broker directory from the Pact Broker Docker project. This will have the recommended settings for database connections, logging, basic auth etc. Note that the Docker image uses Phusion Passenger as the web application server in front of the Pact Broker Ruby application, which is the recommended set up.
  • Modify the config.ru and Gemfile as desired (eg. choose database driver gem, set your database credentials. Use the "pg" gem if using Postgres and the "mysql2" gem if using MySQL)
    • example Sequel configuration for postgres {adapter: "postgres", database: "pact_broker", username: 'pact_broker', password: 'pact_broker', :encoding => 'utf8'}
    • example Sequel configuration for mysql {adapter: "mysql2", database: "pact_broker", username: 'pact_broker', password: 'pact_broker', :encoding => 'utf8'} `
  • Please ensure you use encoding: 'utf8' in your Sequel options to avoid encoding issues.
  • For production usage, use a web application server like Phusion Passenger or Nginx to serve the Pact Broker application. You'll need to read up on the documentation for these yourself as it is beyond the scope of this documentation. See the wiki for instructions on using a reverse proxy with SSL.
  • Ensure the environment variable RACK_ENV is set to production.
  • Deploy to your location of choice.

升级

Please read the UPGRADING.md documentation before upgrading your Pact Broker, for information on the supported upgrade paths.

原文 :https://github.com/pact-foundation/pact_broker

讨论: 知识星球【数字化和智能转型】

 

Article
知识星球
 
微信公众号
 
视频号