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服务

测试结果

相关推荐
Suhan426 小时前
IDEA 设置启动参数和环境变量
spring boot·spring·编辑器·idea
猫头_8 小时前
AI 流式传输工程指南:有了 EventSource 为何还要 Fetch?
javascript·http·llm
橘子海全栈攻城狮9 小时前
【最新源码】基于SpringBoot + Vue的超市管理系统的设计与实现D002
java·开发语言·vue.js·spring boot·后端·spring
Chloeis Syntax9 小时前
JAVAEE初阶 --- 构造HTTP请求
网络·网络协议·http·postman
空中湖9 小时前
Spring AI Function Calling 完全指南:让 AI 查数据库、调 API、发通知
数据库·人工智能·spring
风起洛阳@不良使10 小时前
spring中xml和注解开发的对比
xml·java·spring
野蛮人6号10 小时前
黑马天机学堂Day01-08.搭建项目环境-本地开发部署方式——不知道nacos的密码是什么
java·spring cloud·黑马程序员·天机学堂·黑马天机学堂
weixin_7275356210 小时前
双Token认证体系深度拆解:Spring Security + JWT + Redis
redis·spring·wpf
Ethan010712 小时前
spring事务隔离级别,如果数据库设置为RC,spring可以改为RR吗
spring
Ethan010712 小时前
详解Spring 事务三大核心接口,以及我们能用它来做什么
spring