admin管理员组

文章数量:1394984

I am running spring boot application and localstack with docker compose, it seems that I configured correctly but still throws the exception. my compose file:

version: '3.9'

services:
  localstack:
    container_name: "localstack"
    image: localstack/localstack
    ports:
      - "8080:8080"
      - "4566:4566"
      - "4510-4559:4510-4559"
    environment:
    DEBUG: 1
    volumes:
      - "./localstack/vol:/var/lib/localstack"
      - "./localstack/init:/etc/localstack/init"
    healthcheck:
    test: \[ "CMD", "curl", "-f", "http://localhost:4566/_localstack/health" \]
    interval: 30s
    timeout: 10s
    retries: 3
    start_period: 30s
    networks:
      - my_network

resource-service:
  build: ./resource-service/.
  container_name: resource-service
  depends_on:
   - localstack
  ports:
   - "8083:8083"
  environment:
    SERVER_PORT: ${RESOURCE_SERVER_PORT}
    AWS_ACCESS_KEY_ID: ${AWS_ACCESS_KEY_ID}
    AWS_SECRET_KEY: ${AWS_SECRET_KEY}
    AWS_ENDPOINT_URL: ${AWS_ENDPOINT_URL}
  env_file:
   - .env
  healthcheck:
    test: \[ "CMD", "curl", "-f", "http://localhost:8083/actuator/health" \]
    interval: 30s
    timeout: 10s
    retries: 3
    start_period: 30s
  networks:
   - my_network

networks:
  my_network:
    driver: bridge

I test connectivity with command from resource-service

docker exec -it resource-service /bin/sh 

/app # curl http://localstack:4566/_localstack/health 

and got the message:

{"services": {"acm": "available", "apigateway": "available", "cloudformation": "available", "cloudwatch": "available", "config": "available", "dynamodb": "available", "dynamodbstreams": "available", "ec2": "available", "es": "available", "events": "available", "firehose": "available", "iam": "available", "kinesis": "available", "kms": "available", "lambda": "available", "logs": "available", "opensearch": "available", "redshift": "available", "resource-groups": "available", "resourcegroupstaggingapi": "available", "route53": "available", "route53resolver": "available", "s3": "running"...}

but when I am trying to upload a file to the bucket I get error: java.UnknownHostException: module-1.localstack here, bucket module-01 is created.

in .env file AWS_ENDPOINT_URL is http://localstack:4566 and s3 client is configured in java:

@Bean
public S3Client s3Client() {
    log.info("accessKeyId: {}, endpoint: {}", accessKeyId, endpointUrl);
    return S3Client.builder()
            .region(Region.of(region))
            .endpointOverride(URI.create(endpointUrl))
            .credentialsProvider(StaticCredentialsProvider.create(
                    AwsBasicCredentials.create(accessKeyId, secretAccessKey)))
            .build();
}

I am running spring boot application and localstack with docker compose, it seems that I configured correctly but still throws the exception. my compose file:

version: '3.9'

services:
  localstack:
    container_name: "localstack"
    image: localstack/localstack
    ports:
      - "8080:8080"
      - "4566:4566"
      - "4510-4559:4510-4559"
    environment:
    DEBUG: 1
    volumes:
      - "./localstack/vol:/var/lib/localstack"
      - "./localstack/init:/etc/localstack/init"
    healthcheck:
    test: \[ "CMD", "curl", "-f", "http://localhost:4566/_localstack/health" \]
    interval: 30s
    timeout: 10s
    retries: 3
    start_period: 30s
    networks:
      - my_network

resource-service:
  build: ./resource-service/.
  container_name: resource-service
  depends_on:
   - localstack
  ports:
   - "8083:8083"
  environment:
    SERVER_PORT: ${RESOURCE_SERVER_PORT}
    AWS_ACCESS_KEY_ID: ${AWS_ACCESS_KEY_ID}
    AWS_SECRET_KEY: ${AWS_SECRET_KEY}
    AWS_ENDPOINT_URL: ${AWS_ENDPOINT_URL}
  env_file:
   - .env
  healthcheck:
    test: \[ "CMD", "curl", "-f", "http://localhost:8083/actuator/health" \]
    interval: 30s
    timeout: 10s
    retries: 3
    start_period: 30s
  networks:
   - my_network

networks:
  my_network:
    driver: bridge

I test connectivity with command from resource-service

docker exec -it resource-service /bin/sh 

/app # curl http://localstack:4566/_localstack/health 

and got the message:

{"services": {"acm": "available", "apigateway": "available", "cloudformation": "available", "cloudwatch": "available", "config": "available", "dynamodb": "available", "dynamodbstreams": "available", "ec2": "available", "es": "available", "events": "available", "firehose": "available", "iam": "available", "kinesis": "available", "kms": "available", "lambda": "available", "logs": "available", "opensearch": "available", "redshift": "available", "resource-groups": "available", "resourcegroupstaggingapi": "available", "route53": "available", "route53resolver": "available", "s3": "running"...}

but when I am trying to upload a file to the bucket I get error: java.UnknownHostException: module-1.localstack here, bucket module-01 is created.

in .env file AWS_ENDPOINT_URL is http://localstack:4566 and s3 client is configured in java:

@Bean
public S3Client s3Client() {
    log.info("accessKeyId: {}, endpoint: {}", accessKeyId, endpointUrl);
    return S3Client.builder()
            .region(Region.of(region))
            .endpointOverride(URI.create(endpointUrl))
            .credentialsProvider(StaticCredentialsProvider.create(
                    AwsBasicCredentials.create(accessKeyId, secretAccessKey)))
            .build();
}
Share Improve this question asked Mar 27 at 8:27 გენო მუმლაძეგენო მუმლაძე 1312 silver badges19 bronze badges 2
  • Are you using Spring Cloud AWS? (Of note, that library automatically provides pretty much exactly that S3Client bean.) If so, there's a Spring property you can set to make this work; see Unknown host when using localstack with Spring Cloud AWS 2.3. – David Maze Commented Mar 27 at 11:12
  • No. just localstack library for S3 – გენო მუმლაძე Commented Mar 29 at 5:58
Add a comment  | 

1 Answer 1

Reset to default 0

I have added serviceConfiguration(...)

@Bean
public S3Client s3Client() {
    return S3Client.builder()
            .region(Region.of(region))
            .endpointOverride(URI.create(endpointUrl))
            .serviceConfiguration(
                    S3Configuration.builder()
                            .pathStyleAccessEnabled(true)// Critical for LocalStack
                            .build()
            )
            .credentialsProvider(StaticCredentialsProvider.create(
                    AwsBasicCredentials.create(accessKeyId, secretAccessKey)))
            .build();
}

本文标签: