Skip to content

Commit 0f6878a

Browse files
authored
support nacos config annotation (#3857)
1 parent e5b596a commit 0f6878a

15 files changed

+1447
-2
lines changed

spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-nacos-config/src/main/java/com/alibaba/cloud/nacos/NacosConfigAutoConfiguration.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package com.alibaba.cloud.nacos;
1818

19+
import com.alibaba.cloud.nacos.annotation.NacosAnnotationProcessor;
1920
import com.alibaba.cloud.nacos.refresh.NacosContextRefresher;
2021
import com.alibaba.cloud.nacos.refresh.NacosRefreshHistory;
2122
import com.alibaba.cloud.nacos.refresh.SmartConfigurationPropertiesRebinder;
@@ -63,8 +64,12 @@ public NacosConfigManager nacosConfigManager(
6364
}
6465

6566
@Bean
66-
public NacosContextRefresher nacosContextRefresher(
67-
NacosConfigManager nacosConfigManager,
67+
public NacosAnnotationProcessor nacosAnnotationProcessor() {
68+
return new NacosAnnotationProcessor();
69+
}
70+
71+
@Bean
72+
public NacosContextRefresher nacosContextRefresher(NacosConfigManager nacosConfigManager,
6873
NacosRefreshHistory nacosRefreshHistory) {
6974
// Consider that it is not necessary to be compatible with the previous
7075
// configuration
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/*
2+
* Copyright 2013-2023 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.alibaba.cloud.nacos.annotation;
18+
19+
import java.util.Map;
20+
21+
import com.alibaba.nacos.api.config.ConfigChangeEvent;
22+
import com.alibaba.nacos.api.config.ConfigChangeItem;
23+
import com.alibaba.nacos.api.config.listener.AbstractSharedListener;
24+
import com.alibaba.nacos.client.config.impl.ConfigChangeHandler;
25+
26+
public abstract class AbstractConfigChangeListener extends AbstractSharedListener implements TargetRefreshable {
27+
28+
String lastContent;
29+
30+
Object target;
31+
32+
@Override
33+
public Object getTarget() {
34+
return target;
35+
}
36+
37+
@Override
38+
public void setTarget(Object target) {
39+
this.target = target;
40+
}
41+
42+
public AbstractConfigChangeListener(Object target) {
43+
this.target = target;
44+
}
45+
46+
protected void setLastContent(String lastContent) {
47+
this.lastContent = lastContent;
48+
}
49+
50+
@Override
51+
public void innerReceive(String dataId, String group, String configInfo) {
52+
53+
Map<String, ConfigChangeItem> data = null;
54+
try {
55+
data = ConfigChangeHandler.getInstance().parseChangeData(lastContent, configInfo, type(dataId));
56+
}
57+
catch (Exception e) {
58+
throw new RuntimeException(e);
59+
}
60+
ConfigChangeEvent event = new ConfigChangeEvent(data);
61+
receiveConfigChange(event);
62+
lastContent = configInfo;
63+
}
64+
65+
private String type(String dataId) {
66+
if (dataId.endsWith(".yml") || dataId.endsWith(".yaml")) {
67+
return "yaml";
68+
}
69+
return "properties";
70+
}
71+
72+
abstract void receiveConfigChange(ConfigChangeEvent event);
73+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* Copyright 2013-2023 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.alibaba.cloud.nacos.annotation;
18+
19+
import java.io.IOException;
20+
import java.text.SimpleDateFormat;
21+
import java.util.Date;
22+
23+
import com.fasterxml.jackson.core.JsonParser;
24+
import com.fasterxml.jackson.databind.DeserializationContext;
25+
import com.fasterxml.jackson.databind.JsonDeserializer;
26+
import com.fasterxml.jackson.databind.JsonNode;
27+
28+
public class CustomDateDeserializer extends JsonDeserializer<Date> {
29+
30+
private static final long serialVersionUID = 1L;
31+
32+
private SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
33+
34+
public CustomDateDeserializer() {
35+
super();
36+
}
37+
38+
@Override
39+
public Date deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException {
40+
JsonNode node = jsonParser.getCodec().readTree(jsonParser);
41+
String date = node.textValue();
42+
try {
43+
return dateFormat.parse(date);
44+
}
45+
catch (Exception e) {
46+
throw new IOException("Invalid date format");
47+
}
48+
}
49+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
* Copyright 2013-2023 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.alibaba.cloud.nacos.annotation;
18+
19+
import java.io.IOException;
20+
import java.lang.reflect.Type;
21+
22+
import com.alibaba.nacos.api.exception.runtime.NacosDeserializationException;
23+
import com.fasterxml.jackson.annotation.JsonInclude;
24+
import com.fasterxml.jackson.databind.DeserializationFeature;
25+
import com.fasterxml.jackson.databind.ObjectMapper;
26+
import com.fasterxml.jackson.databind.type.TypeFactory;
27+
28+
final class JsonUtils {
29+
30+
private JsonUtils() {
31+
}
32+
33+
static ObjectMapper mapper = new ObjectMapper();
34+
35+
static {
36+
mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
37+
mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
38+
}
39+
40+
/**
41+
* Json string deserialize to Object.
42+
*
43+
* @param json json string
44+
* @param cls class of object
45+
* @param <T> General type
46+
* @return object
47+
* @throws NacosDeserializationException if deserialize failed
48+
*/
49+
public static <T> T toObj(String json, Class<T> cls) {
50+
try {
51+
return mapper.readValue(json, cls);
52+
}
53+
catch (IOException e) {
54+
throw new NacosDeserializationException(cls, e);
55+
}
56+
}
57+
58+
public static <T> T toObj(String json, Type type) {
59+
try {
60+
return mapper.readValue(json, TypeFactory.defaultInstance().constructType(type));
61+
}
62+
catch (IOException e) {
63+
throw new NacosDeserializationException(type, e);
64+
}
65+
}
66+
}

0 commit comments

Comments
 (0)