“mitao”
2026-04-10 2b306afe79cd8096dd9394b0d8d04ff467b7e09c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package com.xiaopeng.automaticPilot.util;
 
import com.fasterxml.jackson.databind.ObjectMapper;
import com.xiaopeng.api.dto.automaticPilot.VehicleEventDTO;
import lombok.extern.slf4j.Slf4j;
import org.apache.kafka.clients.producer.*;
import org.apache.kafka.common.serialization.StringSerializer;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
 
import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.concurrent.Future;
 
@Slf4j
@Component
public class VehicleBuriedPointProducer {
 
    // 预发环境 Kafka 集群
 
    @Value("${spring.kafka.bootstrap-servers}")
    private String bootstrapServers;
 
    // 目标 Topic
    private static final String TOPIC = "topic-robotaxi-vehicle-buriedPoint";
 
    private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
 
    private Properties kafkaProperties;
    @PostConstruct
    public void init() {
        kafkaProperties = buildProperties();
    }
 
    /**
     * 发送埋点消息
     *
     * @param event
     * @throws Exception
     */
    public void sendVehicleEvent(VehicleEventDTO event){
        if (kafkaProperties == null) {
            kafkaProperties = buildProperties();
        }
        KafkaProducer<String, String> producer = new KafkaProducer<>(kafkaProperties);
        try {
            // 转 JSON
            String messageBody = OBJECT_MAPPER.writeValueAsString(event);
 
            // 建议用 vin 作为 key(保证同车有序)
            ProducerRecord<String, String> record =
                    new ProducerRecord<>(TOPIC, event.getVin(), messageBody);
            Future<RecordMetadata> future = producer.send(record);
            RecordMetadata metadata = future.get();
            log.info("发送成功:");
            log.info("Topic: " + metadata.topic());
            log.info("Partition: " + metadata.partition());
            log.info("Offset: " + metadata.offset());
        } catch (Exception e) {
            log.error("发送失败:{}", e.getMessage());
            e.printStackTrace();
        } finally {
            producer.close();
        }
    }
 
    /**
     * 构造埋点消息体
     *
     * @param vin
     * @param orderId
     * @param xpuId
     * @param eventKey
     * @param eventTime
     * @param extraAttributes
     * @return
     */
    public VehicleEventDTO buildVehicleEvent(String vin, String orderId, String xpuId,
                                          String eventKey, Long eventTime,
                                          Map<String, Object> extraAttributes) {
        VehicleEventDTO event = new VehicleEventDTO();
        event.setVin(vin);
        event.setXpuId(xpuId != null ? xpuId : "");
        event.setOrderId(orderId != null ? orderId : "");
        event.setEventKey(eventKey);
        event.setEventTime(eventTime != null ? eventTime : System.currentTimeMillis());
 
        Map<String, Object> attributes = new HashMap<>();
        attributes.put("vin", vin);
        attributes.put("order_id", orderId != null ? orderId : "");
        attributes.put("timestamp", event.getEventTime());
 
        if (extraAttributes != null) {
            attributes.putAll(extraAttributes);
        }
        event.setAttributes(attributes);
        return event;
    }
 
    private Properties buildProperties() {
        Properties props = new Properties();
 
        props.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrapServers);
        props.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class.getName());
        props.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class.getName());
 
        // 生产建议配置
        props.put(ProducerConfig.ACKS_CONFIG, "all");
        props.put(ProducerConfig.RETRIES_CONFIG, 3);
        props.put(ProducerConfig.LINGER_MS_CONFIG, 5);
        props.put(ProducerConfig.BATCH_SIZE_CONFIG, 16384);
        props.put(ProducerConfig.ENABLE_IDEMPOTENCE_CONFIG, true);
 
        return props;
    }
 
//    public static void main(String[] args) throws Exception {
//
//        long timeMillis = System.currentTimeMillis();
//
//        // 构造埋点消息体(示例)
//        VehicleEvent event = new VehicleEvent();
//        event.setVin("L1NSPGHB7SB018421");
//        event.setXpuId("");
//        event.setOrderId("");
//        event.setEventKey("RT_web_vehicle_status_changed");
//        event.setEventTime(timeMillis);
//
//        Map<String, Object> attributes = new HashMap<>();
//        attributes.put("vin", "L1NSPGHB7SB018421");
//        attributes.put("order_id", "");
//        attributes.put("timestamp", timeMillis);
//        attributes.put("sub_status", "");
//        attributes.put("prev_sub_status", "");
//        event.setAttributes(attributes);
//
//        // 转 JSON
//        String messageBody = OBJECT_MAPPER.writeValueAsString(event);
//
//        // 建议用 vin 作为 key(保证同车有序)
//        ProducerRecord<String, String> record =
//                new ProducerRecord<>(TOPIC, event.getVin(), messageBody);
//
//        try {
//            Future<RecordMetadata> future = producer.send(record);
//            RecordMetadata metadata = future.get();
//
//            System.out.println("发送成功:");
//            System.out.println("Topic: " + metadata.topic());
//            System.out.println("Partition: " + metadata.partition());
//            System.out.println("Offset: " + metadata.offset());
//
//        } catch (Exception e) {
//            e.printStackTrace();
//        } finally {
//            producer.close();
//        }
//    }
}