如何在apigee代理中添加动态uuid?

时间:2019-05-29 21:25:02

标签: apigee

在发送给后端的每个请求中获取随机UUID时,应该使用uuid代替{java.util.UUID.randomUUID()}是什么?

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<AssignMessage async="false" continueOnError="false" enabled="true" 
    name="HeaderAddition">
    <DisplayName>HeaderAddition</DisplayName>
    <Properties/>
    <Add>
        <Headers>
            <Header name="uuid">{java.util.UUID.randomUUID()}</Header>
        </Headers>
    </Add>
    <IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables>
    <AssignTo createNew="false" transport="http" type="request"/>
</AssignMessage>

1 个答案:

答案 0 :(得分:0)

我有解决方案。 我在预流程中添加了Javascript来生成uuid。然后创建了一个分配消息策略,以在请求中包含该变量。

JS:

 function generateUUID() { // Public Domain/MIT
    var d = new Date().getTime();
    if (typeof performance !== 'undefined' && typeof performance.now === 'function'){
        d += performance.now(); //use high-precision timer if available
    }
    return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
        var r = (d + Math.random() * 16) % 16 | 0;
        d = Math.floor(d / 16);
        return (c === 'x' ? r : (r & 0x3 | 0x8)).toString(16);
    });
}

var uuid = generateUUID();
context.setVariable('x-apigee-uuid', uuid); 

分配消息策略:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<AssignMessage async="false" continueOnError="false" enabled="true" 
name="HeaderAddition-123">
    <DisplayName>HeaderAddition-123</DisplayName>
    <Add>
        <Headers>
            <Header name="uuid">{x-apigee-uuid}</Header>
        </Headers>
    </Add>
    <IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables>
    <AssignTo createNew="false" transport="http" type="request"/>
</AssignMessage>