Amazon应用程序配置从Spring Boot

7nbnzgx9  于 2022-12-29  发布在  Spring
关注(0)|答案(2)|浏览(98)

我如何在我的spring Boot 应用程序中从aws appconfig访问配置?
既然appconfig是一个新服务,那么我们是否可以使用任何java sdk,因为我在https://github.com/aws/aws-sdk-java/tree/master/src/samples中还没有看到appconfig的任何内容

wgx48brx

wgx48brx1#

下面是我如何将AWS AppConfig集成到我的Sping Boot 项目中的。
首先,让我们确保在pom.xml中有这样的依赖关系:

<dependency>
      <groupId>com.amazonaws</groupId>
      <artifactId>aws-java-sdk-appconfig</artifactId>
      <version>1.12.134</version>
 </dependency>

接下来,让我们为自己的AWS AppConfig客户端创建一个简单的配置类:

@Configuration
public class AwsAppConfiguration {

    private static final Logger LOGGER = LoggerFactory.getLogger(AwsAppConfiguration.class);

    private final AmazonAppConfig appConfig;
    private final GetConfigurationRequest request;

    public AwsAppConfiguration() {
        appConfig = AmazonAppConfigClient.builder().build();
        request = new GetConfigurationRequest();
        request.setClientId("clientId");
        request.setApplication("FeatureProperties");
        request.setConfiguration("JsonProperties");
        request.setEnvironment("dev");
    }

    public JSONObject getConfiguration() throws UnsupportedEncodingException {
        GetConfigurationResult result = appConfig.getConfiguration(request);
        String message = String.format("contentType: %s", result.getContentType());
        LOGGER.info(message);

        if (!Objects.equals("application/json", result.getContentType())) {
            throw new IllegalStateException("config is expected to be JSON");
        }

        String content = new String(result.getContent().array(), "ASCII");
        return new JSONObject(content).getJSONObject("feature");
    }
}

最后,让我们创建一个从AWS AppConfig轮询配置的计划任务:

@Configuration
@EnableScheduling
public class AwsAppConfigScheduledTask {

    private static final Logger LOGGER = LoggerFactory.getLogger(AwsAppConfigScheduledTask.class);

    @Autowired
    private FeatureProperties featureProperties;

    @Autowired
    private AwsAppConfiguration appConfiguration;

    @Scheduled(fixedRate = 5000)
    public void pollConfiguration() throws UnsupportedEncodingException {
        LOGGER.info("polls configuration from aws app config");
        JSONObject externalizedConfig = appConfiguration.getConfiguration();
        featureProperties.setEnabled(externalizedConfig.getBoolean("enabled"));
        featureProperties.setLimit(externalizedConfig.getInt("limit"));
    }

}

我遇到了这个问题,因为我也试图找出如何最好地将AWS AppConfig集成到Sping Boot 中。
这是我写的一篇文章,你可以在这里访问:https://levelup.gitconnected.com/create-features-toggles-using-aws-appconfig-in-spring-boot-7454b122bf91
另外,源代码可以在github上找到:https://github.com/emyasa/medium-articles/tree/master/aws-spring-boot/app-config

0yycz8jy

0yycz8jy2#

首先我添加了依赖项

<dependency>
        <groupId>software.amazon.awssdk</groupId>
        <artifactId>appconfig</artifactId>
        <version>2.18.41</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/software.amazon.awssdk/appconfigdata -->
    <dependency>
        <groupId>software.amazon.awssdk</groupId>
        <artifactId>appconfigdata</artifactId>
        <version>2.19.4</version>
    </dependency>

然后构建客户端

client = AppConfigDataClient.builder()
            .credentialsProvider(() -> AwsBasicCredentials.create("<your id>", "your secret key"))
            .region(Region.<your region>)
            .build();

使用客户端启动配置会话

StartConfigurationSessionRequest startConfigurationSessionRequest = StartConfigurationSessionRequest.builder()
            .applicationIdentifier("<your application id>")
            .environmentIdentifier("your environment id")
            .configurationProfileIdentifier("your config id")
            .build();

在开始时获取会话令牌并将其用于初始调用。

String sessionToken = client.startConfigurationSession(startConfigurationSessionRequest).initialConfigurationToken();
    
    
    GetLatestConfigurationRequest latestConfigurationRequest = GetLatestConfigurationRequest.builder()
            .configurationToken(sessionToken)
            .build();
    GetLatestConfigurationResponse latestConfigurationResponse = client.getLatestConfiguration(latestConfigurationRequest);
    String response =  latestConfigurationResponse.configuration().asUtf8String();

您可以使用响应中的下一个可用令牌进行下一次调用。令牌可以根据需要进行缓存。

相关问题