将电子邮件应用程序的 DEV 和 PROD 环境的 Springboot 应用程序分开

Segregating Springboot app for DEV and PROD Environment for the email app

我的 springboot 应用程序在 DEV 环境中运行良好,现在我想隔离 DEV 和 PROD 环境,以便它获取各自的代码。

我确实阅读了一些教程,得到了一些线索,但无法得出关于如何分离 PROD 部分的结论。

这个应用基本上是在从 angular 应用提交表单时向 gmail 发送一封电子邮件。这是我到目前为止在 springboot 应用程序上所做的以隔离它。

我从实际的 application.properties 文件创建了 application-dev.propertiesapplication-prod.properties,并添加了两个单独的 gmail 帐户,一个用于 DEV,一个用于 PROD。并将配置文件添加到 POM.xml

任何有关如何从现有代码创建 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

新申请-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@

新申请-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

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>

主要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);
    }

}

与 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!!");
    }
}

MailRequest 文件:

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;
    }
}

I have created application-dev.properties and application-prod.properties from the actual application.properties

这已经非常好(除了git部分的揭秘...),你实际上已经完成了!

对于activate/override他们,你只需要在相应的环境中设置spring.profiles.active即可。

这可以通过 14(!) defined property sources in Chapter 2, Externalized Configuration 之一来完成,例如:

  • (java.lang.)系统属性 (6.):
    java -jar -Dspring.profiles.active=prod target/myApp.jar 
    
  • 也很方便:(5.)环境变量(我们必须申请"relaxed rules"..):
    # in windows: SET SPRING_PROFILES_ACTIVE=prod
    export SPRING_PROFILES_ACTIVE=prod
    java -jar target/myApp.jar
    
  • 或 11。(与 6 不同,且优先级更高。):
    java -jar target/myApp.jar --spring.profiles.active=prod
    
  • ...(我推荐一些“低于 5”的东西,虽然它是)也可能在 application.properties (3.)

一般提示:

  • 不要“双重维护”公共属性,例如:
    spring.mail.host=smtp.gmail.com
    spring.mail.port=587
    # ...
    spring.mail.properties.mail.smtp.auth=true
    # ...
    
    对于这些单一位置 (application.properties) 没问题!
  • 使用 devprod(推荐)application.properties 中的属性,这会增加您的“默认行为”(及其好处(不太关心 about/maintain) & 权衡(更多 forget/hidden))

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

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

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

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

如果我们选择 PROD 作为 application.properties,那么我们需要为 DEV 做基本相同的设置 - 设置 spring.profiles.active:

  • spring.profiles.active=dev 属性 添加到我们的 maven/gradle 构建(配置,在 IDE 中)
  • 或者在我们的spring-boot上添加--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>
    
    reference

测试中

这取决于:

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

我们有非常方便的 annotation @ActiveProfiles,但它的值应该是相当“固定”的(对于所有环境,其中测试是 运行)。

当这还不够时,考虑引入额外的配置文件(组)/测试设置。

但我们也可以将 spring.profiles.active 属性 传递给(例如)maven-surefire/failesafe-plugin,例如:

<configuration>
  <argLine>-Dspring.profiles.active=${some.maven.property}</argLine>
</configuration>

秘密

Spring Boot (admits&) recommends:

Spring Boot does not provide any built in support for encrypting property values, however, it does provide the hook points necessary to modify values contained in the Spring Environment. The EnvironmentPostProcessor interface allows you to manipulate the Environment before the application starts. See howto.html for details.

If you need a secure way to store credentials and passwords, the Spring Cloud Vault project provides support for storing externalized configuration in HashiCorp Vault.

尽管 container/cloud-driven,spring-boot 没有真正支持:

Docker 秘密

...但是有 several articles/so-questions/github-respositories around,针对该用例。


更多链接: