首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Spring Cloud Contract与Zuul

Spring Cloud Contract与Zuul
EN

Stack Overflow用户
提问于 2017-05-02 06:38:20
回答 1查看 360关注 0票数 0

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

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

EN

回答 1

Stack Overflow用户

发布于 2017-12-22 17:40:53

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

代码语言:javascript
复制
/**
 * 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));
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43727502

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档