- Replies:
- 0
- Words:
- 4270

application.yml, you can manage your database configuration in a clean, profile-based way for both development and production environments.application.yml files for both dev and Kubernetes-based prod environments.
docker run -e "ACCEPT_EULA=Y" -e "SA_PASSWORD=YourStrong!Passw0rd" \ -p 1433:1433 --name mssql-dev -d mcr.microsoft.com/mssql/server:2019-latest
ACCEPT_EULA=Y: Accepts Microsoft’s end-user license agreement.SA_PASSWORD: Sets the admin password (must meet complexity requirements).1433:1433: Exposes the default SQL Server port.pom.xml:
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>mssql-jdbc</artifactId>
<version>12.2.0.jre11</version> <!-- Adjust for your Java version -->
</dependency>
his allows Spring Boot to understand and communicate with MSSQL databases.
application-dev.yml for Local Setupsrc/main/resources folder, create a file named application-dev.yml:
spring:
datasource:
url: jdbc:sqlserver://localhost:1433;databaseName=testdb
username: sa
password: YourStrong!Passw0rd
driver-class-name: com.microsoft.sqlserver.jdbc.SQLServerDriver
jpa:
hibernate:
ddl-auto: update
Replace testdb with your actual database name. If the database doesn’t exist yet, you can create it manually using a SQL client like DBeaver or Azure Data Studio.SPRING_PROFILES_ACTIVE=dev
ConfigMap and Secret resources to manage your configuration.
apiVersion: v1
kind: ConfigMap
metadata:
name: mssql-config
namespace: your-namespace
data:
mssql_host: mssql-service
mssql_port: "1433"
mssql_database: prod_db
apiVersion: v1
kind: Secret
metadata:
name: mssql-secret
namespace: your-namespace
type: Opaque
data:
mssql_user: c2E= # base64 for 'sa'
mssql_password: WW91clN0cm9uZyFQYXNzdzByZA== # base64 for 'YourStrong!Passw0rd'
containers:
- name: your-app
image: your-app-image
env:
- name: MSSQL_HOST
valueFrom:
configMapKeyRef:
name: mssql-config
key: mssql_host
- name: MSSQL_PORT
valueFrom:
configMapKeyRef:
name: mssql-config
key: mssql_port
- name: MSSQL_DATABASE
valueFrom:
configMapKeyRef:
name: mssql-config
key: mssql_database
- name: MSSQL_USER
valueFrom:
secretKeyRef:
name: mssql-secret
key: mssql_user
- name: MSSQL_PASSWORD
valueFrom:
secretKeyRef:
name: mssql-secret
key: mssql_password
- name: SPRING_PROFILES_ACTIVE
value: prod
application-prod.ymlapplication-prod.yml file:
spring:
datasource:
url: jdbc:sqlserver://${MSSQL_HOST}:${MSSQL_PORT};databaseName=${MSSQL_DATABASE}
username: ${MSSQL_USER}
password: ${MSSQL_PASSWORD}
driver-class-name: com.microsoft.sqlserver.jdbc.SQLServerDriver
jpa:
hibernate:
ddl-auto: validate
This configuration uses environment variables to dynamically inject values at runtime, making your app safe and environment-agnostic.
application-dev.yml: for local developmentapplication-prod.yml: for cloud or Kubernetes productionSPRING_PROFILES_ACTIVE.Example:
SPRING_PROFILES_ACTIVE=prod
Or via Kubernetes YAML, as shown earlier.
image quote pre code