在微服务架构中,我有三个服务。第一个服务使用Spring Cloud stream在kafka队列中创建消息。在这个服务中,我使用Spring Cloud合约来生成合约。第二个服务是Spring Cloud存根运行器引导服务,它读取第一个服务的合约并将它们公开给第三个服务。第三个服务使用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
烟雾测试:
@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);
}
}
修复了在master中的下一个版本之后的spring-cloud d-crating-v2.1.0。M2
发行参考:https://github.com/spring-cloud/spring-cloud-contract/pull/805