AWS SQS在启用基于内容的去重时,默认情况下会考虑消息的所有属性,包括消息属性和系统属性。如果需要自定义去重规则,可以使用AWS SNS和Lambda来实现。
下面是一个示例Lambda函数,它定义了自定义的去重规则:
import json
def lambda_handler(event, context):
# Get the message content from the SNS event
message = json.loads(event['Records'][0]['Sns']['Message'])
# Get the attributes
message_attributes = message['MessageAttributes']
system_attributes = message['Attributes']
# Define the hash key based on the relevant attributes
hash_key = message_attributes['MyAttribute']['Value'] + system_attributes['SentTimestamp']
# Return the hash key as the message deduplication ID
return {
'body': message['Message'],
'deduplication_id': hash_key
}
该函数获取SNS事件中的消息内容,然后从消息属性和系统属性中获取所需的字段。根据这些字段定义哈希键,并将其作为消息去重ID返回。将此函数与SNS集成,以便在发布消息时自动调用Lambda函数进行去重。