From 3bbdd9a3d3b1165877162f652d73b4c19b92b851 Mon Sep 17 00:00:00 2001 From: Araf Karsh Hamid Date: Thu, 23 Jan 2025 22:02:25 +0530 Subject: [PATCH] Optimized the properties to work for PostgreSQL inside Docker & Kubernetes --- backup/props/application.properties.215 | 264 ++++++++++++++++++ build | 1 - buildx | 3 +- compile | 2 + config/application.properties | 4 +- .../Input/application-prod.properties-r | 2 +- src/docker/Input/application.properties | 4 +- .../Package/dev/application.properties | 4 +- .../Package/prod/application.properties | 4 +- .../Package/staging/application.properties | 4 +- .../server/controllers/ProfileController.java | 27 +- .../server/service/ServiceEventListener.java | 40 +-- src/main/resources/app.props.build | 4 +- 13 files changed, 292 insertions(+), 71 deletions(-) create mode 100644 backup/props/application.properties.215 diff --git a/backup/props/application.properties.215 b/backup/props/application.properties.215 new file mode 100644 index 0000000..54ab27a --- /dev/null +++ b/backup/props/application.properties.215 @@ -0,0 +1,264 @@ +# ======================================================================= +# MS-Vanilla Service Properties +# ======================================================================= +build.number=216 +build.date=Thu Jan 23 21:59:30 IST 2025 +# ------------------------------------------------------------------------ +# Spring Profile +# ------------------------------------------------------------------------ +spring.profiles.default=dev +# ======================================================================= +# Service Name & API Details - Version, Path +# ======================================================================= +service.org=arafkarsh +service.name=MS-Vanilla +service.api.name=ms-vanilla +service.api.prefix=api +service.api.version=v1 +service.api.error.prefix=13 +service.container=ms-vanilla-service +service.api.repository=https://github.com/arafkarsh/ms-springboot-334-vanilla +service.api.path=/${service.api.name}/${service.api.prefix}/${service.api.version} +service.url=http://www.arafkarsh.com/ +service.license=Apache 2 License +service.license.url=https://github.com/arafkarsh/ms-springboot-334-vanilla?tab=Apache-2.0-1-ov-file +# ======================================================================= +# Microservice Server Properties +# ======================================================================= +server.port=9334 +server.version=0.3.4 +server.restart=false +server.leak.test=3 +server.resources.url=${service.url}${service.api.path} +server.api.url.print=true +# ------------------------------------------------------------------------ +# Host SSL Details : +# Rename the file fusionAir.pkcs12 in resources folder and uncomment the +# following lines to enable SSL +# ------------------------------------------------------------------------ +#server.ssl.key-store=classpath:fusionAir.pkcs12 +#server.ssl.key-store-type=PKCS12 +#server.ssl.key-store-password=Fusion.2023 +#server.ssl.key-alias=fusionAir +# ------------------------------------------------------------------------ +# Security & JWT Token (Type 1 = secret key, 2 = public / private key) +# ------------------------------------------------------------------------ +server.crypto.public.key=publicKey.pem +server.crypto.private.key=privateKey.pem +server.token.issuer=${service.org} +# Type 1 = Secret, Type 2 = Public / Private Key +server.token.type=1 +server.token.test=true +# Token Expiry Times - Security Policy will reject High Expiry Time +server.token.auth.expiry=600000 +server.token.refresh.expiry=3600000 +server.token.key=<([1234567890SecretKey!!To??Encrypt##Data@12345%6790])> +server.secure.data.key=<([1234567890SecretKEY!!TO??Encrypt##DATA@12345%6790])> +# ------------------------------------------------------------------------ +# Keycloak Details for OAuth 2 Authentication +# ------------------------------------------------------------------------ +server.keycloak.url=http://localhost:8080/realms/Fusion-Air/protocol/openid-connect/token +server.keycloak.certs=http://localhost:8080/realms/Fusion-Air/protocol/openid-connect/certs +server.keycloak.clientId=fusion-air-user-service +server.keycloak.secret=2AGe7XxP8evCmhdjD3cmgpE23y0g6PGU +server.keycloak.grantType=password +server.keycloak.publicKey=publicKey.KeyCloak.pem +server.keycloak.issuer=http://localhost:8080/realms/Fusion-Air +server.keycloak.enabled=false +# ------------------------------------------------------------------------ +# Host Details +# ------------------------------------------------------------------------ +server.host=localhost +server.host.dev=http://localhost:${server.port} +server.host.dev.desc=Development Server +server.host.uat=https://uat.${service.org}.com +server.host.uat.desc=UAT Server +server.host.prod=https://prod.${service.org}.com +server.host.prod.desc=Production Server +server.error.whitelabel.enabled=true +server.vulnerability.demos.allowed=false +# Service Properties Details +# ------------------------------------------------------------------------ +spring.codec.max-in-memory-size=3MB +app.property.product=fusion.air.product +app.property.product.list=/create, /status, /all/secured, /search/product, /search/price, /search/active, /deactivate, /activate, /update, /update/price, /update/details, /delete +app.property.list=element1, element2, element3 +app.property.map={key1:'val1', key2 : 'val2', key3 : 'val3'} +# ------------------------------------------------------------------------ +# Logging / Events: LogBack Setup / File Roll Over, +# ------------------------------------------------------------------------ +# Log Groups +logging.group.tomcat=org.apache.catalina, org.apache.coyote, org.apache.tomcat +# Logging Levels for various components +logging.level.org.springframework.boot.web.servlet=INFO +logging.level.org.springframework.web.servlet.DispatcherServlet=INFO +logging.level.org.springframework.security.web.FilterChainProxy=INFO +# Spring Default Groups +logging.level.web=INFO +logging.level.sql=INFO +logging.level.tomcat=INFO +# Root level logging +logging.level.root=INFO +# Specify the logback configuration file +logging.config=classpath:logback-spring.xml +# Supported Log Format plain / json +logging.format=plain +# Log file paths and rolling policy +logging.path=/tmp/logs/${service.api.name} +# Log File Name. File Extension .log or .json is added dynamically based on logging format. +logging.file.name=${service.api.name}.v${server.version} +# Log File Pattern for Size and Time based File Roll Over Name +logging.pattern.rolling-file-name=${logging.path}/${logging.file.name}-%d{yyyy-MM-dd}-%i +# Max Single File Size = 30 MB +logging.file.max-size=10MB +# Retention and total size cap for logs +# Keep Log Files for 100 Days with Max Cap at 3 GB +logging.file.max-history=100 +logging.file.total-size-cap=3GB +# ------------------------------------------------------------------------ +# Metrics: Micrometer / Prometheus / Actuator / +# ------------------------------------------------------------------------ +# Enable exposure of specific actuator endpoints (health, metrics, prometheus) +springdoc.show-actuator=true +management.endpoints.web.exposure.include=health,metrics,prometheus,info +# Enable Spring Boot Actuator Endpoints +management.endpoint.health.enabled=true +management.endpoint.metrics.enabled=true +management.endpoint.prometheus.enabled=true +# Enable Metrics +management.metrics.export.prometheus.enabled=true +management.metrics.enable.jvm=false +management.metrics.enable.jvm.gc=false +management.metrics.enable.datasource=false +# ------------------------------------------------------------------------ +# Tracing: Open Telemetry / Micrometer +# Micrometer only supports HTTP/Protobuf (http) for OTLP. grpc is not supported. +# ------------------------------------------------------------------------ +# OpenTelemetry Setup +# W3C Standard, B3 Zipkin Standard +# ------------------------------------------------------------------------ +otel.propagators=tracecontext +otel.resource.attributes.deployment.environment=dev +otel.resource.attributes.service.name=ms-vanilla +otel.resource.attributes.service.namespace=shopping +# ------------------------------------------------------------------------ +# Export to Collector +# ------------------------------------------------------------------------ +# Enable OpenTelemetry +otel.javaagent.debug=true +otel.service.name=${service.container} +otel.exporter.otlp.endpoint=http://localhost:4318 +# to load the Custom OpenTelemetric Config +otel.traces.exporter=none +otel.metrics.exporter=none +otel.logs.exporter=none +# ------------------------------------------------------------------------ +# Disable global auto-instrumentation and explicitly enable for jdbc, logback, web, kafka, etc +# ------------------------------------------------------------------------ +otel.instrumentation.common.default-enabled=false +# Enable specific instrumentation (JDBC is not supported in this version of Open Telemetry) +otel.instrumentation.jdbc.enabled=false +otel.instrumentation.logback-mdc.enabled=true +otel.instrumentation.spring-web.enabled=true +otel.instrumentation.spring-webmvc.enabled=true +otel.instrumentation.micrometer.enabled=true +# External Systems Kafka / MongoDB +otel.instrumentation.kafka.enabled=false +otel.instrumentation.mongo.enabled=false +# ------------------------------------------------------------------------ +# Async - Reactive Framework +# ------------------------------------------------------------------------ +#otel.instrumentation.spring-webflux.enabled=false +#otel.instrumentation.r2dbc.enabled=false +# ------------------------------------------------------------------------ +#management.tracing.enabled=false +# OpenTelemetry Tracing Configuration +#management.otlp.metrics.export.enabled=false +#management.otlp.metrics.export.url=http://localhost:4358/v1/metrics +# number of Meter to include in a single payload sent to the backend. The default is 10,000. +#management.otlp.metrics.export.batchSize=15000 +#management.otlp.metrics.export.timeout=5s +# Determines how the additive quantities are expressed, in relation to time. Values are cumulative or delta +#management.otlp.metrics.export.aggregationTemporality=cumulative +# Additional headers to send with exported metrics +#management.otlp.metrics.export.headers.Authorization=Bearer abc123 +# the interval at which metrics will be published. +#management.otlp.metrics.export.step=10s +# Resource Attributes are used for all metrics published. +#management.otlp.metrics.export.resourceAttributes.service.name=ms-vanilla-service +# ======================================================================= +# Text Encryption +# ======================================================================= +# Ensure this matches your environment variable +jasypt.encryptor.password=${JASYPT_ENCRYPTOR_PASSWORD} +#jasypt.encryptor.password=${BASE64:JASYPT_ENCRYPTOR_PASSWORD} +# Match the algorithm +jasypt.encryptor.algorithm=PBEWithHmacSHA512AndAES_256 +jasypt.encryptor.iv-generator-classname=org.jasypt.iv.RandomIvGenerator +jasypt.encryptor.salt-generator-classname=org.jasypt.salt.RandomSaltGenerator +# ======================================================================= +# Database Properties +# ======================================================================= +db.server=mem +db.port=5432 +db.name=ms_cache +db.schema=ms_schema +db.vendor=H2 +# To Store the Data in File +#spring.datasource.url=jdbc:h2:file:/data/demo +spring.datasource.url=jdbc:h2:${db.server}:${db.name};DB_CLOSE_ON_EXIT=FALSE +spring.datasource.driverClassName=org.h2.Driver +spring.datasource.username=sa +spring.datasource.password=ENC(lA6JCEpK7+wuHDpB1A41DOUfn6L74DQxaazXLTjyQHY5/X6CONfUEyDt6erWifrN) +spring.jpa.database-platform=org.hibernate.dialect.H2Dialect +spring.datasource.hikari.connection-test-query=SELECT 1 +# ======================================================================= +# JPA / Hibernate Properties +# ------------------------------------------------------------------------ +spring.jpa.show-sql=true +spring.jpa.defer-datasource-initialization=true +#spring.sql.init.data-locations=data-trans.sql +# Hibernate ddl auto (create, create-drop, validate, update) +spring.jpa.hibernate.ddl-auto=create-drop +spring.jpa.properties.hibernate.format_sql=true +spring.jpa.properties.hibernate.validator.apply_to_ddl=false +#spring.jpa.properties.hibernate.check_nullability=true +# ======================================================================= +# External Remote Server Properties +# ======================================================================= +remote.host=127.0.0.1 +remote.port=8080 +remote.protocol=http +# ======================================================================= +# Open API Properties +# For More Info: https://springdoc.org/#Introduction +# springdoc.api-docs.path=/api-docs +# ======================================================================= +springdoc.api-docs.path=${service.api.path} +springdoc.swagger-ui.path=${service.api.path}/swagger-ui.html +springdoc.swagger-ui.tryItOutEnabled=true +springdoc.swagger-ui.filter=true +springdoc.swagger-ui.use-root-path=true +springdoc.swagger-ui.disable-swagger-default-url=true +server.forward-headers-strategy=framework +springdoc.cache.disabled=true +springdoc.writer-with-default-pretty-printer=true +#springdoc.swagger-ui.configUrl=${service.api.path}/swagger-config/swagger-config.json +# Disabling the api-docs endpoint +springdoc.api-docs.enabled=true +# Disabling the swagger-ui +#springdoc.swagger-ui.enabled=true +springdoc.swagger-ui.operationsSorter=method +# For sorting tags alphabetically +springdoc.swagger-ui.tagsSorter=alpha +#springdoc.show-actuator=true +# Packages to include +# springdoc.packagesToScan=io.fusion.water, io.fusion.fire +# Paths to include +# springdoc.pathsToMatch=/v1, /api/health/** +# To expose the swagger-ui, on the management port +#springdoc.use-management-port=true +# This property enables the openapi and swaggerui endpoints to be exposed +# beneath the actuator base path. +# management.endpoints.web.exposure.include=openapi, swaggerui +# ======================================================================= \ No newline at end of file diff --git a/build b/build index 9ff6fdb..a833df4 100755 --- a/build +++ b/build @@ -7,7 +7,6 @@ # Get the Org Name, Service Name, Version and Full Container Name source serviceName -source updateProperties echo "---------------------------------------------------------------------------------------" echo "Building the Container for $ORG/$SERVICE Service ..... " diff --git a/buildx b/buildx index 4bb2305..2b5b4e8 100755 --- a/buildx +++ b/buildx @@ -7,12 +7,11 @@ # Get the Org Name, Service Name, Version and Full Container Name source serviceName -source updateProperties echo "---------------------------------------------------------------------------------------" echo "Building the Container (arm64 / amd64) for $ORG/$SERVICE Service ..... " echo "---------------------------------------------------------------------------------------" cd src/docker -buildx +buildx $1 $2 cd ../../ echo "---------------------------------------------------------------------------------------" diff --git a/compile b/compile index 31d4da8..f6a64e3 100755 --- a/compile +++ b/compile @@ -55,6 +55,8 @@ then convert4ConfigMap config/application-staging.properties src/kubernetes/Package/staging/application-staging.properties convert4ConfigMap config/application-prod.properties src/kubernetes/Package/prod/application-prod.properties + source updateProperties + echo "2.5 Copy logback-spring xml Files to config directory." cp src/main/resources/logback-spring*xml config diff --git a/config/application.properties b/config/application.properties index 9cbcf76..fa4e50f 100644 --- a/config/application.properties +++ b/config/application.properties @@ -1,8 +1,8 @@ # ======================================================================= # MS-Vanilla Service Properties # ======================================================================= -build.number=215 -build.date=Thu Jan 23 21:34:48 IST 2025 +build.number=216 +build.date=Thu Jan 23 22:01:49 IST 2025 # ------------------------------------------------------------------------ # Spring Profile # ------------------------------------------------------------------------ diff --git a/src/docker/Input/application-prod.properties-r b/src/docker/Input/application-prod.properties-r index 9a76659..aa20652 100644 --- a/src/docker/Input/application-prod.properties-r +++ b/src/docker/Input/application-prod.properties-r @@ -10,7 +10,7 @@ logging.level.root=INFO # PostgreSQL DB Properties - # ======================================================================= spring.config.activate.on-profile=prod -db.server=host.docker.internal +db.server=localhost db.port=5433 db.name=ms_vanilla_334 db.schema=ms_schema diff --git a/src/docker/Input/application.properties b/src/docker/Input/application.properties index 9cbcf76..fa4e50f 100644 --- a/src/docker/Input/application.properties +++ b/src/docker/Input/application.properties @@ -1,8 +1,8 @@ # ======================================================================= # MS-Vanilla Service Properties # ======================================================================= -build.number=215 -build.date=Thu Jan 23 21:34:48 IST 2025 +build.number=216 +build.date=Thu Jan 23 22:01:49 IST 2025 # ------------------------------------------------------------------------ # Spring Profile # ------------------------------------------------------------------------ diff --git a/src/kubernetes/Package/dev/application.properties b/src/kubernetes/Package/dev/application.properties index 2ad54d1..59235e0 100644 --- a/src/kubernetes/Package/dev/application.properties +++ b/src/kubernetes/Package/dev/application.properties @@ -1,5 +1,5 @@ -build.number=215 -build.date=Thu Jan 23 21:34:48 IST 2025 +build.number=216 +build.date=Thu Jan 23 22:01:49 IST 2025 spring.profiles.default=dev service.org=arafkarsh service.name=MS-Vanilla diff --git a/src/kubernetes/Package/prod/application.properties b/src/kubernetes/Package/prod/application.properties index 2ad54d1..59235e0 100644 --- a/src/kubernetes/Package/prod/application.properties +++ b/src/kubernetes/Package/prod/application.properties @@ -1,5 +1,5 @@ -build.number=215 -build.date=Thu Jan 23 21:34:48 IST 2025 +build.number=216 +build.date=Thu Jan 23 22:01:49 IST 2025 spring.profiles.default=dev service.org=arafkarsh service.name=MS-Vanilla diff --git a/src/kubernetes/Package/staging/application.properties b/src/kubernetes/Package/staging/application.properties index 2ad54d1..59235e0 100644 --- a/src/kubernetes/Package/staging/application.properties +++ b/src/kubernetes/Package/staging/application.properties @@ -1,5 +1,5 @@ -build.number=215 -build.date=Thu Jan 23 21:34:48 IST 2025 +build.number=216 +build.date=Thu Jan 23 22:01:49 IST 2025 spring.profiles.default=dev service.org=arafkarsh service.name=MS-Vanilla diff --git a/src/main/java/io/fusion/air/microservice/server/controllers/ProfileController.java b/src/main/java/io/fusion/air/microservice/server/controllers/ProfileController.java index 0e13ac8..496ff44 100644 --- a/src/main/java/io/fusion/air/microservice/server/controllers/ProfileController.java +++ b/src/main/java/io/fusion/air/microservice/server/controllers/ProfileController.java @@ -19,6 +19,7 @@ import io.fusion.air.microservice.server.config.DatabaseConfig; import io.fusion.air.microservice.server.config.ServiceConfig; // Swagger +import io.fusion.air.microservice.server.service.ProfileService; import io.fusion.air.microservice.utils.Utils; import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.media.Content; @@ -62,6 +63,7 @@ public class ProfileController extends AbstractController { private final DatabaseConfig dbConfig; // Autowired using the Constructor private ConfigurableEnvironment environment; + private ProfileService profileService; private final String serviceName; @@ -71,10 +73,11 @@ public class ProfileController extends AbstractController { * @param dbConfig */ public ProfileController(ServiceConfig serviceConfig, DatabaseConfig dbConfig, - ConfigurableEnvironment environment) { + ConfigurableEnvironment environment, ProfileService profileService) { this.serviceConfig = serviceConfig; this.dbConfig = dbConfig; this.environment = environment; + this.profileService = profileService; this.serviceName = serviceConfig.getServiceName(); } @@ -100,7 +103,7 @@ public ResponseEntity retrieveProfile() throws SecurityExcepti Map data = new LinkedHashMap<>(); StandardResponse stdResponse = createSuccessResponse("Profile Data Retrieved!"); data.put("Service", serviceName); - data.put("Active Profile", getActiveProfile()); + data.put("Active Profile", profileService.getActiveProfile()); data.put("Service Version", serviceConfig.getServerVersion()); data.put("Build Date", serviceConfig.getBuildDate()); data.put("Build No", serviceConfig.getBuildNumber()); @@ -133,7 +136,7 @@ public ResponseEntity retrieveDBProfile() throws SecurityExcep Map data = new LinkedHashMap<>(); StandardResponse stdResponse = createSuccessResponse("Profile Data Retrieved!"); data.put("Service", serviceName); - data.put("Active Profile", getActiveProfile()); + data.put("Active Profile", profileService.getActiveProfile()); data.put("Service Version", serviceConfig.getServerVersion()); data.put("Build Date", serviceConfig.getBuildDate()); data.put("Build No", serviceConfig.getBuildNumber()); @@ -145,23 +148,5 @@ public ResponseEntity retrieveDBProfile() throws SecurityExcep stdResponse.setPayload(data); return ResponseEntity.ok(stdResponse); } - - /** - * Get Active Profile - * @return - */ - private String getActiveProfile() { - if (environment.getActiveProfiles().length == 0) { - log.info("Spring Profile is missing, so defaulting to {} Profile!", serviceConfig.getActiveProfile()); - environment.addActiveProfile(serviceConfig.getActiveProfile()); - } - StringBuilder sb = new StringBuilder(); - for(String profile : environment.getActiveProfiles()) { - sb.append(profile).append(" "); - } - String profile = sb.toString().trim().replace(" ", ", "); - log.info("Spring Active Profiles = {} ", profile); - return profile; - } } diff --git a/src/main/java/io/fusion/air/microservice/server/service/ServiceEventListener.java b/src/main/java/io/fusion/air/microservice/server/service/ServiceEventListener.java index 7b21ccf..8869335 100644 --- a/src/main/java/io/fusion/air/microservice/server/service/ServiceEventListener.java +++ b/src/main/java/io/fusion/air/microservice/server/service/ServiceEventListener.java @@ -63,7 +63,7 @@ public class ServiceEventListener { private MeterRegistry meterRegistry; // Autowired using the Constructor - private ConfigurableEnvironment environment; + private ProfileService profileService; /** * Autowired using the Constructor @@ -71,42 +71,14 @@ public class ServiceEventListener { * @param serviceConfig * @param tokenManager * @param meterRegistry - * @param environment + * @param profileService */ public ServiceEventListener(ServiceConfig serviceConfig, TokenManager tokenManager, - MeterRegistry meterRegistry, ConfigurableEnvironment environment) { + MeterRegistry meterRegistry, ProfileService profileService) { this.serviceConfig = serviceConfig; this.tokenManager = tokenManager; - this.meterRegistry = meterRegistry; - this.environment = environment; - } - - /** - * Check the Dev Mode - * @return - */ - private boolean getDevMode() { - String activeProfile = getActiveProfile(); - return (activeProfile != null && activeProfile.equalsIgnoreCase("prod")) ? false : true; - } - - /** - * Get Active Profile - * @return - */ - private String getActiveProfile() { - if (environment.getActiveProfiles().length == 0) { - log.info("Spring Profile is missing, so defaulting to {} Profile!", serviceConfig.getActiveProfile()); - environment.addActiveProfile(serviceConfig.getActiveProfile()); - } - StringBuilder sb = new StringBuilder(); - for(String profile : environment.getActiveProfiles()) { - sb.append(profile).append(" "); - } - String profile = sb.toString().trim().replace(" ", ", "); - log.info("Spring Active Profiles = {} ", profile); - return profile; + this.profileService = profileService; } /** @@ -139,7 +111,7 @@ public void setupLogoMetricsTokens() { // 2: Register the APIs with Micrometer registerAPICallsForMicroMeter(); // 3: Generate Tokens - ONLY For Dev Mode (For Developer Testing) - if(tokenManager.getJsonWebTokenConfig().isServerTokenTest() && getDevMode() ) { + if(tokenManager.getJsonWebTokenConfig().isServerTokenTest() && profileService.getDevMode() ) { generateTestToken(); } } @@ -235,7 +207,7 @@ public void showLogo() { } } private String geDeploymentMode() { - return switch (getActiveProfile()) { + return switch (profileService.getActiveProfile()) { case "staging" -> "Staging"; case "prod" -> "Production"; case "dev" -> "Development"; diff --git a/src/main/resources/app.props.build b/src/main/resources/app.props.build index 3c64248..bfdb70b 100644 --- a/src/main/resources/app.props.build +++ b/src/main/resources/app.props.build @@ -1,5 +1,5 @@ # ======================================================================= # MS-Vanilla Service Properties # ======================================================================= -build.number=215 -build.date=Thu Jan 23 21:34:48 IST 2025 +build.number=216 +build.date=Thu Jan 23 22:01:49 IST 2025