为各种环境配置MySql并通过IntelliJ

时间:2017-08-14 13:53:22

标签: google-app-engine spring-boot intellij-idea google-cloud-sql google-cloud-tools

我正在使用IntelliJ IDE与Maven一起开发Spring Boot服务,并使用Google Cloud Tools插件部署到App Engine Flexible。我使用以下(连接到本地)并运行应用程序。在本地,它工作正常(在application.properties中)。

  

spring.datasource.url = JDBC:MySQL的://本地主机:3309 /测试

但是,当我尝试使用以下内容(在application.properties中)部署到GAE时,

  ?

spring.datasource.url = JDBC:MySQL的://谷歌/测试cloudSqlInstance = [云-SQL实例]安培;的SocketFactory = com.google.cloud.sql.mysql.SocketFactory

在上传到GAE之前尝试构建项目时,会抛出UnknownHostException:“google”。

问题:

  1. 如何为各种环境(dev(本地)/ qa(gae)/ production(gae))创建不同的配置并部署到具有相应环境值的环境?

  2. 从IDE进行构建时,它验证数据库连接字符串(指向云sql实例)并在无法访问的情况下抛出异常(但是如果它不在QA / Prod环境中则会构建成功)。如何解决这个案子?

  3. 对此的任何帮助将不胜感激。

    提前致谢。

1 个答案:

答案 0 :(得分:2)

您需要使用Spring Profiles。请阅读文档中的所有信息以获得详尽的解释。

简言之:

  

Spring Profiles提供了一种隔离应用程序部分的方法   配置并使其仅在某些环境中可用

现在,解决手头的问题。它可以通过为您的开发引入“本地”配置文件并保留要在生产中使用的“默认”配置文件(GAE)来解决。

<强> application.properties

# this file is for the "default" profile that will be used when 
# no spring.profiles.active is defined. So consider this production config.

spring.datasource.url=jdbc:mysql://google/test?cloudSqlInstance=[cloud-sql-instance]&socketFactory=com.google.cloud.sql.mysql.SocketFactory

application-local.properties

# this file is for the "local" profile that will be used when 
# -Dspring.profiles.active=local is specified when running the application. 
# So consider this "local" development config

spring.datasource.url=jdbc:mysql://localhost:3309/test

# In this file you can also override any other property defined in application.properties, or add additional ones

现在运行应用程序,同时开发所有必须在运行配置中的IntelliJ中指定的-Dspring.profiles.active=local VM options下的Active Profiles,或者如果您使用的是“Spring Boot”运行配置,则可以只需在update字段中添加 local 即可。

在GAE上,根本不指定任何配置文件,将使用默认值。