THIS IS NOT AN ANSWER, JUST GATHERING DEBUG INFORMATION.
ADD ProfileController.java into your Spring Boot backend project.
package com.example;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.HashMap;
import java.util.Map;
@RestController
public class ProfileController {
private static final Logger logger = LoggerFactory.getLogger(ProfileController.class);
private final Environment env;
@Value("${message}")
private String message;
@Value("${spring.redis.host}")
private String redisHost;
@Value("${spring.redis.port}")
private String redisPort;
@Value("${spring.redis.timeout}")
private String redisTimeout;
public ProfileController(Environment env) {
this.env = env;
}
@GetMapping("/infoJson")
public Map<String, String> getInfoJson() {
String[] profiles = env.getActiveProfiles();
String profile = profiles.length > 0 ? profiles[0] : "default";
logger.info("Current Profile: {} , Message: {}" , profile, message);
Map<String, String> result = new HashMap<>();
result.put("profile", profile);
result.put("message", message);
result.put("spring.redis.host", redisHost);
result.put("spring.redis.port", redisPort);
result.put("spring.redis.timeout", redisTimeout);
logger.info("Result: {}", result);
return result;
}
}
ADD message , message=DOCKER Hello from properties!
spring.application.name=demo-redis-docker
message=DOCKER Hello from properties!
spring.redis.host=${SPRING_DATA_REDIS_HOST}
spring.redis.port=${SPRING_DATA_REDIS_PORT}
spring.redis.timeout=10000ms
*Note:
I changed the configuration property name to use spring.redis.host instead of spring.data.redis.host because I am using Spring Boot 3.x.
rebuild, mvn clean package
rebuild docker image: docker compose build --no-cache app-backend
restart docker compose: docker compose up -d
In the host:
open CMD.exe . run command curl http://localhost:8080/infoJson
Result:
curl http://localhost:8080/infoJson
{"spring.redis.host":"app-redis","profile":"docker","spring.redis.port":"6379","spring.redis.timeout":"10000ms","message":"DOCKER Hello from properties!"}
Use ProfileController.java (http://localhost:8080/infoJson) to display which profile you are currently using and the value of the setting (spring.redis.host).
You should first verify the information: why does your error message show a connection to localhost/127.0.0.1 (connection refused: no further information: localhost/127.0.0.1:6379)?