nacos map及listmap 配置项bind bug

nacos spring boot 0.2.3 支持了配置list及listmap 属性(configuration),在sample中也带了一个例子,示意yaml文件如下

--- 
list: 
  - "Boston Red Soxe"
  - "Detroit Tigers"
  - "New York Yankees"
listMap: 
    l1: "New York Mets"
    l2: "Chicago Cubs"

但此处有bug,nacos是通过Spring 的RelaxedDataBinder binder = new RelaxedDataBinder(bean, properties.prefix()); 来实现的,RelaxedDataBinder 默认在更新的时候是使用update原有内容来实现,因此当list item数量比原有少,或者map 的key与原有不同,旧数据会保留
在这里插入图片描述
解决办法是修改public class NacosBootConfigurationPropertiesBinder的doBind函数,在
binder.bind(new MutablePropertyValues(prop));
前加入 ObjectUtils.cleanMapOrCollectionField(bean);

另外根据应用需要考虑加入同步执行机制,如果更新不频繁,可以简单使用synchronized。

@Override
	protected void doBind(Object bean, String beanName, String dataId, String groupId,
			NacosConfigurationProperties properties, String content, ConfigService configService) {
		String configType = properties.yaml() ? ConfigType.YAML.getType() : properties.type().getType();
		Properties prop = toProperties(dataId, groupId, content, configType);
		RelaxedDataBinder binder = new RelaxedDataBinder(bean, properties.prefix());

		MutablePropertyValues mut = new MutablePropertyValues(prop);
		synchronized(bean) {
		ObjectUtils.cleanMapOrCollectionField(bean);
		binder.bind(mut);
		}
		publishBoundEvent(bean, beanName, dataId, groupId, properties, content, configService);
		publishMetadataEvent(bean, beanName, dataId, groupId, properties);
	}
Nacos中,可以使用Map类型的配置。你可以将Map对象的键值对作为配置存储在Nacos中,并通过Nacos API或客户端SDK进行读取和更新。 首先,你需要将Map对象转换为JSON格式,并将其作为配置的值存储在Nacos中。你可以使用任何支持JSON序列化的方式,例如使用Gson或Jackson库。 以下是一个示例,演示如何将一个Map对象转换为JSON格式并存储在Nacos中: ```java import com.alibaba.nacos.api.config.ConfigService; import com.alibaba.nacos.api.config.annotation.NacosValue; import com.alibaba.nacos.spring.context.annotation.config.NacosPropertySource; import com.alibaba.nacos.spring.context.annotation.config.NacosValueListener; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import java.util.HashMap; import java.util.Map; @Component @NacosPropertySource(dataId = "example", autoRefreshed = true) public class ExampleConfig { @NacosValue(value = "${example.mapConfig}", autoRefreshed = true) private String mapConfigJson; private Map<String, String> mapConfig; @Autowired private ConfigService configService; // 监听配置变化 @NacosValueListener(dataId = "example", timeout = 5000) public void onMapConfigChanged(Map<String, String> mapConfig) { this.mapConfig = mapConfig; } public Map<String, String> getMapConfig() { return mapConfig; } public void updateMapConfig(Map<String, String> newMapConfig) { // 将Map对象转换为JSON格式 String newMapConfigJson = toJson(newMapConfig); // 更新Nacos中的配置 configService.publishConfig("example", newMapConfigJson); } private String toJson(Map<String, String> map) { // 将Map对象转换为JSON格式 // 使用你喜欢的JSON序列化库进行转换 // 这里使用Gson作为示例 return new Gson().toJson(map); } private Map<String, String> fromJson(String json) { // 将JSON格式转换为Map对象 // 使用你喜欢的JSON序列化库进行转换 // 这里使用Gson作为示例 Type type = new TypeToken<Map<String, String>>(){}.getType(); return new Gson().fromJson(json, type); } public void init() { // 从Nacos中获取配置,将其转换为Map对象 String mapConfigJson = configService.getConfig("example", "DEFAULT_GROUP", 5000); this.mapConfig = fromJson(mapConfigJson); } } ``` 在上面的示例中,`ExampleConfig`类使用了Spring Cloud Alibaba Nacos组件,它简化了与Nacos的交互。你可以在Spring Boot应用程序中使用该组件。 在这个示例中,`ExampleConfig`类注解`@NacosPropertySource`指定了使用Nacos作为配置源,并指定了配置的dataId为"example"。`@NacosValue`注解用于注入Nacos中的配置值,并指定了配置的key为"${example.mapConfig}"。`@NacosValueListener`注解用于监听配置变化,并在配置变化时更新`mapConfig`字段。 你可以通过调用`getMapConfig()`方法来获取当前的Map类型配置值,通过调用`updateMapConfig()`方法来更新Map类型配置值。 请注意,上述示例仅供参考。实际使用时,你需要根据自己的目结构和需求进行相应的修改和适配。 希望这个示例能对你有所帮助!如果有任何问题,请随时提问。
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

weixin_40455124

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值