将 Springboot 应用程序隔离为电子邮件应用程序的 DEV 环境和 PROD 环境

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)

xer*_*593 5

我已经创建application-dev.propertiesapplication-prod.properties从实际application.properties

这已经很好了(除了在 git 部分暴露秘密......),你实际上已经完成了!

要激活/覆盖它们,您只需设置spring.profiles.active相应的环境即可。

这可以通过第 2 章“外部化配置”中的 14(!) 个定义的属性源之一来完成,例如:


一般提示:

  • 不要“双重维护”常见属性,例如:
    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) 就可以了!
  • 在 中使用 or devor prod(推荐)属性application.properties,这会为您添加“默认行为”(及其好处(较少关心/维护)和权衡(更多需要忘记/隐藏))

如何在 PROD 上实现配置文件激活

  • 或者使用prod属性application.properties(推荐,“选择加入”DEV+其他环境)
  • 或者以上述方式之一激活它。

如何在DEV上实现配置文件激活(使用IDE

如果我们选择 DEV 作为application.properties,那么这也可以完成(甚至可能更喜欢非常“开发驱动”/原型环境)。

如果我们选择 PROD 作为application.properties,那么我们需要对 DEV 集做基本相同的事情spring.profiles.active

  • spring.profiles.active=dev 将属性添加到我们的 maven/gradle 构建(配置,在 IDE 中)
  • 或者在我们的 spring-boot:run 上添加--spring.profiles.active=dev 参数(配置,在 IDE 中)
  • 如果你没有充分的理由,我不会将 Maven 配置文件与 Spring 配置文件混合在一起,但是如果......
  • 在 spring-boot-maven(/gradle)-plugin 上,您可以将其设置为:
    <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>
    
    Run Code Online (Sandbox Code Playgroud)
    参考

测试中

这取决于:

  • 它将在哪个(所有)环境中运行?
  • 它应该使用什么配置文件?

我们有非常方便的注释@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 Boot 不提供任何对加密属性值的内置支持,但是,它确实提供了修改 Spring 环境中包含的值所需的挂钩点。该EnvironmentPostProcessor界面允许您在应用程序启动之前操纵环境。有关详细信息,请参阅howto.html

如果您需要一种安全的方式来存储凭据和密码,Spring Cloud Vault项目提供了在HashiCorp Vault中存储外部化配置的支持。

尽管是容器/云驱动的,但 spring-boot 并没有真正支持:

Docker 的秘密

...但是有几篇文章/so-questions/github-respositories 围绕该用例。


更多链接: