当前位置: 首页 > 知识库问答 >
问题:

Spring云合同和Zuul

章威
2023-03-14

使用Spring Cloud Dalston,我们构建了一个代理服务,当然使用Zuul。我现在正在尝试添加Spring Cloud合约测试,以从合约遵守的角度验证我们的代理服务按预期工作。奇怪的是,我可以发送请求并收到200状态代码,但预期的响应正文、内容类型标头等为空,因此我的测试失败了。

对于使用Zuul功能的测试服务,是否有Spring Cloud合同文档中未指定的其他配置?

共有1个答案

钱选
2023-03-14

这里有一个例子https://github.com/spring-cloud/spring-cloud-contract/issues/450

/**
 * Abstraction of configuration to test contracts with external microservices which are using a zuul-gateway in between.
 *
 * When a provider of data only allows communication through a Zuul-Gateway, a special way to ensure valid contracts is needed.
 * The Goal is to emulate the Zuul-Interception to be able to use the contracts stored within the providing service.
 *
 * F.e.: Consumer-Service consumes the Provider-Service, but the provider can not be called directly.
 * The Consumer-Service calls an URL with a prefix consisting of the name of the gateway ("gateway") and name of the
 * service (in this example "Provider-Service"). For example: http://gateway/provider/path/to/resource.
 * The contract provided by Provider-Service holds only a part of the provided URL (here: "/path/to/resource").
 * The resolution of "gateway" and "provider" is done within this class.
 */
@RunWith(SpringRunner.class)
@SpringBootTest(classes = EmbeddedZuulProxy.class,
    properties = {"server.port: 8080",
        "zuul.routes.provider.path: /provider/**",
        "zuul.routes.provider.service-id: provider",
        "zuul.routes.provider.url: http://localhost:9090/" //the url of the provider-stub is at port 9090
    },
    webEnvironment =  WebEnvironment.DEFINED_PORT) //defined port is important! Ribbon points to zuul, which then points to the provider-stub
@AutoConfigureMockMvc
@AutoConfigureJsonTesters
//the stub runs on fixed port 9090, so that zuul can point to it
@AutoConfigureStubRunner(ids = "<group-id>:<artifact-id>:+:stubs:9090")
@DirtiesContext
public abstract class ZuulContractBase {

}

/**
 * Configuration and Setup of an embedded Zuul-Gateway.
 * This way it is possible to use contracts, stored in providing service
 */
@Configuration
@ComponentScan(basePackages = "<path.to.feign.client>") //autowiring feign client
@EnableAutoConfiguration
@EnableZuulProxy
@EnableFeignClients
@RibbonClient(name = "gateway", configuration = SimpleRibbonClientConfiguration.class)
class EmbeddedZuulProxy {

    @Bean
    RouteLocator routeLocator(DiscoveryClient discoveryClient,
                              ZuulProperties zuulProperties) {
        return new DiscoveryClientRouteLocator("/", discoveryClient, zuulProperties);
    }
}

/**
 * Ribbon Load balancer with fixed server list for "simple" pointing to localhost,
 * which holds the mocked zuul-gateway
 *
 * f.e. a call with feign would normally look like:
 * http://gateway/provider/rest/path/to/your/{url}
 * which is mapped to:
 * http://localhost:{zuulport}/provider/rest/path/to/your/{url}
 */
@Configuration
class SimpleRibbonClientConfiguration {

    @Value("${server.port}")
    private Integer zuulPort;

    @Bean
    public ServerList<Server> ribbonServerList() {
        return new StaticServerList<>(new Server("localhost", zuulPort));
    }
}
 类似资料:
  • 我能够收到生产者端的绿色通行证,但在消费者端,我一直得到 合同是存在的。 我尝试了几个用户案例,如官方spring cloud文档中描述的。 https://cloud.spring.io/spring-cloud-contract/reference/html/gett-started.html#gett-started-cdc-consumer-run 以下是使用者代码示例:https://g

  • 到目前为止,我已经发现,通过在contract中定义“TriggeredBy”,生成的测试将调用那里提供的方法,因此我们将需要提供该方法在TestBase中执行的操作的实际实现。另一个是“outputMessage”,在这里我们可以验证之前的方法调用是否正确地生成了发送到某个交换的消息体。 资料来源:文件和样本 我的问题是,有没有办法从契约中产生输入消息,而不是触发自己的自定义方法?文档中的Spr

  • 我正在尝试将Spring Cloud Contract应用于使用Spring WebFlux构建的反应API。基本上能够以以下方式从API发送通量和接收响应通量: 但是,我在网上或文档中找不到关于我是否可以使用Spring Cloud Contract实现这一点的信息。 甚至有可能为此写一份合同吗?它是否能够提供wiremock并生成适当的JUnit测试?

  • 想要改进这个问题吗?更新问题,以便通过编辑这篇文章用事实和引用来回答。 最初的关闭原因未得到解决 我正在尝试了解PACT和Spring Cloud合同之间更好的工具来实施消费者驱动程序合同测试。我没有找到任何明确的例子来找到优缺点。 我想实现CDCT,我在项目中不使用Spring。根据我的理解,我认为PACT是很好的选择。 欢迎任何信息或建议。谢谢你。

  • 我正在尝试理解spring cloud contract,所以正在阅读本教程。 我的问题是关于本节的: https://cloud.spring.io/spring-cloud-contract/multi/multi__spring_cloud_contract_verifier_introduction.html#_defining_the_contract 查看请求正文部分,Groovy和Y