Dynamo DB Local - Connection Refused

梦想的初衷 提交于 2020-03-12 03:25:30

问题


I am trying to connect to Local Dynamo DB using the AWS Java SDK. So I installed the Local Dynamo DB and started the javascript shell. All works fine and the shell starts at the usual address http://localhost:8000/shell/

Now when I try to access the Dynamo DB Instance via the AWS SDK things start to break.

Here is my code:

public class MyDynamoDB {
    private AmazonDynamoDBClient client;

    public MyDynamoDB() {
        client = new AmazonDynamoDBClient();
        client.setEndpoint("http://localhost:8000");
    }

    public void saveAndLoad() {
        DynamoDBMapperConfig config = new DynamoDBMapperConfig(new TableNameOverride("xyz"));
        DynamoDBMapper mapper = new DynamoDBMapper(client, config);
        Data data = new Data();
        ...
        mapper.save(data);


        //check if persisted
        Data d = mapper.load(Data.class, "Key");
        if (d != null) {
            System.out.println(" Found data: " + d.getStuff());
        } else {
            System.out.println("Data not found");
        }
    }
}

On running this I am getting the following stack trace

Nov 19, 2015 4:00:47 PM com.amazonaws.http.AmazonHttpClient executeHelper
INFO: Unable to execute HTTP request: Connection refused: connect
java.net.ConnectException: Connection refused: connect
at java.net.DualStackPlainSocketImpl.waitForConnect(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at org.apache.http.conn.scheme.PlainSocketFactory.connectSocket(PlainSocketFactory.java:117)
at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:177)
at org.apache.http.impl.conn.ManagedClientConnectionImpl.open(ManagedClientConnectionImpl.java:304)

回答1:


You need to trigger DynamoDB in command prompt..

Go the location where the Dynamodb cli is installed and run the following command

java -Djava.library.path=./DynamoDBLocal_lib -jar DynamoDBLocal.jar -sharedDb

verify if it is running by http://localhost:8000/shell/




回答2:


Just fought with the same issue - append a slash to the endpoint url solved it for me.

client.setEndpoint( "http://localhost:8000/");



回答3:


If you are using SAM-LOCAL or developing aws lambda function using eclipse this answer can be useful.

Short Answer assign an IP alias to loopback to reach out using a different address to 127.0.0.1, and update the endpoint to that address

From the command line run

ifconfig lo0 alias 172.16.123.1

And from lambda code instead of accessing http://localhost:8000/ use http://172.16.123.1:8000/

refer to this link for explanation.

Long Answer

In lambda function pom

<dependency>
    <groupId>com.amazonaws</groupId>
    <artifactId>aws-java-sdk-dynamodb</artifactId>
    <version>1.11.313</version>
</dependency>

started dynomodb using

java -Djava.library.path=./DynamoDBLocal_lib -jar DynamoDBLocal.jar -sharedDb

do IP alias

ifconfig lo0 alias 172.16.123.1

For accessing DynmoDb from Code do

DynamoDB dynamoDb = new DynamoDB(AmazonDynamoDBClientBuilder.standard().withCredentials(new EnvironmentVariableCredentialsProvider()).withEndpointConfiguration(new AwsClientBuilder.EndpointConfiguration("http://172.16.123.1:8000/", "local")).build());

After you are done with testing lambda function do

ifconfig lo0 -alias 172.16.123.1


来源:https://stackoverflow.com/questions/33801460/dynamo-db-local-connection-refused

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!