DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Because the DevOps movement has redefined engineering responsibilities, SREs now have to become stewards of observability strategy.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Related

  • Distributed Tracing System (Spring Cloud Sleuth + OpenZipkin)
  • Component Tests for Spring Cloud Microservices
  • A Robust Distributed Payment Network With Enchanted Audit Functionality - Part 2: Spring Boot, Axon, and Implementation
  • 7 Microservices Best Practices for Developers

Trending

  • Enhancing Business Decision-Making Through Advanced Data Visualization Techniques
  • Can You Run a MariaDB Cluster on a $150 Kubernetes Lab? I Gave It a Shot
  • AI Speaks for the World... But Whose Humanity Does It Learn From?
  • Distributed Consensus: Paxos vs. Raft and Modern Implementations
  1. DZone
  2. Software Design and Architecture
  3. Cloud Architecture
  4. Contracts for Microservices With OpenAPI and Spring Cloud Contract

Contracts for Microservices With OpenAPI and Spring Cloud Contract

In this post, I'll explain how to define contracts at the service level using OpenAPI and Spring Cloud Contract.

By 
John Thompson user avatar
John Thompson
·
Apr. 19, 19 · Tutorial
Likes (4)
Comment
Save
Tweet
Share
20.7K Views

Join the DZone community and get the full member experience.

Join For Free

I've previously posted about defining Consumer Driven Contracts using the OpenAPI specification and Spring Cloud Contract. This approach works well because you can tie your contracts to your API specification, and also wire in a request validator into your contract tests.

But one problem that remained is typically your OpenAPI specification will span multiple services. Thus, when generating contract tests, you'd get tests generated for all services and not just a specific service.

This leads to headaches in your CI/CD process. If you ran the tests at the service level, contract tests for other services would, of course, fail.

Ideally you'd want contract testing at the service level, and not downstream at the scope of the OpenAPI specification.

To address this problem, I've made an enhancement to my OpenAPI SCC contract parser to allow contract generation at the service level.

This enhancement is to allow contract generation at the microservice level from a OpenAPI specification which is typically from the enterprise level.

In this post, I'll explain how to define contracts at the service level using OpenAPI and Spring Cloud Contract.

This feature is available in release 2.1.1.2 of Spring Cloud Contract OpenAPI and higher.

In the x-contracts extension on the OpenAPI Operations object, declare the service name as follows:

x-contracts:
      -   contractId: 1
          name: Test Get Payor
          contractPath: "/v1/payors/0a818933-087d-47f2-ad83-2f986ed087eb"
          serviceName: payor-service

At this point, if you do nothing, all contracts in the spec will still be parsed.

To filter on the service name, the parser will read a system environment variable called scc.enabled.servicenames.

Spring Cloud Contacts are typically generated via the SCC Maven or Gradle plugins. The plugin configuration is upstream of the parser, thus a system environment variable was the best configuration solution.

For use with Maven, setting the system environment variable is simple. You can use a Maven plugin as shown below.

<!--  Set env prop for contract tests - filters to just this service-->
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>properties-maven-plugin</artifactId>
                <version>1.0.0</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>set-system-properties</goal>
                        </goals>
                        <configuration>
                            <properties>
                                <property>
                                    <name>scc.enabled.servicenames</name>
                                    <value>${artifactId}</value>
                                </property>
                            </properties>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

Here, I'm setting the environment variable to the artifact id. Thus my 'serviceName' is the same as the Maven artifactId. A good idea from my buddy Mike R.

The value can be any string value. Or any comma separated list of string values if you wish to activate multiple serviceNames.

One side note - if a contact does not have a serviceName value set, the parser will still pick it up.

One other note, if your configuration does not produce any contracts, the SCC Maven plugin will throw an exception. Currently, the SCC YamlContract parser is picking up the OpenAPI YAML document and throwing an exception when trying to parse it. This should be fixed in the next release of Spring Cloud Contract. See this issue for details. At the time of writing, Spring Cloud Contract is on version 2.1.1.RELEASE. I expect the fix will make the next release of Spring Cloud Contract.

Through this configuration, you can have one OpenAPI specification for your APIs, and then re-use the specification to generate contracts at the individual service level.

This post was specifically about using the new service name feature. If you'd like to learn more about defining consumer driven contracts in OpenAPI for Spring Cloud Contract, please see the home page of my project repository.

Spring Cloud Spring Framework microservice OpenAPI Specification

Published at DZone with permission of John Thompson, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Distributed Tracing System (Spring Cloud Sleuth + OpenZipkin)
  • Component Tests for Spring Cloud Microservices
  • A Robust Distributed Payment Network With Enchanted Audit Functionality - Part 2: Spring Boot, Axon, and Implementation
  • 7 Microservices Best Practices for Developers

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!