从本地连接到Cloudant DB时出现连接错误

时间:2016-01-15 10:21:50

标签: java ibm-cloud cloudant

我是cloudant的新手。我编写了连接cloudant DB的代码,这是DBaas 在bluemix中。 我已在本地编写代码来连接cloudant DB。以下是代码

try {
            user="4728f43d-fcd2-41f0-be63-8945a78a9dab-bluemix";
            password="xxxxxxxxxxxxxxxx";
            url="https://4728f43d-fcd2-41f0-be63-8945a78a9dab-bluemix:xxxxxxxxxxxxxxxx@4728f43d-fcd2-41f0-be63-8945a78a9dab-bluemix.cloudant.com";

            System.setProperty("http.proxyHost", "9.184.9.13");
            System.setProperty("http.proxyPort", "80");

            //user="8452e722-8945-47d9-b372-ede45e55a7a7-bluemix";
            //password="xxxxxxxxxxxxxxxx";
            //url="https://8452e722-8945-47d9-b372-ede45e55a7a7-bluemix:xxxxxxxxxxxxxxxx@8452e722-8945-47d9-b372-ede45e55a7a7-bluemix.cloudant.com";

            CloudantClient client = ClientBuilder.account(user)
                    .username(user)
                    .password(password)
                    .build();
            return client;
        } catch (CouchDbException e) {
            throw new RuntimeException("Unable to connect to repository", e);
    }

我在Web范围应用程序Liberty配置文件上本地部署了应用程序。 在访问数据库连接时,我遇到了异常。请告诉我连接cloudant DB需要做什么。 我正在使用cloudant-client-2.2.0.jar来访问cloudant DB

