1. 程式人生 > >對接阿里雲車型庫介面,Access-Control-Allow-Headers 通過使用者流量控制節流

對接阿里雲車型庫介面,Access-Control-Allow-Headers 通過使用者流量控制節流

其中有三個介面,車型的資訊分為四個層級,1-品牌,2-汽車分公司即廠商,3-車系,4-車型;

介面一:無引數呼叫,阿里雲返回172個品牌資訊(當前資料量,阿里半個月一次更新);

介面二:使用品牌id作為parentid呼叫,返回depth為2,3,4的資料;其中第四層級的車型資訊為簡單資訊;

介面三:使用具體的車型id作為引數carid呼叫,返回具體的車型資訊,很詳細;

注意,再設定引數時,requestDto.setParentid("1")或者requestDto.setCarid("41375");

設定的那個實體類的引數,parentid和carid,不能寫成parentId和carId!

我在呼叫了介面一,將品牌資訊遍歷入庫,在使用品牌id作為引數直接呼叫介面二時,只能得到一部分(五六個廠商)資訊,

後臺報錯:Access-Control-Allow-Headers,返回的錯誤程式碼資訊是403,通過與阿里技術員溝通得知:

是"通過使用者流量控制節流",一種流量控制,阿里的這個介面,有限制:一分鐘最大訪問次數三十次!

沒有其他辦法避免這個限制!

解決辦法:在自己的程式碼中,執行緒休眠!2秒即可!

另外,在遍歷車廠商資訊,獲取每個廠商的車系list,再獲取每個車系的車型list,取出其中的車型id呼叫介面三時,

有的車系,沒有車型!getList為null!要多加一個非null判斷,再去取車型list!

阿里雲返回的車型詳情中的顏色欄位: 

是這樣:

    魔力黑,#000000|季風灰,#2c5866|海南藍,#48689e|白鯨棕,#574126|西拉紅,#681529|喀納斯綠

或者是這樣: 

    幽靈黨灰,傳奇黑,#000000|探索藍,#858585

或者這樣:
    傳奇黑,#000000|探索藍,#9e0f0f|白金色金屬漆,#9f9c8e|

可以在自己的資料庫車型詳情表中加一個欄位,儲存處理之後的顏色欄位:

   /**
     * ","
     */
    public static final String SPLIT_0                         = ",";

    /**
     * "|"
     */
    public static final String SPLIT_1                         = "|";

    /**
     * "#"
     */
    public static final String SPLIT_2                         = "#";

    /**
     * "%s%s"
     */
    public static final String STRING_FORMAT_2                 = "%s%s";

    /**
     * "%s%s%s"
     */
    public static final String STRING_FORMAT_3                 = "%s%s%s";

/**
     * 處理車型顏色,返回正確的顏色
     * 
     * @param color 阿里雲返回的車型詳情的顏色: 聖邦藍,#375F92|天雲灰,#6E6E70|米薩諾紅,#E52B2C 或者 :
     *            幽靈黨灰,傳奇黑,#000000|探索藍,#858585 或者:
     *            傳奇黑,#000000|探索藍,#9e0f0f|白金色金屬漆,#9f9c8e|
     * @return 處理之後的顏色: 聖邦藍,天雲灰,米薩諾紅
     */
    @Override
    public String genColorList(String color) {
        log.info("傳入的顏色color:{} ", color);
        String colorList = StringUtils.EMPTY;
        // 根據","分割
        for (String colorArr : color.split(VehicleConstants.SPLIT_0)) {
            // 含有 |
            if (colorArr.contains(VehicleConstants.SPLIT_1)) {
                String colorSubstring = colorArr.substring(colorArr.indexOf(VehicleConstants.SPLIT_1) + 1);
                if (StringUtils.isNotBlank(colorSubstring)) {
                    colorList = colorList + colorSubstring + VehicleConstants.SPLIT_0;
                }
            } else {
                // 含有 # 
                if (colorArr.contains(VehicleConstants.SPLIT_2)) {
                    colorList = String.format(VehicleConstants.STRING_FORMAT_2, colorList,
                            colorArr.substring(0, colorArr.indexOf(VehicleConstants.SPLIT_2)));
                } else {
                    colorList = String.format(VehicleConstants.STRING_FORMAT_3, colorList, colorArr,
                            VehicleConstants.SPLIT_0);
                }
            }
        }
        colorList = colorList.substring(0, colorList.length() - 1);
        log.info("處理完的顏色colorList:{} ", colorList);
        return colorList;
    }