Lea*_*ner 2 email environment-variables spring-boot
我的 springboot 应用程序在 DEV 环境中运行良好,现在我想隔离 DEV 和 PROD 环境,以便它获取各自的代码。
我确实浏览了一些教程并获得了一些线索,但无法就如何分离出 PROD 部分得出结论。
这个应用程序基本上在从角度应用程序提交表单时向 gmail 发送电子邮件。这就是我到目前为止在 springboot 应用程序上所做的,以将其隔离。
我已经根据实际文件创建application-dev.properties并添加了两个单独的 gmail 帐户,一个用于 DEV,一个用于 PROD。并将配置文件添加到 POM.xmlapplication-prod.propertiesapplication.properties
有关如何从现有代码创建 PROD 环境的任何帮助都将非常有帮助。我对 springboot 以及如何针对不同环境进行分析还很陌生。
application.properties 文件(实际文件)是:
server.port=8084
spring.mail.host=smtp.gmail.com
spring.mail.port=587
spring.mail.username=test1@gmail.com
spring.mail.password=xxxxxxxxxxxxx
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true
Run Code Online (Sandbox Code Playgroud)
新的 application-dev.properties 文件
server.port=8084
spring.mail.host=smtp.gmail.com
spring.mail.port=587
spring.mail.username=test1@gmail.com
spring.mail.password=xxxxxxxxxxxxxxx
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true
spring.profiles.active=@activatedProperties@
Run Code Online (Sandbox Code Playgroud)
新的 application-prod.properties 文件
server.port=8085
spring.mail.host=smtp.gmail.com
spring.mail.port=587
spring.mail.username=test2@gmail.com
spring.mail.password=xxxxxxxxxxxxxxx
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true
Run Code Online (Sandbox Code Playgroud)
POM.xml 文件
<profile>
<id>dev</id>
<properties>
<activatedProperties>dev</activatedProperties>
</properties>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
<profile>
<id>prod</id>
<properties>
<activatedProperties>prod</activatedProperties>
</properties>
</profile>
Run Code Online (Sandbox Code Playgroud)
主要的EmailClientHmwApplication.java文件如下:
package com.sami.EmailClientHMW;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class EmailClientHmwApplication {
public static void main(String[] args) {
SpringApplication.run(EmailClientHmwApplication.class, args);
}
}
Run Code Online (Sandbox Code Playgroud)
我与 DEV 一起使用的 MailController 文件如下:
package com.sami.EmailClientHMW.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.web.bind.annotation.*;
@RestController
public class MailController {
@Autowired
private JavaMailSender mailSender;
@GetMapping("/testsite")
public String Testing()
{
return "Welcome to HMW Email Client Version..New!!";
}
@PostMapping("/mail")
@CrossOrigin
public String sendmail(@RequestBody MailRequest request) {
sendSimpleEmail(request.getFrom(), request.getBody(), request.getSubject());
return "email sent successfully";
}
private void sendSimpleEmail(
String fromEmail,
String body,
String subject)
{
SimpleMailMessage message = new SimpleMailMessage();
message.setFrom("test1@gmail.com");
message.setTo("test1@gmail.com");
message.setSubject(subject);
message.setText(body);
mailSender.send(message);
System.out.println("From DEV Mail Sent Successfully..hmw!!");
}
}
Run Code Online (Sandbox Code Playgroud)
邮件请求文件:
package com.sami.EmailClientHMW.controller;
public class MailRequest {
private String from;
private String to;
private String subject;
private String body;
public String getFrom() {
return from;
}
public void setFrom(String from) {
this.from = from;
}
public String getTo() {
return to;
}
public void setTo(String to) {
this.to = to;
}
public String getSubject() {
return subject;
}
public void setSubject(String subject) {
this.subject = subject;
}
public String getBody() {
return body;
}
public void setBody(String body) {
this.body = body;
}
}
Run Code Online (Sandbox Code Playgroud)
我已经创建
application-dev.properties并application-prod.properties从实际application.properties
这已经很好了(除了在 git 部分暴露秘密......),你实际上已经完成了!
要激活/覆盖它们,您只需设置spring.profiles.active相应的环境即可。
这可以通过第 2 章“外部化配置”中的 14(!) 个spring-boot定义的属性源之一来完成,例如:
java.lang.)系统属性 (6.):
java -jar -Dspring.profiles.active=prod target/myApp.jar
Run Code Online (Sandbox Code Playgroud)
# in windows: SET SPRING_PROFILES_ACTIVE=prod
export SPRING_PROFILES_ACTIVE=prod
java -jar target/myApp.jar
Run Code Online (Sandbox Code Playgroud)
java -jar target/myApp.jar --spring.profiles.active=prod
Run Code Online (Sandbox Code Playgroud)
application.properties(3)中也是可能的spring.mail.host=smtp.gmail.com
spring.mail.port=587
# ...
spring.mail.properties.mail.smtp.auth=true
# ...
Run Code Online (Sandbox Code Playgroud)
对于这些单一位置 ( application.properties) 就可以了!devor prod(推荐)属性application.properties,这会为您添加“默认行为”(及其好处(较少关心/维护)和权衡(更多需要忘记/隐藏))prod属性application.properties(推荐,“选择加入”DEV+其他环境)如果我们选择 DEV 作为application.properties,那么这也可以完成(甚至可能更喜欢非常“开发驱动”/原型环境)。
如果我们选择 PROD 作为application.properties,那么我们需要对 DEV 集做基本相同的事情spring.profiles.active:
spring.profiles.active=dev 将属性添加到我们的 maven/gradle 构建(配置,在 IDE 中)--spring.profiles.active=dev 参数(配置,在 IDE 中)参考Run Code Online (Sandbox Code Playgroud)<project> <properties> <app.profiles>local,dev</app.profiles> </properties> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <profiles>${app.profiles}</profiles> </configuration> </plugin> </plugins> </build> </project>
这取决于:
我们有非常方便的spring-test注释@ActiveProfiles,但它的值应该是相当“固定”的(对于运行测试的所有环境)。
当这还不够时,请考虑引入额外的配置文件(组)/设置进行测试。
但我们也可以将spring.profiles.active属性传递给 (eg) maven-surefire/failesafe-plugin,例如:
<configuration>
<argLine>-Dspring.profiles.active=${some.maven.property}</argLine>
</configuration>
Run Code Online (Sandbox Code Playgroud)
Spring Boot 不提供任何对加密属性值的内置支持,但是,它确实提供了修改 Spring 环境中包含的值所需的挂钩点。该
EnvironmentPostProcessor界面允许您在应用程序启动之前操纵环境。有关详细信息,请参阅howto.html。
如果您需要一种安全的方式来存储凭据和密码,Spring Cloud Vault项目提供了在HashiCorp Vault中存储外部化配置的支持。
尽管是容器/云驱动的,但 spring-boot 并没有真正支持:
...但是有几篇文章/so-questions/github-respositories 围绕该用例。
更多链接:
| 归档时间: |
|
| 查看次数: |
8576 次 |
| 最近记录: |