spring-cloud-bindings icon indicating copy to clipboard operation
spring-cloud-bindings copied to clipboard

spring.redis.* should be spring.data.redis.* in spring boot 3

Open mhoshi-vm opened this issue 3 years ago • 7 comments

spring cloud bindings is failing in spring boot 3 due to pointing against old value

mhoshi-vm avatar Dec 16 '22 08:12 mhoshi-vm

@mhoshi-vm

In the meantime, you can use this:

Import spring cloud bindings in your app compile scope

e.g. gradle:

compileOnly("org.springframework.cloud:spring-cloud-bindings:1.10.0")

Please note this is in repo.spring.io, not maven-central.

Add the Boot3RedisPropertiesProcessor

public class Boot3RedisPropertiesProcessor implements BindingsPropertiesProcessor {

	public static final String TYPE = "redis";

	@Override
	public void process(Environment environment, Bindings bindings, Map<String, Object> properties) {
		if (!shouldApply(environment)) {
			return;
		}

		bindings.filterBindings(TYPE).forEach(binding -> {
			var mapper = new BindingPropertiesMapper(binding.getSecret(), properties);
			mapper.map("client-name", "spring.data.redis.client-name");
			mapper.map("cluster.max-redirects", "spring.data.redis.cluster.max-redirects");
			mapper.map("cluster.nodes", "spring.data.redis.cluster.nodes");
			mapper.map("database", "spring.data.redis.database");
			mapper.map("host", "spring.data.redis.host");
			mapper.map("password", "spring.data.redis.password");
			mapper.map("port", "spring.data.redis.port");
			mapper.map("sentinel.master", "spring.data.redis.sentinel.master");
			mapper.map("sentinel.nodes", "spring.data.redis.sentinel.nodes");
			mapper.map("ssl", "spring.data.redis.ssl");
			mapper.map("url", "spring.data.redis.url");
		});
	}

	static class BindingPropertiesMapper {

		private final Map<String, String> secret;

		private final Map<String, Object> properties;

		private final PropertyMapper mapper;

		public BindingPropertiesMapper(Map<String, String> secret, Map<String, Object> properties) {
			this.secret = secret;
			this.properties = properties;
			this.mapper = PropertyMapper.get().alwaysApplyingWhenNonNull();
		}

		public void map(String from, String to) {
			mapper.from(secret.get(from)).to(value -> this.properties.put(to, value));
		}

	}

	private static boolean shouldApply(Environment environment) {
		return environment.getProperty(String.format("org.springframework.cloud.bindings.boot.%s.enable", TYPE),
				Boolean.class, true);
	}

}

Register the processor

In src/main/resources/META-INF/spring.factories add:

org.springframework.cloud.bindings.boot.BindingsPropertiesProcessor=YOUR.PACKAGE.PATH.Boot3RedisPropertiesProcessor

Kehrlann avatar Dec 21 '22 15:12 Kehrlann

Here's an other workaround:

spring:
  data:
    redis:
      # Workaround for https://github.com/spring-cloud/spring-cloud-bindings/issues/87:
      host: ${spring.redis.host}
      password: ${spring.redis.password}

alexandreroman avatar Jan 12 '23 11:01 alexandreroman

Related: #89

Kehrlann avatar Jan 26 '23 08:01 Kehrlann

Here's an other workaround:

spring:
  data:
    redis:
      # Workaround for https://github.com/spring-cloud/spring-cloud-bindings/issues/87:
      host: ${spring.redis.host}
      password: ${spring.redis.password}

Note: the above workaround will break when gh-94 gets merged ; the custom BindingsPropertiesProcessor will not break.

Kehrlann avatar Mar 02 '23 13:03 Kehrlann

hello, latest work is in https://github.com/spring-cloud/spring-cloud-bindings/pull/94 and should be merged soon; stay tuned! as @Kehrlann mentioned in https://github.com/spring-cloud/spring-cloud-bindings/issues/87#issuecomment-1451861110

anthonydahanne avatar Mar 07 '23 14:03 anthonydahanne

https://github.com/spring-cloud/spring-cloud-bindings/pull/94 is closed, but superseded by #99. Does that mean this issue is resolved in v2.0.1?

mamachanko avatar Jul 28 '23 13:07 mamachanko