[ERROR   ] Failed to read cookie response header
Connection refused: connect
[ERROR   ] Failed to get response code from request
Connection refused: connect
[err] java.lang.RuntimeException: DB Not found
[err]   at com.ibm.util.CloudantClientMgr.getDB(CloudantClientMgr.java:106)
[err]   at com.ibm.dao.CloudantDaoImpl.getSiteList(CloudantDaoImpl.java:54)
[err]   at com.ibm.service.SocialKeyManagerImpl.getSiteList(SocialKeyManagerImpl.java:60)
[err]   at com.ibm.actions.BaseAction.execute(BaseAction.java:56)
[err]   at org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:484)
[err]   at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:274)
[err]   at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1482)
[err]   at org.apache.struts.action.ActionServlet.doGet(ActionServlet.java:507)
[err]   at javax.servlet.http.HttpServlet.service(HttpServlet.java:687)
[err]   at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
[err]   at com.ibm.ws.webcontainer.servlet.ServletWrapper.service(ServletWrapper.java:1287)
[err]   at [internal classes]
[err]   at org.displaytag.filter.ResponseOverrideFilter.doFilter(ResponseOverrideFilter.java:125)
[err]   at com.ibm.ws.webcontainer.filter.FilterInstanceWrapper.doFilter(FilterInstanceWrapper.java:207)
[err]   at [internal classes]
[err]   at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
[err]   at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
[err]   at java.lang.Thread.run(Thread.java:745)
[err] Caused by: com.cloudant.client.org.lightcouch.CouchDbException: Error retrieving server response
[err]   at com.cloudant.client.org.lightcouch.CouchDbClient.execute(CouchDbClient.java:535)
[err]   at com.cloudant.client.org.lightcouch.CouchDbClient.executeToInputStream(CouchDbClient.java:550)
[err]   at com.cloudant.client.org.lightcouch.CouchDbClient.put(CouchDbClient.java:361)
[err]   at com.cloudant.client.org.lightcouch.CouchDbClient.put(CouchDbClient.java:351)
[err]   at com.cloudant.client.org.lightcouch.CouchDatabaseBase.create(CouchDatabaseBase.java:437)
[err]   at com.cloudant.client.org.lightcouch.CouchDatabaseBase.<init>(CouchDatabaseBase.java:61)
[err]   at com.cloudant.client.org.lightcouch.CouchDatabase.<init>(CouchDatabase.java:26)
[err]   at com.cloudant.client.org.lightcouch.CouchDbClient.database(CouchDbClient.java:165)
[err]   at com.cloudant.client.api.CloudantClient.database(CloudantClient.java:216)
[err]   at com.ibm.util.CloudantClientMgr.getDB(CloudantClientMgr.java:103)
[err]   ... 37 more
[err] Caused by: java.net.ConnectException: Connection refused: connect
[err]   at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
[err]   at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
[err]   at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
[err]   at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
[err]   at sun.net.www.protocol.http.HttpURLConnection$6.run(HttpURLConnection.java:1676)
[err]   at sun.net.www.protocol.http.HttpURLConnection$6.run(HttpURLConnection.java:1674)
[err]   at java.security.AccessController.doPrivileged(Native Method)
[err]   at sun.net.www.protocol.http.HttpURLConnection.getChainedException(HttpURLConnection.java:1672)
[err]   at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1245)
[err]   at java.net.HttpURLConnection.getResponseCode(HttpURLConnection.java:468)
[err]   at sun.net.www.protocol.https.HttpsURLConnectionImpl.getResponseCode(HttpsURLConnectionImpl.java:338)
[err]   at com.cloudant.client.org.lightcouch.CouchDbClient.execute(CouchDbClient.java:483)
[err]   ... 46 more
[err] Caused by: java.net.ConnectException: Connection refused: connect
[err]   at java.net.DualStackPlainSocketImpl.waitForConnect(Native Method)
[err]   at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:85)
[err]   at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:339)
[err]   at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:200)
[err]   at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:182)
[err]   at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
[err]   at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
[err]   at java.net.Socket.connect(Socket.java:579)
[err]   at sun.security.ssl.SSLSocketImpl.connect(SSLSocketImpl.java:625)
[err]   at sun.net.NetworkClient.doConnect(NetworkClient.java:175)
[err]   at sun.net.www.http.HttpClient.openServer(HttpClient.java:432)
[err]   at sun.net.www.http.HttpClient.openServer(HttpClient.java:527)
[err]   at sun.net.www.protocol.https.HttpsClient.<init>(HttpsClient.java:275)
[err]   at sun.net.www.protocol.https.HttpsClient.New(HttpsClient.java:371)
[err]   at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.getNewHttpClient(AbstractDelegateHttpsURLConnection.java:191)
[err]   at sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:933)
[err]   at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(AbstractDelegateHttpsURLConnection.java:177)
[err]   at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1301)
[err]   at java.net.HttpURLConnection.getResponseCode(HttpURLConnection.java:468)
[err]   at sun.net.www.protocol.https.HttpsURLConnectionImpl.getResponseCode(HttpsURLConnectionImpl.java:338)
[err]   at com.cloudant.http.interceptors.CookieInterceptor.interceptResponse(CookieInterceptor.java:102)
[err]   at com.cloudant.http.HttpConnection.execute(HttpConnection.java:244)
[err]   at com.cloudant.client.org.lightcouch.CouchDbClient.execute(CouchDbClient.java:475)
[err]   ... 46 more

3 个答案:

答案 0 :(得分:2)

您使用的代理设置适用于HTTP。使用CloudantClient方法创建account时,默认情况下将使用HTTPS over HTTP。使用HTTPS代理可以解决您的问题。

答案 1 :(得分:0)

在上面的代码中,您似乎使用了http的系统属性。如JVM proxies documentation中所述,http vs https的JVM范围代理设置需要不同的系统属性。对于http s ,它们是:

https.proxyHost
https.proxyPort

值得注意的是,您还可以设置专门用于CloudantClient的代理,而不是使用JVM宽代理的系统属性。例如:

CloudantClient client = ClientBuilder.account(user)
                                     .username(user)
                                     .password(password)
                                     .proxyURL(new URL("https://9.184.9.13"))
                                     .build();

我认为此选项也适用于代理仅具有http端点的情况,例如proxyURL(new URL("http://9.184.9.13")),但很明显,在这种情况下,您的通信对于客户端和代理之间的支路而言是未加密的。

答案 2 :(得分:0)

代理配置仅在我明确设置时才适用于我:

Authenticator authenticator = new Authenticator() {

                public PasswordAuthentication getPasswordAuthentication() {
                    return (new PasswordAuthentication(cloudantProxyUsername,
                            cloudantProxyPassword.toCharArray()));
                }
            };
            Authenticator.setDefault(authenticator);