VIJ*_*VIJ 6 amazon-web-services spring-boot aws-sdk aws-app-config
如何在 spring boot 应用程序中从 aws appconfig 访问配置?
由于 appconfig 是一项新服务,是否有任何我们可以使用的 java sdk,因为我在https://github.com/aws/aws-sdk-java/tree/master/src/samples 中还没有看到任何有关 appconfig 的内容
小智 1
以下是我如何将 AWS AppConfig 集成到我的 Spring Boot 项目中。
\n首先,让\xe2\x80\x99s 确保我们的 pom.xml 中有此依赖项:
\n <dependency>\n <groupId>com.amazonaws</groupId>\n <artifactId>aws-java-sdk-appconfig</artifactId>\n <version>1.12.134</version>\n </dependency>\nRun Code Online (Sandbox Code Playgroud)\n接下来,让\xe2\x80\x99s创建一个我们自己的AWS AppConfig客户端的简单配置类:
\n@Configuration\npublic class AwsAppConfiguration {\n\n private static final Logger LOGGER = LoggerFactory.getLogger(AwsAppConfiguration.class);\n\n private final AmazonAppConfig appConfig;\n private final GetConfigurationRequest request;\n\n public AwsAppConfiguration() {\n appConfig = AmazonAppConfigClient.builder().build();\n request = new GetConfigurationRequest();\n request.setClientId("clientId");\n request.setApplication("FeatureProperties");\n request.setConfiguration("JsonProperties");\n request.setEnvironment("dev");\n }\n\n public JSONObject getConfiguration() throws UnsupportedEncodingException {\n GetConfigurationResult result = appConfig.getConfiguration(request);\n String message = String.format("contentType: %s", result.getContentType());\n LOGGER.info(message);\n\n if (!Objects.equals("application/json", result.getContentType())) {\n throw new IllegalStateException("config is expected to be JSON");\n }\n\n String content = new String(result.getContent().array(), "ASCII");\n return new JSONObject(content).getJSONObject("feature");\n }\n}\nRun Code Online (Sandbox Code Playgroud)\n最后,让\xe2\x80\x99s 创建一个计划任务,用于从 AWS AppConfig 轮询配置:
\n@Configuration\n@EnableScheduling\npublic class AwsAppConfigScheduledTask {\n\n private static final Logger LOGGER = LoggerFactory.getLogger(AwsAppConfigScheduledTask.class);\n\n @Autowired\n private FeatureProperties featureProperties;\n\n @Autowired\n private AwsAppConfiguration appConfiguration;\n\n @Scheduled(fixedRate = 5000)\n public void pollConfiguration() throws UnsupportedEncodingException {\n LOGGER.info("polls configuration from aws app config");\n JSONObject externalizedConfig = appConfiguration.getConfiguration();\n featureProperties.setEnabled(externalizedConfig.getBoolean("enabled"));\n featureProperties.setLimit(externalizedConfig.getInt("limit"));\n }\n\n}\nRun Code Online (Sandbox Code Playgroud)\n我遇到了这个问题,因为我也在尝试找出如何最好地将 AWS AppConfig 集成到 Spring Boot 中。
\n这是我创建的一篇文章。您可以在这里访问它:https://levelup.gitconnected.com/create-features-toggles-using-aws-appconfig-in-spring-boot-7454b122bf91
\n另外,源代码可在 github 上找到:https://github.com/emyasa/medium-articles/tree/master/aws-spring-boot/app-config
\n| 归档时间: |
|
| 查看次数: |
595 次 |
| 最近记录: |