The Pact Broker is an application for sharing for consumer driven contracts and verification results.
- The consumer CI build generates the pacts during execution of its isolated tests, and then publishes the generated pacts.
- The provider CI retrieves the pacts, performs the verification locally, and then publishes the verification results back to the broker.
- Consumer and provider CI deployment builds check with the broker before deploying to ensure the application version they are about deploy will be compatible with the versions of the other applications that are already in that environment.
We can use many approaches to deposite pact files and verification results, i use docker as an example,
Docker Setup
Get info from pact_broker-docker.
- Install docker.
Download
docker-compose.yml
and modify.1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26version: "3"
services:
postgres:
image: postgres
healthcheck:
test: psql postgres --command "select 1" -U postgres
ports:
- "5432:5432"
environment:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: password
POSTGRES_DB: postgres
broker_app:
image: dius/pact-broker
links:
- postgres
# If you remove nginx, enable the following
ports:
- "80:80"
environment:
PACT_BROKER_DATABASE_USERNAME: postgres
PACT_BROKER_DATABASE_PASSWORD: password
PACT_BROKER_DATABASE_HOST: postgres
PACT_BROKER_DATABASE_NAME: postgresRun
docker-compose up
command to get a running Pact Broker and a clean Postgres database.- Verify.
Pact Broker Usage
consumer
Add the below configs in consumer pom.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15<plugin>
<!-- mvn pact:publish -->
<groupId>au.com.dius</groupId>
<artifactId>pact-jvm-provider-maven_2.11</artifactId>
<version>3.5.24</version>
<configuration>
<!-- <pactDirectory>../pacts</pactDirectory> Defaults to ${project.build.directory}/pacts -->
<pactBrokerUrl>http://192.168.220.139:80</pactBrokerUrl>
<projectVersion>${project.version}</projectVersion> <!-- Defaults to ${project.version} -->
<trimSnapshot>true</trimSnapshot> <!-- Defaults to false -->
<tags>
<tag>cdc/pact_broker</tag>
</tags>
</configuration>
</plugin>execute
mvn pact:publish
to publish the pact files.
provider
Add the below configs in provider pom.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19<plugin>
<groupId>au.com.dius</groupId>
<artifactId>pact-jvm-provider-maven_2.12</artifactId>
<version>3.5.24</version>
<configuration>
<serviceProviders>
<!-- You can define as many as you need, but each must have a unique name -->
<serviceProvider>
<name>test_provider</name>
<!-- All the provider properties are optional, and have sensible defaults (shown below) -->
<protocol>http</protocol>
<host>localhost</host>
<port>8080</port>
<path>/</path>
<pactBrokerUrl>http://192.168.220.139:80/</pactBrokerUrl>
</serviceProvider>
</serviceProviders>
</configuration>
</plugin>Execute
mvn pact:verify
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24D:\Java\pact\pact-jvm\pact-provider>mvn pact:verify
[INFO] Scanning for projects...
[INFO]
[INFO] ----------------------< com.izheyi:pact-provider >----------------------
[INFO] Building pact-provider 0.0.1-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- pact-jvm-provider-maven_2.12:3.5.24:verify (default-cli) @ pact-provider ---
Verifying a pact between test_consumer and test_provider
[from Pact Broker http://192.168.220.139/pacts/provider/test_provider/consumer/test_consumer/version/0.0.1]
A request for age for person zhangsan
returns a response which
has status code 200 (OK)
includes headers
"Content-Type" with value "application/json" (OK)
has a matching body (OK)
[WARNING] Skipping publishing of verification results as it has been disabled (pact.verifier.publishResults is not 'true')
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 6.702 s
[INFO] Finished at: 2018-11-18T13:12:24+08:00
[INFO] ------------------------------------------------------------------------We can see there has one
WARNING
, Publishing of verification results will be skipped unless this property is set to ‘true’, so we need to add the below config.1
2
3<configuration>
<pact.verifier.publishResults>true</pact.verifier.publishResults>
</configuration>Execute
mvn pact:verify
again.