• OpenStack学习系列-----第二篇 由一个错误看理解整个架构的重要性


       看了openstack没几天,然后就开始试着用Java调用所有的API,第一步得到Credentials的时候成功了,然后第二步,传参数使所有的server信息都列出来的时候报错404.具体描述如下(曾经发到论坛求助):

            最近搭建好openstack环境,测试API的时候出错,是404错误,涉及到的代码如下:

    [java] view plain copy
     
     在CODE上查看代码片派生到我的代码片
    1. public static Map<String, Object> get(URL url, String token) throws IOException {  
    2.         HttpURLConnection conn = getURLConnection(url);  
    3. //        conn.setRequestProperty("Content-Type", APPLICATION_JSON);  
    4.         conn.setRequestMethod(HTTP_GET);  
    5.         conn.setDoInput(true);  
    6. //        conn.setDoOutput(true);  
    7.         if (token != null) {  
    8.             System.out.println(token);  
    9.             conn.setRequestProperty("X-Auth-Token", token);  
    10.         }  
    11.         System.out.println(conn.toString());  
    12.   
    13.         if (conn.getResponseCode() == 401) {  
    14.             throw new IOException("Authorize Error occurs and the response code is :" + conn.getResponseCode() + ",and the message is:" + conn.getResponseMessage());  
    15.         }  
    16.           
    17.         System.out.println(conn.getResponseCode());  
    18.           
    19.         ObjectMapper mapper = new ObjectMapper();  
    20.         mapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);  
    21.           
    22. //        System.out.println(mapper.readValue(conn.getInputStream(), new TypeReference<Map<String, Object>>() {}));  
    23.           
    24.         return mapper.readValue(conn.getInputStream(), new TypeReference<Map<String, Object>>() {  
    25.         });  
    26.           
    27.   
    28.     }  
    29.   
    30.     /** 
    31.      * Creates the HTTP URL connection 
    32.      *  
    33.      * @param url 
    34.      *            the URL to be used to establish HTTP connection 
    35.      * @return A HttpURLConnection 
    36.      */  
    37.     private static HttpURLConnection getURLConnection(URL url) throws IOException {  
    38.         HttpURLConnection conn = (HttpURLConnection) url.openConnection();  
    39.         // disable cache  
    40.         conn.setUseCaches(false);  
    41.         // turn off HTTP keep-alive  
    42.         conn.setRequestProperty("Connection", "close");  
    43.   
    44.         return conn;  
    45.     }  

    上面的参数URL 是:http://127.0.0.1:5000/v2/2835ba0b17384840941b5c5d653fb81a/servers/detail
      报错如下:

    [plain] view plain copy
     
     在CODE上查看代码片派生到我的代码片
    1. Java code  
    2. Exception in thread "main" java.io.FileNotFoundException: http://127.0.0.1:5000/v2/2835ba0b17384840941b5c5d653fb81a/servers/detail  
    3.     at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)  
    4.     at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)  
    5.     at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)  
    6.     at java.lang.reflect.Constructor.newInstance(Constructor.java:525)  
    7.     at sun.net.www.protocol.http.HttpURLConnection$6.run(HttpURLConnection.java:1664)  
    8.     at sun.net.www.protocol.http.HttpURLConnection$6.run(HttpURLConnection.java:1662)  
    9.     at java.security.AccessController.doPrivileged(Native Method)  
    10.     at sun.net.www.protocol.http.HttpURLConnection.getChainedException(HttpURLConnection.java:1660)  
    11.     at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1243)  
    12.     at com.intel.cpa.openstack.HttpUtil.get(HttpUtil.java:148)  
    13.     at com.intel.cpa.openstack.NovaAPI.listServers(NovaAPI.java:46)  
    14.     at com.intel.cpa.openstack.NovaAPI.main(NovaAPI.java:37)  
    15. Caused by: java.io.FileNotFoundException: http://127.0.0.1:5000/v2/2835ba0b17384840941b5c5d653fb81a/servers/detail  
    16.     at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1613)  
    17.     at java.net.HttpURLConnection.getResponseCode(HttpURLConnection.java:468)  
    18.     at com.intel.cpa.openstack.HttpUtil.get(HttpUtil.java:137)  
    19.     ... 2 more  

    请问,这个是什么问题?有没有什么好的建议?
        
      最终要的是发现404问题后思路应该什么样子的?
      譬如这个,首先应该考虑构建的URL是否正确,然后这里还有一个要求是把一个值作为header传进去,也做了,请大家帮忙分析一下。
      感谢您花时间思考!

                多谢大家,已经解决了,确实是URL问题,主要原因是第一次请求后,第二次发送请求应该交由nova模块处理,所以应该换掉端口。

             主要问题在呢?第一次得到Credentials的时候,是去keystone,返回得到token,tenant等后,发送第二个请求,是应该去nova compute模块,所以,应该从第一次的返回值里面得到url,然后再发送请求。

            所以,还是要去了解openstack这个东西的整体架构

                遇到404错误后,只是一遍一遍的看,没什么用,现在想想,要是分解开始去看应该会很快的找到原因了:

                1:http还是https

                2:主机地址对不对?

                3:端口对不对?(有时候调用不同的模块可能就会换新的端口)

                4:端口后面的内容

     

  • 相关阅读:
    【server】瞎写
    【windows c++ 笔记】随笔
    【VScode】使用VScode来写markdown ② 流程图
    【VScode】使用VScode 来写markdown ① 时序图
    【windows c++ 笔记】 正则表达
    【sublime text】中文乱码问题解决过程
    【vue3】从0搭建Vue3项目
    【git】设置VScode为默认查看差异工具
    【lua实战摸索】C API&auxiliary library 使用笔记
    【vue3】监控响应: reactive/watch/watchEffect/provide.inject/computed
  • 原文地址:https://www.cnblogs.com/beautiful-code/p/6046284.html
Copyright © 2020-2023  润新知