Spring Boot Kafka消费者应用程序实现心跳

时间:2018-08-21 02:44:48

标签: spring-mvc spring-boot apache-kafka kafka-consumer-api

下面是我的春季启动kafka消费者应用程序,用于从kafka主题读取数据。在此应用程序中,我们计划通过@schduling注释功能性地实现心跳,以将其心跳发布到url,以了解其运行和运行情况(这会将我的json输入数据加载到db)。此发布请求的目的是更新应用程序监视工具上的状态。

为实现这一点,我将心跳代码放置到了应用程序的许多地方,但是 我无法达到这个要求,因为@postconstuct或Consumer.poll()不允许运行心跳代码。

我们正在使用apache kafka 2.12,在我的Spring Boot应用程序中实施此行为的正确方法是什么?是他们的任何其他api来执行此类发布请求以发送URL,每隔几分钟就会遍历整个应用程序?编写后台线程将解决此问题,请分享吗?为什么postconstuct()或poll()阻止其他递归代码运行。 请帮我。预先感谢。

@SpringBootApplication
@EnableScheduling 
public class KafkaApp {

    @Autowired
    ConsumerService kcService;

    public static void main(String[] args) {
        SpringApplication.run(KafkaApp.class, args);
    }

    @PostConstruct
    public void init(){
        kcService.getMessagesFromKafka();
    }   
}
  

和2个@Service定义:

import org.apache.kafka.clients.consumer.Consumer;

@Service public class ConsumerService { 
    final Consumer<Long, String> consumer = createConsumer();
    final int giveUp = 100; 
    int noRecordsCount = 0;

    while (true) {
        final ConsumerRecords<Long, String> consumerRecords = consumer.poll(1000);
        if (consumerRecords.count()==0) { 
            noRecordsCount++; 
            if (noRecordsCount > giveUp) break; 
            else continue;
            }     
            consumerRecords.forEach(record -> { 
                System.out.printf("Consumer Record:(%d, %s, %d, %d)\n",
              record.key(), record.value(),
              record.partition(), record.offset()); 
              }); 
              consumer.commitAsync();
              } 
    }
@Scheduled(fixedDelay = 180000)
        public void heartbeat() {
            RestTemplate restTemplate = new RestTemplate();
            String url = "endpoint url";
            String requestJson = "{\"I am alive\":\"App name?\"}";
            HttpHeaders headers = new HttpHeaders();
            headers.setContentType(MediaType.APPLICATION_JSON);    
            HttpEntity<String> entity = new HttpEntity<String>(requestJson,headers);
            String answer = restTemplate.postForObject(url, entity, String.class);
            System.out.println(answer);
    }

1 个答案:

答案 0 :(得分:0)

在您的主类中添加注释,例如:

import argparse
import paramiko
class add():
   def __init__(self):
      pass

   def execute_remote_command(self,Host, Port, User_name, Pwd, Cmd):
       ssh = paramiko.SSHClient()
       ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
       ssh.connect(Host, Port, User_name, Pwd, Cmd)

       stdin,stdout,stderr=ssh.exec_command(Cmd)
       outlines = stdout.readlines()
       resp = ''.join(outlines)
       ans = str(resp)
       print(ans)

obj = add()
parser = argparse.ArgumentParser(description='Pinging a server')
parser.add_argument('-host','--Host',dest = 'Host',help='IP of server')
parser.add_argument('-port','--Port',dest = 'Port',help='Port of server')
parser.add_argument('-username','--User_name',dest 
='User_name',help='User_name)
parser.add_argument('-pwd','--Pwd',dest = 'Pwd',help='Pwd of server')
parser.add_argument('-cmd','--Cmd',dest = 'Cmd',help='Cmd of server')
args = parser.parse_args()


obj.execute_remote_command(args.Host, args.Port, args.User_name, args.Pwd, args.Cmd)

有关更多详细信息,spring-boot-task-scheduling-with-scheduled-annotation您可以访问以下链接:

如果您要为此目的编写cron作业,则在@SpringBootApplication @EnableScheduling public class KafkaApp { @Autowired ConsumerService kcService; public static void main(String[] args) { SpringApplication.run(KafkaApp.class, args); } @PostConstruct public void init(){ kcService.getMessagesFromKafka(); } } 中添加以下内容:

application.properties

您可以在线制作cron表达式,以下是链接:cron-expression-generator-quartz

在您的心跳功能中,将上述功能编写为:

cron.expression=5 0 0 ? * * *   //Its means it'll execute every 5 sec