我们找到了解决我们特定问题的方法:
- 消费者不知道哪个服务充当提供者,但它知道它调用的(HTTP 方法,URL)元组。
- 微服务知道它负责的每一个(HTTP 方法、URL)。
我们将提供者定义为元组(HTTP 方法,URL)。结果,消费者包含许多提供者的许多测试,而微服务也包含许多提供者的许多测试。
在 node.js 中为消费者提供这样的东西:
const consumer = "MyConsumer";
const providerGetArticles = new Pact({ consumer, provider: 'GET-articles', ... });
const providerGetArticlesArticleId = new Pact({ consumer, provider: 'GET-articles-:articleId', ... });
const providerPostUsers = new Pact({ consumer, provider: 'POST-users', ... });
const providerGetUsers = new Pact({ consumer, provider: 'GET-users', ... });
const providerGetUsersUserId = new Pact({ consumer, provider: 'GET-users-:userId', ... });
providerGetArticles.setup().then(() => {
providerGetArticles.addInteraction(
{
withRequest: { method: 'GET', path: '/articles' },
...
providerGetArticlesArticleId.setup().then(() => {
providerGetArticlesArticleId.addInteraction(
{
withRequest: { method: 'GET', path: '/articles/12345' },
...
providerPostUsers.setup().then(() => {
providerPostUsers.addInteraction(
{
withRequest: { method: 'POST', path: '/users' },
...
像这样处理GET /articles
和处理的微服务GET /articles/:articleId
new Verifier({ provider: 'GET-articles', ... }).verifyProvider()...
new Verifier({ provider: 'GET-articles-:articleId', ... }).verifyProvider()...
我们现在可以单独启动单个微服务并运行提供程序测试。