用于烟雾测试的Spring Cloud Stub Runner启动应用程序和云流(Kafka)
在微服务架构中,我有三个服务。第一个是使用spring cloud stream在kafka队列中创建消息。在这个服务中,我使用SpringCloudContract生成一个契约。第二个服务是SpringCloudStubRunner引导服务,它读取第一个服务的契约并将它们公开给第三个服务。第三个服务使用endpont/triiggers/{label}对存根运行程序服务进行冒烟测试。我理解,当我调用/triggers/{label}时,服务存根运行程序应该将在服务契约中创建的消息发送到kafka队列,但决不将其发送到队列。如何让存根运行器服务将合同消息发送到kafka队列?。 谢谢 代码: 服务1 合同:用于烟雾测试的Spring Cloud Stub Runner启动应用程序和云流(Kafka),spring,spring-boot,apache-kafka,spring-cloud-contract,Spring,Spring Boot,Apache Kafka,Spring Cloud Contract,在微服务架构中,我有三个服务。第一个是使用spring cloud stream在kafka队列中创建消息。在这个服务中,我使用SpringCloudContract生成一个契约。第二个服务是SpringCloudStubRunner引导服务,它读取第一个服务的契约并将它们公开给第三个服务。第三个服务使用endpont/triiggers/{label}对存根运行程序服务进行冒烟测试。我理解,当我调用/triggers/{label}时,服务存根运行程序应该将在服务契约中创建的消息发送到kafk
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
主要内容:
服务3
烟雾测试:
@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)
);
}
@测试
public void应按客户计算客户总数,按客户设置{
mongoOperations.dropCollection(“CustomerClass”);
mongoOperations.dropCollection(“ClassCustomerDayTotal”);
字符串url=this.stubRunnerUrl+“/triggers/CustomerRegistered”;
log.info(“Mongo collections删除”);
log.info(“Url存根运行程序启动:”+Url);
ResponseEntity response=this.restTemplate.postForEntity(url,”,Map.class);
然后(response.getStatusCode().is2xxSuccessful()).isTrue();
日志信息(“触发的客户事件”);
等待()。直到(()->
customerClassRepository
.findById(1)
.map((c)->c.getClasses().isEmpty())
.orElse(错)
);
}
水槽:
@服务
@EnableBinding(ClassCustomerConsumer.class)
@所需参数构造函数
公共类ClassCustomerEvent{
公共静态最终字符串CONST\u EVENT\u CUSTOMER\u registed=“CustomerRegistered”;
公共静态最终字符串CONST\u EVENT\u CUSTOMER\u UNREGISTERED=“CustomerUnregistered”;
@非空私有类CustomerTotalView类CustomerTotalView;
@非空私有CustomerClassView CustomerClassView;
@StreamListener(ClassCustomerConsumer.INPUT)
public void ConsumerClassCustomerEvent(EventMessage EventMessage){
classCustomerTotalView.calculate(事件消息);
customerClassView.Selector或Unselected(事件消息);
}
}
在spring-cloud-contract-v2.1.0.M2之后的下一个版本的主版本中修复
问题参考:这里有一个兔子的例子。你能检查一下,看看你是否在用同样的方式做事吗?谢谢,我已经基于,并且我认为我在用同样的方式做事。我已经看到,创建的频道是一个DirectChannel,带有类型为org.springframework.cloud.stream.test.binder.TestSupportBinder的调度处理程序。此处理程序将消息存储在内存中。是否有任何方法可以使用kafka处理程序代替流测试绑定器?是否排除了自动配置?TestSupportBinderAutoConfiguration?是的,我使用了@SpringBootApplication(exclude={TestSupportBinderAutoConfiguration.class}),但不起作用。我不知道为什么?
@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);
}
}