英文:
Call Controller from kafka component
问题
I've been searching a lot about it, and the only two things I've found is that my design is possibly bad, but I doubt it is. The problem I am having is that I have Spring Kafka classes, where I have a trigger waiting for the messages that reach a topic, and when that info arrives I want to send it to my controller, and from my controller it will be taken to another microservice.
Could I autowired my controller without creating infinite instances?
When kafka is activated I would like to call a method from my controller.
Code Kafka:
@Component
public class KafkaConsumer {
private static final String TOPIC = "topic_name";
@KafkaListener(topics = TOPIC)
private void listen(String msg) {
//Send msg to my controller
}
}
@Slf4j
@RestController
@RequestMapping("controllerA")
public class Controller {
private RestTemplate restTemplate = new RestTemplate();
@Autowired
private MyService serviceA;
// More services....
private String sendMyMessageToAnotherMicroservice(String msg)
{
//Code...
restTemplate.exchange("MyUrlInAnotherMicroservice:9090", HttpMethod.POST, msg,
String.class);
//Code...
}
//More methods....
}
I need my kafka call my method 'sendMyMessageToAnotherMicroservice(String msg)'.
英文:
I've been searching a lot about it, and the only two things I've found is that my design is possibly bad, but I doubt it is. The problem I am having is that I have Spring Kafka classes, where I have a trigger waiting for the messages that reach a topic, and when that info arrives I want to send it to my controller, and from my controller it will be taken to another microservice.
Could I autowired my controller without creating infinite instances?
When kafka is activated I would like to call a method from my controller.
Code Kafka:
@Component
public class KafkaConsumer {
private static final String TOPIC = "topic_name";
@KafkaListener(topics = TOPIC)
private void listen(String msg) {
//Send msg to my controller
}
}
@Slf4j
@RestController
@RequestMapping("controllerA")
public class Controller {
private RestTemplate restTemplate = new RestTemplate();
@Autowired
private MyService serviceA;
// More services....
private String sendMyMessageToAnotherMicroservice(String msg)
{
//Code...
restTemplate.exchange("MyUrlInAnotherMicroservice:9090", HttpMethod.POST, msg,
String.class);
//Code...
}
//More methods....
}
I need my kafka call my method 'sendMyMessageToAnotherMicroservice(String msg)'
答案1
得分: 4
创建一个独立的服务用于处理器:
@Component
public class KafkaConsumer {
@Autowired
private MyKafkaProcessorService service;
private static final String TOPIC = "topic_name";
@KafkaListener(topics = TOPIC)
private void listen(String msg) {
service.sendMyMessageToAnotherMicroservice(msg);
}
}
@Service
public class MyKafkaProcessorService {
//this can be a bean
private RestTemplate restTemplate = new RestTemplate();
public String sendMyMessageToAnotherMicroservice(String msg){
//代码...
restTemplate.exchange("MyUrlInAnotherMicroservice:9090", HttpMethod.POST, msg,
String.class);
//代码...
}
}
@Slf4j
@RestController
@RequestMapping("controllerA")
public class Controller {
//如果控制器需要,你也可以在那里进行自动装配
@Autowired
private MyKafkaProcessorService service;
@Autowired
private MyService serviceA;
// 更多服务...
// 更多方法...
}
英文:
Create a separate service to the processor:
@Component
public class KafkaConsumer {
@Autowired
private MyKafkaProcessorService service;
private static final String TOPIC = "topic_name";
@KafkaListener(topics = TOPIC)
private void listen(String msg) {
service.sendMyMessageToAnotherMicroservice(msg);
}
}
@Service
public class MyKafkaProcessorService {
//this can be a bean
private RestTemplate restTemplate = new RestTemplate();
public String sendMyMessageToAnotherMicroservice(String msg){
//Code...
restTemplate.exchange("MyUrlInAnotherMicroservice:9090", HttpMethod.POST, msg,
String.class);
//Code...
}
}
@Slf4j
@RestController
@RequestMapping("controllerA")
public class Controller {
//if you need it in the controller then you can autowire there too
@Autowired
private MyKafkaProcessorService service;
@Autowired
private MyService serviceA;
// More services....
//More methods....
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论