nacos与springboot结合使用

一 基础类型的值的获取与自动更新

    @NacosValue(value = "${festival.order.orderExpired}", autoRefreshed = true)
    private String orderExpired;

二 nacos中复杂类型的值的获取与自动更新

1 配置信息

activity:
  activityRemark: 
    - "第一滴血"
    - "2.第一滴血。"
    - "3.第一滴血啊同学"
  mobileRemark:
    - 短信收取常见问题:
    - 1.短信收发需要30s左右时间,请耐心等待。
    - 2.如您一直未收到短信请联系客服处理。

2 不正确的使用方法 如果这样使用,是肯定不行的。

 @NacosValue(value = "${activity.activityRemark}", autoRefreshed = true)
 private List<String> activityRemark;

正常情况下,nacos中的值为基础类型,当我们需要把列表,数组或者其它的object的类型的数据,配置到nacos中的时候,是无法直接通过autorefresh=true来获更新后的值的。

3.正确的使用方法

**添加配置类**  ActivityRemarkProperties.java

import com.alibaba.nacos.api.config.ConfigType;
import com.alibaba.nacos.api.config.annotation.NacosConfigurationProperties;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;

import java.util.List;

@Data
@Configuration
@ConfigurationProperties(
        prefix = "activity")
public class ActivityRemarkProperties {
    private List<String> activityRemark;
    private List<String> mobileRemark;
}


**添加监听器** ActivityRemarkConfig.java
import com.alibaba.nacos.api.config.ConfigType;
import com.alibaba.nacos.api.config.annotation.NacosConfigListener;
import com.alibaba.nacos.spring.util.NacosUtils;
import com.google.common.base.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.bind.Bindable;
import org.springframework.boot.context.properties.bind.Binder;
import org.springframework.boot.context.properties.source.ConfigurationPropertySource;
import org.springframework.boot.context.properties.source.MapConfigurationPropertySource;
import org.springframework.context.annotation.Configuration;


import java.util.Collections;
import java.util.List;
import java.util.Map;

@Configuration
public class ActivityRemarkConfig {
    // 配置注入
    @Autowired
    private ActivityRemarkProperties activityRemarkProperties;
    
	//添加对 nacos的事件监听
    @NacosConfigListener(
            groupId = "DEFAULT_GROUP",
            dataId = "activity_remark.yml",
            type = ConfigType.YAML
    )
    public void onMessage(String msg) {
		//处理监听到的新内容
        ActivityRemarkProperties newProperties = getNewProperties(msg);
		//更新新的内容到容器中相对应的字段
        updateProperties(newProperties);
    }

    private ActivityRemarkProperties getNewProperties(String msg) {
        if (Strings.isNullOrEmpty(msg)) {
            return null;
        }

        Map<String,Object> properties= NacosUtils.toProperties(msg,"yaml");
        List<ConfigurationPropertySource> propertySourceList=
                Collections.singletonList(new MapConfigurationPropertySource(properties));

        return new Binder(propertySourceList).bindOrCreate("activity", Bindable.of(ActivityRemarkProperties.class));

    }

    private void updateProperties(ActivityRemarkProperties newProperties) {
        //activityRemarkProperties = newProperties;
        activityRemarkProperties.setActivityRemark(newProperties.getActivityRemark());
        activityRemarkProperties.setMobileRemark(newProperties.getMobileRemark());
    }
}


4 代码中使用

    ** 在需要的地方,直接进行一般容器的属性注入即可 **

    @Autowired
    private ActivityRemarkProperties activityRemarkProperties;