用于冒烟测试的Spring Cloud Stub Runner Boot应用程序和Cloud Stream(Kafka)

时间:2018-08-07 17:02:28

标签: spring spring-boot apache-kafka spring-cloud-contract

在微服务架构中,我有三个服务。第一个在带有春季云流的kafka队列中创建一条消息。在此服务中,我使用Spring Cloud Contract生成合同。第二项服务是Spring Cloud StubRunner Boot服务,该服务读取第一项服务的合同并将其公开给第三项服务。第三项服务使用endpont / triiggers / {label}针对存根转轮服务进行抽烟测试。我了解,当我调用/ triggers / {label}时,服务存根运行程序应将在服务合同中创建的消息发送到kafka队列,但绝不要将其发送到队列。存根转轮服务如何将合同消息发送到kafka队列? 谢谢

代码:

服务1

合同:

org.springframework.cloud.contract.spec.Contract.make {

    description 'Register event: Customer registered'

    label 'CustomerRegistered'

    input {
        // the contract will be triggered by a method
        triggeredBy('registerEvent()')
    }
    // output message of the contract
    outputMessage {
        // destination to which the output message will be sent
        sentTo 'ClassCustomerEvent'
        // the body of the output message
        body('''{"id":1,"eventType":"CustomerRegistered","entity": {"clientId":1,"clientName":"David, Suarez, Pascual","classCalendarId":1,"classCalendarName":"Aula 1 - Aerobic","classCalendarDayId":7}}''')
    headers {
        header('contentType', applicationJson())
    }
}

}

服务2:

application.yml:

spring:
  cloud:
    stream:
      kafka:
        binder:
          brokers: localhost
          zkNodes: localhost
      default-binder: kafka

stubrunner:
  cloud:
    stubbed:
      discovery:
        enabled: false

stubrunner:
  stubsMode: LOCAL
  ids:
    - com.elipcero.classcustomerschool:classcustomer-school:1.0.0:stubs:8762

主要:

@SpringBootApplication
@EnableStubRunnerServer
@EnableBinding
@AutoConfigureStubRunner
public class ClassCustomerStubrunnerSchoolApplication {

    public static void main(String[] args) {

SpringApplication.run(ClassCustomerStubrunnerSchoolApplication.class, args);
}
}

服务3

SmokeTest:

@Test
public void should_calculate_client_total_by_classrooom_and_set_class_by_client() {

    mongoOperations.dropCollection("CustomerClass");
    mongoOperations.dropCollection("ClassCustomerDayTotal");

    String url = this.stubRunnerUrl + "/triggers/CustomerRegistered";

    log.info("Mongo collections deletes");
    log.info("Url stub runner boot: " + url);

    ResponseEntity<Map> response = this.restTemplate.postForEntity(url, "", Map.class);
    then(response.getStatusCode().is2xxSuccessful()).isTrue();

    log.info("Triggered customer event");

    await().until( () ->
         customerClassRepository
                 .findById(1)
                 .map((c) -> c.getClasses().isEmpty())
                 .orElse(false)
    );
 }

水槽:

@Service
@EnableBinding(ClassCustomerConsumer.class)
@RequiredArgsConstructor
public class ClassCustomerEvent {

    public static final String CONST_EVENT_CUSTOMER_REGISTERED = "CustomerRegistered";
    public static final String CONST_EVENT_CUSTOMER_UNREGISTERED = "CustomerUnregistered";

    @NonNull private ClassCustomerTotalView classCustomerTotalView;
    @NonNull private CustomerClassView customerClassView;

    @StreamListener(ClassCustomerConsumer.INPUT)
    public void ConsumeClassCustomerEvent(EventMessage<ClassCustomer> eventMessage) {
        classCustomerTotalView.calculate(eventMessage);
        customerClassView.selectOrUnSelected(eventMessage);
    }
}

1 个答案:

答案 0 :(得分:0)

在spring-cloud-contract-v2.1.0.M2之后在主版本中已修复

问题参考:https://github.com/spring-cloud/spring-cloud-contract/pull/805