调用控制器从kafka组件

huangapple go评论76阅读模式
英文:

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?

调用控制器从kafka组件

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....

}

huangapple
  • 本文由 发表于 2020年6月29日 15:23:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/62633127.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定