天天看點

【Azure 事件中心】Spring Boot 內建 Event Hub(azure-spring-cloud-stream-binder-eventhubs)指定Partition Key有異常消息

問題描述

在Spring Boot應用中內建Event Hub,發送消息時指定Partition Key,日志中發現異常:

應用使用Event Hub版本為

<dependency>
            <groupId>com.azure.spring</groupId>
            <artifactId>azure-spring-cloud-stream-binder-eventhubs</artifactId>
            <version>2.5.0</version>
        </dependency>      

發送消息指定Partition Key

@RestController
public class ReactiveEventProducerController {
    private static final Logger LOGGER = LoggerFactory.getLogger(ReactiveEventProducerController.class);

    @Autowired
    private Sinks.Many<Message<String>> many;

    @PostMapping("/messages/reactive")
    public ResponseEntity<String> reactiveSendMessage(@RequestParam String message) {
        LOGGER.info("Reactive method to send message: {} to destination.", message);
        many.emitNext(MessageBuilder.withPayload(message).setHeaderIfAbsent(EventHubHeaders.PARTITION_KEY, String.valueOf(vin.hashCode())).build(), Sinks.EmitFailureHandler.FAIL_FAST);
        return ResponseEntity.ok(message);
    }

    @GetMapping("/")
    public String welcome() {
        return "welcome";
    }
}      

異常消息

{"@timestamp":"2022-04-05 13:15:08.643","level":"WARN","host":"bogon","APP":"myehapp","microservice":"ehcenter",
"class":"com.azure.spring.integration.eventhub.converter.EventHubMessageConverter","trackingID":"","spanID":"",
"data":"System property azure_partition_key(1916947495) is not allowed to be defined and will be ignored.","xcptn":""}       

問題解答

經過多次測試驗證,這個異常消息隻是一個Warning(警告),并不影響真正消息的發送。 可以使用Service Bus Explorer( https://github.com/paolosalvatori/ServiceBusExplorer )工具進行檢視發送端發送的消息。此外,在消費資料的時候,加上EventHubHeaders.RAW_PARTITION_ID 就可以檢視分區情況。

message.getHeaders().get(EventHubHeaders.RAW_PARTITION_ID))      
【Azure 事件中心】Spring Boot 內建 Event Hub(azure-spring-cloud-stream-binder-eventhubs)指定Partition Key有異常消息

當在複雜的環境中面臨問題,格物之道需:濁而靜之徐清,安以動之徐生。 雲中,恰是如此!

繼續閱讀