最佳实践
@RestController
@RequestMapping("/order")
public class OrderController {
private final IOrderProducer _IOrderProducer;
public OrderController(IOrderProducer iorderProducer) {
this._IOrderProducer = iorderProducer;
}
@GetMapping("/OrderService")
void get() {
_IOrderProducer.CreateOrderProducer("This is a Producer");
}
}
界面
@Service
public interface IOrderProducer {
void CreateOrderProducer(String message);
}
执行
public class OrderProducer implements IOrderProducer{
private KafkaTemplate<String, String> _template;
public OrderProducer(KafkaTemplate<String, String> template) {
this._template = template;
}
public void CreateOrderProducer(String message){
this._template.send("Topic1", message);
}
}
您需要在 Spring Boot 中包含 Project Lombok 依赖项
摇篮implementation 'org.projectlombok:lombok'