首页 文章

为各种环境配置MySql并通过IntelliJ部署到Google App Engine

提问于
浏览
0

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

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

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

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

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

问题:

  • 如何为各种环境(dev(本地)/ qa(gae)/ production(gae))创建不同的配置,并使用相应的环境值部署到这些环境?

  • 从IDE进行构建时,它验证数据库连接字符串(指向 Cloud sql实例)并在无法访问时抛出异常(但如果构建成功,它将来自QA / Prod环境) . 如何解决这个案子?

任何有关这方面的帮助将不胜感激 .

提前致谢 .

1 回答

  • 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=localVM options 下,或者如果您使用"Spring Boot"运行配置,则可以在 Active Profiles 字段中添加本地 .

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

相关问题