spring - 如何为许多客户端进行 Spring-boot 配置
问题描述
我想知道如何解决问题:我在 docker 上有一个 spring-boot 应用程序,它连接到 db 和其他一些服务。可能有些客户会在其他 url 上拥有 db。
我使用spring.datasource.url
属性连接到数据库。我应该将它添加到 args 并使用:
Properties properties = new Properties();
properties.put("spring.datasource.url", args[1]);
application.setDefaultProperties(properties);
类似的东西会覆盖它吗?但是每次运行都需要添加数据库 url。还是用别的东西?
解决方案
数据源可以从 docker-compose 文件中作为变量读取:
假设这是您的 docker-compose 文件:
version: '2'
services:
db:
image: customimage_mysql
restart: always
ports:
- "3306:3306"
application:
build: .
ports:
- "9111:9111"
depends_on:
- db
links:
- db
environment:
- database.url=jdbc:mysql://mysql-docker-container:3306/spring_app_db?
现在您有 2 个选项:
- 在 docker compose 中为 databse.url 设置不同的值,并为每个应用程序相应地构建图像
- 在 docker-compose 文件中设置不同的变量(databse1.url,databse2.url,databse3.url,...),并从 application.properties 中引用它们:
application.properties
spring.datasource.url=${database.url}
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.jpa.database-platform=org.hibernate.dialect.MySQLDialect
spring.jpa.hibernate.ddl-auto=update
spring.jpa.generate-ddl=true
spring.jpa.show-sql=true
server.port=9111
推荐阅读
- javascript - 如何使用用户输入作为参数在 JS 函数内部创建条件语句?
- laravel - 在刀片内的@include 内使用变量
- python - 如何在 tkinter 中再次显示透明背景?
- scala - Scala:逐行读取一个巨大的压缩文本文件而不加载到内存中
- python - XML ElementTree:两个同名标签
- python - 'NoneType' 对象在使用 pycountry 库时没有属性 'name'
- optaplanner - 在 Quarkus Optaplanner 中调整优化算法
- kubernetes - 将 openshift 操作符移植到 Kubernetes 集群
- java - JavaFX 项目 - 某些导入无法访问
- ubuntu - Jupyter Lab 未在 Ubuntu 上打开