我的 springboot 應用程式在 DEV 環境中運行良好,現在我想分離 DEV 和 PROD 環境,以便它獲取各自的代碼。
我確實通過了一些教程得到了一些線索,但無法就如何分離出 PROD 部分得出結論。
這個應用程式基本上會在從 Angular 應用程式提交表單時向 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
[email protected]
spring.mail.password=xxxxxxxxxxxxx
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true
新的 application-dev.properties 檔案
server.port=8084
spring.mail.host=smtp.gmail.com
spring.mail.port=587
[email protected]
spring.mail.password=xxxxxxxxxxxxxxx
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true
spring.profiles.active=@activatedProperties@
新的 application-prod.properties 檔案
server.port=8085
spring.mail.host=smtp.gmail.com
spring.mail.port=587
[email protected]
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("[email protected]");
message.setTo("[email protected]");
message.setSubject(subject);
message.setText(body);
mailSender.send(message);
System.out.println("From DEV Mail Sent Successfully..hmw!!");
}
}
郵件請求檔案:
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;
}
}
uj5u.com熱心網友回復:
我已經創建
application-dev.properties并application-prod.properties從實際application.properties
這已經很好了(除了在 git 部分暴露秘密......),你實際上已經完成了!
要激活/覆寫它們,您只需要設定spring.profiles.active相應的環境。
這可以通過 14(!) 彈簧靴在第 2 章,外部化配置中定義的屬性源,例如:
- (
java.lang.)系統屬性 (6.):java -jar -Dspring.profiles.active=prod target/myApp.jar - 也方便:(5.)環境變數(我們必須應用“寬松規則”..):
# 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) 是可以的! - 在 中使用或
dev或prod(推薦)屬性application.properties,這會為您添加“默認行為”(其好處(較少關心/維護)和權衡(更多忘記/隱藏))
如何在 PROD 上實作組態檔激活
- 或將
prod屬性用作application.properties(推薦,“選擇加入”DEV 其他環境) - 或以上述建議的方式之一激活它。
如何在 DEV 上實作組態檔激活(使用 IDE)
如果我們選擇 DEV 作為application.properties,那么這也可以完成(甚至可能更喜歡非常“開發驅動”/原型環境)。
如果我們選擇 PROD 作為application.properties,那么我們需要對 DEV-set 執行基本相同的操作spring.profiles.active:
- 將
spring.profiles.active=dev屬性添加到我們的 maven/gradle 構建(配置,在 IDE 中) - 或在我們的 spring-boot:run 上添加
--spring.profiles.active=dev引數(配置,在 IDE 中) - 如果您沒有充分的理由,我不會將 Maven 組態檔與彈簧組態檔混合使用,但如果...
- 在 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>
在測驗中
這取決于:
- 它將在哪個(所有)環境中運行?
- 它應該使用什么組態檔?
我們有非常方便的 彈簧測驗annotation @ActiveProfiles,但它的值應該是相當“固定的”(對于所有運行測驗的環境)。
如果這還不夠,請考慮為測驗引入額外的組態檔(組)/設定。
但是我們也可以將spring.profiles.active屬性傳遞給 (eg) maven-surefire/failesafe-plugin,例如:
<configuration>
<argLine>-Dspring.profiles.active=${some.maven.property}</argLine>
</configuration>
對于秘密
Spring Boot (admits&) 建議:
Spring Boot 不提供對加密屬性值的任何內置支持,但是,它確實提供了修改 Spring 環境中包含的值所需的掛鉤點。該
EnvironmentPostProcessor界面允許您在應用程式啟動之前操作環境。有關詳細資訊,請參閱howto.html。
如果您需要一種安全的方式來存盤憑據和密碼,Spring Cloud Vault專案支持在HashiCorp Vault 中存盤外部化配置。
盡管容器/云驅動,spring-boot 并沒有真正支持:
碼頭工人的秘密
- https://docs.docker.com/engine/swarm/secrets/
...但是圍繞該用例有幾篇文章/so-questions/github-respositories。
更多鏈接:
- https://docs.spring.io/spring-boot/docs/current/reference/html/features.html#features.profiles
- https://docs.spring.io/spring-framework/docs/current/reference/html/core.html#beans-definition-profiles
- https://docs.spring.io/spring-framework/docs/current/reference/html/core.html#beans-property-source-abstraction (ff)
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/408188.html
標籤:
上一篇:AngularService向localhost:4200而不是localhost:8080發出Http請求
下一篇:為什么這個SpringDataJPA的name方法查詢給了我這個錯誤,而Eclipse強迫我回傳一個Optional<User>而不是一個簡單的User物件?
