I’m using Spring Cloud Config Server 4.2.1 with Redis. I want to set up shared settings for all my client apps. The docs explain how to do this, but it doesn’t seem to work with Redis.
I need @Value("${property}")
to work in client apps without making a hash field for each app. Is there a way to do this?
I tried making a hash field called application
in Redis (like JDBC does), but no luck. I can use spring.cloud.config.server.overrides.property="value"
in the server’s settings file, but I need to change this on the fly.
Any ideas on how to make this work? Here’s a simple example of what I’m trying to do:
@SpringBootApplication
public class ConfigServerApp {
public static void main(String[] args) {
SpringApplication.run(ConfigServerApp.class, args);
}
@Bean
public RedisConnectionFactory redisConnectionFactory() {
return new LettuceConnectionFactory();
}
// How can I add shared properties here?
}
Thanks for any help!
hey FlyingEagle, have u considered using Spring Cloud Bus? it lets u broadcast config changes to all clients in real-time. might solve ur problem without needing custom redis setup. just add spring-cloud-starter-bus-redis dependency and enable it with @EnableBus. worth a shot?
Have you considered implementing a custom ConfigServerBootstrapConfiguration? This approach allows you to inject shared properties directly into the bootstrap context. You could create a Redis-specific implementation that fetches common settings and adds them to the property sources. Here’s a basic outline:
@Configuration
@ConditionalOnProperty(name = "spring.cloud.config.server.redis.enabled", matchIfMissing = true)
public class RedisConfigServerBootstrapConfiguration {
@Bean
public PropertySourceLocator redisPropertySourceLocator(RedisConnectionFactory redisConnectionFactory) {
return environment -> {
// Fetch shared properties from Redis
Map<String, Object> properties = fetchSharedPropertiesFromRedis(redisConnectionFactory);
return new MapPropertySource("redisSharedProperties", properties);
};
}
private Map<String, Object> fetchSharedPropertiesFromRedis(RedisConnectionFactory redisConnectionFactory) {
// Implement Redis fetching logic here
}
}
This solution should allow for dynamic updates and work seamlessly with @Value annotations in your client applications.
hey there! have you tried using a custom PropertySource? you could create one that fetches shared properties from redis and add it to the environment. something like:
@Bean
public PropertySource<?> redisSharedPropertySource() {
// fetch shared props from redis
return new MapPropertySource("redisShared", sharedProps);
}
just an idea - curious if you’ve explored this route? what other approaches have u tried?