微服務監控平台獲取網關(zuul)配置列表


步驟:

(1)讀取zuul的配置文件,獲取路由配置項信息;

    private static Properties props;

    static {
        String fileName = "application.properties";
        props = new Properties();
        try {
            props.load(new InputStreamReader(PropertiesUtil.class.getClassLoader().getResourceAsStream(fileName), "UTF-8"));
        } catch (IOException e) {
            log.error("配置文件讀取異常", e);
        }
    }

(2)在獲取到的配置項信息中截取出服務的路由配置信息;

    /**
     * 根據zuul.routes.KSKZT.serviceId
     * 獲取KSKZT
     * @param key
     * @return
     */
    public static String getRoutesKey(String key){
        return key.replace("zuul.routes.", "").replace(".serviceId", "");
    }
    
    /**
     * 根據KSKZT
     * 獲取zuul.routes.KSKZT.path
     * @param key
     * @return
     */
    public static String getRoutesPathKey(String key){
        return "zuul.routes."+key+".path";
    }
    
    /**
     * 根據zuul.routes.KSKZT.path
     * 獲取/KSKZT/**
     * @param pathKay
     * @return
     */
    public static String getRoutesPathPalue(String pathKay){
        return props.getProperty(pathKay.trim());
    }
    
    /**
     * 獲取系統配置的注冊名稱
     * @return
     */
    public static String getApplicationName(){
        return props.getProperty("spring.application.name");
    }
    
    /**
     * 獲取路由表
     * @return
     */
    public static List<Map<String,String>> getRouteList(){
        List<Map<String,String>> routeList = new ArrayList<>();
        for(Object key : props.keySet()){
            if(key.toString().startsWith("zuul.routes") && key.toString().endsWith(".serviceId") ){
                Map< String, String> route = new HashMap<String, String>();
                route.put("serviceName",props.get(key)+"");
                route.put("servicePath",getApplicationName()+getRoutesPathPalue(getRoutesPathKey(getRoutesKey(key.toString()))));
                routeList.add(route);
            }
        }
        return routeList;
    }
    

測試獲取到的路由表

    public static void main(String[] args) {
        List<Map<String,String>> list = getRouteList();
        for (Map<String,String> object : list) {
            System.out.println("********************");
            System.out.println(object.get("serviceName"));
            System.out.println(object.get("servicePath"));
            
        }
    }

(3)保證監控平台獲取正確的路由表;

  1)在監控平台啟動時:通過HttpClient請求zuul提供的接口,獲取路由表信息

  2)在zuul啟動時:通過的HttpClient請求監控平台的更新接口,將路由表作為參數傳遞給監控平台,監控平台接受並更新。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM