Registering Contracts

Reasons for registering contracts:

  1. View and export swagger API documents that are automatically generated (without writing any code or making any configuration).
  2. Enjoy better performance when interconnecting with edge service gateways compared with Spring Cloud gateways. For details, see Performance Tests.
  3. Enjoy the performance advantages brought by the reactive thread model when interconnecting with Java chassis microservice applications.

Prerequisite

You have added the spring-cloud-starter-huawei-servicecomb-discovery dependency.

Getting Started

For a Spring Cloud application, you only need to add the following dependencies.

  1. Add the following dependencies.
    <dependencies>     
      <dependency>       
          <groupId>com.huaweicloud</groupId>        
          <artifactId>spring-cloud-starter-huawei-swagger</artifactId>       
      </dependency>   
    </dependencies>

    Note: If you only need API management but do not need to connect Service Comb Java-chassis applications or edge service gateways, you can implement asynchronous contract registry through spring.cloud.servicecomb.swagger.enableJavaChassisAdapter:false (default value: true) to accelerate application startup.

  2. After the contract registry succeeds, the registered swagger contract information will be printed on the SDK side. Then go to the Cloud Service Engine (CSE) console, choose Service Catalog, and view the service contract in the registered microservice details.

Running the Demo

The following shows how to use a contract during interconnection with a Java-chassis application and an edge service gateway based on a project demo. spring-cloud-huawei-sample/swagger-demo is used to simulate the scenario where a Spring Cloud application is connected to a Java-chassis application. The following three services are involved:

  • springcloud-provider: Spring Cloud provider service.
  • javachassis-consumer: Java-chassis consumer service.
  • edge-service: Edge service gateway service.

Run the demo according to the following procedure:

  1. Download the local lightweight microservice engine, decompress it, and run start.bat.Then you can access http://localhost:30106 to go to the console.
  2. Respectively run the main function to start the three demo services.
  3. Access http://localhost:30106/#/cse/service/list to go to the local CSE console. Then, go to the microservice details page and click the Schema tab to view the contract information.
  4. Access the following addresses:

    http://localhost:18080/rest/swagger-provider/hello?name=123 (when the edge service gateway is used to access services)

    http://localhost:8082/consumer/hello?name=123 (when the Java chassis consumer service is connected to the Spring Cloud service)

    Check whether the expected result is returned.

Configuration Items

The following table lists the configuration items in the application.yaml file.

Configuration Item

Key

Default Value

Calling with Java chassis. If this parameter is set to false, the contract is registered asynchronously to speed up the startup.

spring.cloud.servicecomb.swagger.enableJavaChassisAdapter

true