spring cloud contract http实例

微服务很多时,服务之前相互调用,接口参数的一致性要变得很难维护。

spring cloud contract 提供了测试接口一致性的方法。

一 项目配置

bash 复制代码
plugins {
    id "groovy"
    id "org.springframework.cloud.contract" version "4.0.5"
    id "maven-publish"
}
 
String contractVersion = "1.0.0-contract-SNAPSHOT"
 
ext {
    set('springCloudVersion', "2022.0.5")
    set('groovyVersion', "4.0.20")
    set('contractVersion', "4.0.5")
    set('streamVersion', "4.0.5")
}
 
contracts {
    testMode = 'MockMvc'
    deleteStubsAfterTest = true
    baseClassForTests = "com.leve.${artifactId}.ContractTestBase"
    generatedTestJavaSourcesDir = project.file("src/contractTest/contract")
}
 
dependencies {
    testImplementation("org.apache.groovy:groovy-all:${groovyVersion}")
    testImplementation("org.springframework.cloud:spring-cloud-starter-contract-verifier")
    testImplementation("org.springframework.cloud:spring-cloud-starter-contract-stub-runner")
}
 
dependencyManagement {
    imports {
        mavenBom("org.springframework.cloud:spring-cloud-dependencies:${property("springCloudVersion")}")
        mavenBom "org.springframework.cloud:spring-cloud-contract-dependencies:${property("contractVersion")}"
        mavenBom ("org.springframework.cloud:spring-cloud-stream-dependencies:${property("streamVersion")}")
    }
}
 
publishing {
    publications {
        maven(MavenPublication) {
            artifact(tasks.named('verifierStubsJar'))
        }
        // 如果项目名称带有横杠的,比如说auth-ghost,那么需要像下面这样去掉横杠,不然openfign404
        // maven.artifactId = "authghost"
    }
    repositories {
        maven {
            url = uri("https://nexus.111.com/repository/leve-maven/")
            version = contractVersion
            credentials {
                username = nexusUsername
                password = nexusPassword
            }
            allowInsecureProtocol = false
        }
    }
}

二 基类

公共基类, 需要注意基类所在的路径,可以修改配置指定基类的名称和路径。

java 复制代码
 
/**
 * ContractTestBase
 */
@SpringBootTest(properties = {
        "spring.cloud.function.definition=",
})
@AutoConfigureStubRunner
public class ContractTestBase{
 
 
    @Autowired
    private WebApplicationContext applicationContext;
    @MockBean
    private UserInfoService userInfoService;

 
    @BeforeEach
    public void setUp() {
        RestAssuredMockMvc.webAppContextSetup(applicationContext);
    }
 
}

三 消费端配置

ids看你需要哪些服务,自行增改。版本号可以不要,会拉取最新的。

yaml 复制代码
stubrunner:
  ids:
    - "com.test:user"
    - "com.test:ghost"
  repositoryRoot: https://nexus.test.com/repository/leve-maven/
  stubsMode: REMOTE

在测试类上面使用 @AutoConfigureStubRunner

四 运行测试

User服务调用 ghost服务的 接口

4.1 生产端

在ghost服务按照上面的配置,写好contract.yml文件,构建测试通过后,然后publish到nexus上面


4.2 消费端

bootstrap.yml文件添加对应的生产端服务id

然后再对应的测试类上面添加 @AutoConfigureStubRunner

测试方法

方法中通过openfeign调用ghost服务

测试结果

相关推荐
笨手笨脚の7 小时前
Spring Core常见错误及解决方案
java·后端·spring
00后程序员张9 小时前
HTTPS Everywhere 时代的抓包挑战,从加密流量解析到底层数据流捕获的全流程方案
网络协议·http·ios·小程序·https·uni-app·iphone
YDS82910 小时前
MyBatis-Plus精讲 —— 从快速入门到项目实战
java·后端·spring·mybatis·mybatis-plus
吃喝不愁霸王餐APP开发者10 小时前
霸王餐API文档自动化:Spring REST Docs与Asciidoctor多模块聚合
数据库·spring·自动化
小马爱打代码10 小时前
Spring AI:文生图:调用通义万相 AI 大模型
java·人工智能·spring
摇滚侠11 小时前
2025最新 SpringCloud 教程,网关功能、创建网关,笔记51、笔记52
java·笔记·spring cloud
香吧香11 小时前
Spring boot 中 CommandLineRunner 在服务启动完成后自定义执行
java·spring boot·spring cloud
清晓粼溪11 小时前
SpringMVC02:扩展知识
java·后端·spring
谷哥的小弟11 小时前
Spring Framework源码解析——Ordere
java·后端·spring·源码
摇滚侠12 小时前
2025最新 SpringCloud 教程,Seata-原理-二阶提交协议,笔记70
笔记·spring·spring cloud