与AWS s3的集成测试spring-cloud-config

问题描述

我有一个由AWS S3存储桶支持的配置服务器,如果我通过了region&S3存储桶名称,则只需从给定文件获取属性即可。现在,我想使用我在下面的代码中编写的localstack与s3进行集成测试,但是这很丢人

`org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'healthindicatorRegistry' defined in class path resource [org/springframework/boot/actuate/autoconfigure/health/LegacyHealthEndpointCompatibilityConfiguration.class]: Unsatisfied dependency expressed through method 'healthindicatorRegistry' parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'healthContributorRegistry' defined in class path resource [org/springframework/boot/actuate/autoconfigure/health/HealthEndpointConfiguration.class]: Bean instantiation via factory method Failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.boot.actuate.health.HealthContributorRegistry]: Factory method 'healthContributorRegistry' threw exception; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'configserverHealthindicator' defined in class path resource [org/springframework/cloud/config/server/config/EnvironmentRepositoryConfiguration.class]: Unsatisfied dependency expressed through method 'configserverHealthindicator' parameter 0; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'org.springframework.cloud.config.server.config.CompositeConfiguration': Unsatisfied dependency expressed through method 'setEnvironmentRepos' parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'awsS3EnvironmentRepository' defined in class path resource [org/springframework/cloud/config/server/config/AwsS3RepositoryConfiguration.class]: Bean instantiation via factory method Failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.cloud.config.server.environment.AwsS3EnvironmentRepository]: Factory method 'awsS3EnvironmentRepository' threw exception; nested exception is java.lang.UnsupportedOperationException: Client is immutable when created with the builder.
`

有人可以指导我在这里做错什么吗?是否没有设置任何凭据bcz localstack应该解决这个问题?是造成这种不变性吗?

@SpringBoottest(classes = configserverApplication.class,webEnvironment = RANDOM_PORT,properties = {
    "spring.cloud.config.server.awss3.endpoint = s3endpoint" }) 
@TestPropertySource("/application-test.yml")
@ContextConfiguration(initializers = configserverApplicationIT.Initializer.class)
@Testcontainers
public class configserverApplicationIT{

@Autowired
TestRestTemplate testRestTemplate;

@LocalServerPort
private int port;

private static final String BUCKET_NAME = "testbucket";
private static final String TEST_KEY = "tKey";
private static final String TEST_VALUE = "tValue";

@Autowired
AmazonS3 s3;

public static final int EDGE_PORT = 4566;
@Container
public static LocalStackContainer localstack = new LocalStackContainer("0.11.0").withServices(S3)
        .withExposedPorts(EDGE_PORT);

public static class Initializer implements ApplicationContextinitializer<ConfigurableApplicationContext> {
    @Override
    public void initialize(ConfigurableApplicationContext configurableApplicationContext) {

    String endpoint = String.format("spring.cloud.config.server.awss3.endpoint=http://%s:%s",localstack.getContainerIpAddress(),localstack.getMappedPort(EDGE_PORT)); TestPropertyValues.of(endpoint).applyTo(configurableApplicationContext); URI s3Endpoint = null;
    try {
        s3Endpoint = new URI(Localstack.INSTANCE.getEndpointS3());

        AmazonS3ClientBuilder builder = AmazonS3ClientBuilder.standard();
        builder.setEndpointConfiguration(new EndpointConfiguration(s3Endpoint.toString(),"us-east-1"));
        s3 = builder.enablePathStyleAccess().build();

        s3.createBucket(new CreateBucketRequest(BUCKET_NAME));
        s3.putObject(BUCKET_NAME,TEST_KEY,TEST_VALUE);
        S3Object s3Object = s3.getobject(BUCKET_NAME,TEST_KEY);
        System.out.println(s3Object.toString());

    } catch (URISyntaxException e) {
        // Todo Auto-generated catch block
        e.getMessage();
    }
    ResponseEntity<String> configserverResponse = testRestTemplate
            .getForEntity("http://localhost:" + port + "/config-service/testfile/profile",String.class); // testfile is the name of file which stores app properties on S3.
    }}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)