我有一个由AWS直接调用的AWS lambda RequestHandler
类。最终,我需要使用Spring Boot,因为我需要它能够从Spring Cloud配置服务器检索数据。在Amazon AWS中使用Spring引导/云lambda不会注入值
的问题是,代码作品,如果我从我自己的开发环境本地运行但未能在AWS上部署时注入配置值。
@Configuration
@EnableAutoConfiguration
@ComponentScan("my.package")
public class MyClass implements com.amazonaws.services.lambda.runtime.RequestHandler<I, O> {
public O handleRequest(I input, Context context) {
ApplicationContext applicationContext = new SpringApplicationBuilder()
.main(getClass())
.showBanner(false)
.web(false)
.sources(getClass())
.addCommandLineProperties(false)
.build()
.run();
log.info(applicationContext.getBean(SomeConfigClass.class).foo);
// prints cloud-injected value when running from local dev env
//
// prints "${path.to.value}" literal when running from AWS
// even though Spring Boot starts successfully without errors
}
}
@Configuration
public class SomeConfigClass {
@Value("${path.to.value}")
public String foo;
}
src/main/resources/bootstrap.yml:
spring:
application:
name: my_service
cloud:
config:
uri: http://my.server
failFast: true
profile: localdev
有什么我想:
- 使用常规的Spring MVC,但这doesn't have integration with
@Value
injection/Spring cloud。 - 使用
@PropertySource
- 但发现它不支持.yml文件 - 验证,以确保配置服务器服务请求到任何IP地址(没有IP地址过滤)
- 运行
curl
保证值带回 - 验证确保.jar实际上包含
bootstrap.yml
在jar根 - 验证确保.jar实际上包含Spring Boot类。 FWIW我正在使用Maven shade插件,它将项目打包成具有所有依赖关系的fat .jar。
注:AWS LAMBDA 不支持环境变量,因此,我不能设置像spring.application.name
事情(既不是环境变量也不是-D
参数)。我也无法控制实际启动的基础类MyClass
- 这对最终用户来说是完全透明的。我只是打包jar并提供入口点(类名),休息是照顾。
有什么我可以错过?任何方式,我可以更好地调试这个?
是你的配置服务器可从'外部'?我的意思是,你确定它没有隐藏在IP保护或类似的东西后面吗? – freakman
@freakman好点,但没有 - 只是检查,配置请求是从任何主机提供。 – mindas
我假设你已经看过这个:http://cloud.spring.io/spring-cloud-aws/spring-cloud-aws.html#_spring_boot_auto_configuration,它不适合你?我对AWS Lambda了解不多,但看到这篇文章:https://github.com/cagataygurturk/aws-lambda-java-boilerplate。也许你已经检查过这些但想确认。 –