SpringCloud 如何按主題而不是按生產(chǎn)者定義消息傳遞合同?

2023-12-06 17:40 更新

為了避免通用倉庫中的消息合同重復(fù),當(dāng)很少有生產(chǎn)者將消息寫到一個(gè)主題時(shí),我們可以創(chuàng)建一個(gè)結(jié)構(gòu),將其余合同放置在每個(gè)生產(chǎn)者的文件夾中,并將消息合同放置在每個(gè)主題的文件夾中。

對(duì)于Maven項(xiàng)目

為了能夠在生產(chǎn)者端進(jìn)行工作,我們應(yīng)該指定一個(gè)包含模式,以通過我們感興趣的消息傳遞主題過濾通用存儲(chǔ)庫jar。Maven Spring Cloud Contract pluginincludedFiles屬性允許我們執(zhí)行此操作。還需要指定contractsPath,因?yàn)槟J(rèn)路徑將是公用存儲(chǔ)庫groupid/artifactid

<plugin>
   <groupId>org.springframework.cloud</groupId>
   <artifactId>spring-cloud-contract-maven-plugin</artifactId>
   <version>${spring-cloud-contract.version}</version>
   <configuration>
      <contractsMode>REMOTE</contractsMode>
      <contractsRepositoryUrl>http://link/to/your/nexus/or/artifactory/or/sth</contractsRepositoryUrl>
      <contractDependency>
         <groupId>com.example</groupId>
         <artifactId>common-repo-with-contracts</artifactId>
         <version>+</version>
      </contractDependency>
      <contractsPath>/</contractsPath>
      <baseClassMappings>
         <baseClassMapping>
            <contractPackageRegex>.*messaging.*</contractPackageRegex>
            <baseClassFQN>com.example.services.MessagingBase</baseClassFQN>
         </baseClassMapping>
         <baseClassMapping>
            <contractPackageRegex>.*rest.*</contractPackageRegex>
            <baseClassFQN>com.example.services.TestBase</baseClassFQN>
         </baseClassMapping>
      </baseClassMappings>
      <includedFiles>
         <includedFile>**/${project.artifactId}/**</includedFile>
         <includedFile>**/${first-topic}/**</includedFile>
         <includedFile>**/${second-topic}/**</includedFile>
      </includedFiles>
   </configuration>
</plugin>

對(duì)于Gradle項(xiàng)目

  • 為common-repo依賴項(xiàng)添加定制配置:
ext {
    conractsGroupId = "com.example"
    contractsArtifactId = "common-repo"
    contractsVersion = "1.2.3"
}

configurations {
    contracts {
        transitive = false
    }
}
  • 將common-repo依賴項(xiàng)添加到您的類路徑中:
dependencies {
    contracts "${conractsGroupId}:${contractsArtifactId}:${contractsVersion}"
    testCompile "${conractsGroupId}:${contractsArtifactId}:${contractsVersion}"
}
  • 將依賴項(xiàng)下載到適當(dāng)?shù)奈募A:
task getContracts(type: Copy) {
    from configurations.contracts
    into new File(project.buildDir, "downloadedContracts")
}
  • 解壓縮JAR:
task unzipContracts(type: Copy) {
    def zipFile = new File(project.buildDir, "downloadedContracts/${contractsArtifactId}-${contractsVersion}.jar")
    def outputDir = file("${buildDir}/unpackedContracts")

    from zipTree(zipFile)
    into outputDir
}
  • 清理未使用的合同:
task deleteUnwantedContracts(type: Delete) {
    delete fileTree(dir: "${buildDir}/unpackedContracts",
        include: "**/*",
        excludes: [
            "**/${project.name}/**"",
            "**/${first-topic}/**",
            "**/${second-topic}/**"])
}
  • 創(chuàng)建任務(wù)依賴項(xiàng):
unzipContracts.dependsOn("getContracts")
deleteUnwantedContracts.dependsOn("unzipContracts")
build.dependsOn("deleteUnwantedContracts")
  • 通過使用contractsDslDir屬性指定包含合同的目錄來配置插件
contracts {
    contractsDslDir = new File("${buildDir}/unpackedContracts")
}
以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)