Skip to content

Commit

Permalink
support nacos config annotation (#3858)
Browse files Browse the repository at this point in the history
Co-authored-by: chickenlj <[email protected]>
  • Loading branch information
shiyiyue1102 and chickenlj authored Nov 26, 2024
1 parent c637544 commit 40a6fc9
Show file tree
Hide file tree
Showing 15 changed files with 1,443 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package com.alibaba.cloud.nacos;

import com.alibaba.cloud.nacos.annotation.NacosAnnotationProcessor;
import com.alibaba.cloud.nacos.refresh.NacosContextRefresher;
import com.alibaba.cloud.nacos.refresh.NacosRefreshHistory;
import com.alibaba.cloud.nacos.refresh.SmartConfigurationPropertiesRebinder;
Expand Down Expand Up @@ -63,8 +64,12 @@ public NacosConfigManager nacosConfigManager(
}

@Bean
public NacosContextRefresher nacosContextRefresher(
NacosConfigManager nacosConfigManager,
public NacosAnnotationProcessor nacosAnnotationProcessor() {
return new NacosAnnotationProcessor();
}

@Bean
public NacosContextRefresher nacosContextRefresher(NacosConfigManager nacosConfigManager,
NacosRefreshHistory nacosRefreshHistory) {
// Consider that it is not necessary to be compatible with the previous
// configuration
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
* Copyright 2013-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.alibaba.cloud.nacos.annotation;

import java.util.Map;

import com.alibaba.nacos.api.config.ConfigChangeEvent;
import com.alibaba.nacos.api.config.ConfigChangeItem;
import com.alibaba.nacos.api.config.listener.AbstractSharedListener;
import com.alibaba.nacos.client.config.impl.ConfigChangeHandler;

public abstract class AbstractConfigChangeListener extends AbstractSharedListener implements TargetRefreshable {

String lastContent;

Object target;

@Override
public Object getTarget() {
return target;
}

@Override
public void setTarget(Object target) {
this.target = target;
}

public AbstractConfigChangeListener(Object target) {
this.target = target;
}

protected void setLastContent(String lastContent) {
this.lastContent = lastContent;
}

@Override
public void innerReceive(String dataId, String group, String configInfo) {

Map<String, ConfigChangeItem> data = null;
try {
data = ConfigChangeHandler.getInstance().parseChangeData(lastContent, configInfo, type(dataId));
}
catch (Exception e) {
throw new RuntimeException(e);
}
ConfigChangeEvent event = new ConfigChangeEvent(data);
receiveConfigChange(event);
lastContent = configInfo;
}

private String type(String dataId) {
if (dataId.endsWith(".yml") || dataId.endsWith(".yaml")) {
return "yaml";
}
return "properties";
}

abstract void receiveConfigChange(ConfigChangeEvent event);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* Copyright 2013-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.alibaba.cloud.nacos.annotation;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonNode;

public class CustomDateDeserializer extends JsonDeserializer<Date> {

private static final long serialVersionUID = 1L;

private SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

public CustomDateDeserializer() {
super();
}

@Override
public Date deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException {
JsonNode node = jsonParser.getCodec().readTree(jsonParser);
String date = node.textValue();
try {
return dateFormat.parse(date);
}
catch (Exception e) {
throw new IOException("Invalid date format");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* Copyright 2013-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.alibaba.cloud.nacos.annotation;

import java.io.IOException;
import java.lang.reflect.Type;

import com.alibaba.nacos.api.exception.runtime.NacosDeserializationException;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.type.TypeFactory;

final class JsonUtils {

private JsonUtils() {
}

static ObjectMapper mapper = new ObjectMapper();

static {
mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
}

/**
* Json string deserialize to Object.
*
* @param json json string
* @param cls class of object
* @param <T> General type
* @return object
* @throws NacosDeserializationException if deserialize failed
*/
public static <T> T toObj(String json, Class<T> cls) {
try {
return mapper.readValue(json, cls);
}
catch (IOException e) {
throw new NacosDeserializationException(cls, e);
}
}

public static <T> T toObj(String json, Type type) {
try {
return mapper.readValue(json, TypeFactory.defaultInstance().constructType(type));
}
catch (IOException e) {
throw new NacosDeserializationException(type, e);
}
}
}
Loading

0 comments on commit 40a6fc9

Please sign in to comment.