forked from mercyblitz/java-training-camp
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
528 additions
and
0 deletions.
There are no files selected for viewing
46 changes: 46 additions & 0 deletions
46
stage-2/src/middleware-projects/distributed-config-project/distributed-config-common/pom.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
<parent> | ||
<groupId>com.acme</groupId> | ||
<artifactId>middleware-projects</artifactId> | ||
<version>0.0.1-SNAPSHOT</version> | ||
<relativePath>../../pom.xml</relativePath> | ||
</parent> | ||
|
||
<artifactId>distributed-config-common</artifactId> | ||
|
||
<properties> | ||
<maven.compiler.source>11</maven.compiler.source> | ||
<maven.compiler.target>11</maven.compiler.target> | ||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||
</properties> | ||
|
||
<dependencies> | ||
|
||
<dependency> | ||
<groupId>com.fasterxml.jackson.core</groupId> | ||
<artifactId>jackson-core</artifactId> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>com.fasterxml.jackson.core</groupId> | ||
<artifactId>jackson-databind</artifactId> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>com.fasterxml.jackson.dataformat</groupId> | ||
<artifactId>jackson-dataformat-xml</artifactId> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>com.fasterxml.jackson.datatype</groupId> | ||
<artifactId>jackson-datatype-jsr310</artifactId> | ||
</dependency> | ||
|
||
|
||
</dependencies> | ||
|
||
</project> |
147 changes: 147 additions & 0 deletions
147
...g-project/distributed-config-common/src/main/java/com/acme/config/common/ConfigEntry.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,147 @@ | ||
package com.acme.config.common; | ||
|
||
import com.fasterxml.jackson.annotation.JsonFormat; | ||
import com.fasterxml.jackson.annotation.JsonIgnore; | ||
|
||
import java.time.LocalDateTime; | ||
import java.util.Collection; | ||
import java.util.Collections; | ||
import java.util.Map; | ||
|
||
/** | ||
* @author <a href="mailto:maimengzzz@gmail.com">韩超</a> | ||
* @since 1.0.0 | ||
*/ | ||
public class ConfigEntry implements Comparable<ConfigEntry> { | ||
|
||
private String configId; | ||
|
||
private String configType; | ||
|
||
private String content; | ||
|
||
@JsonIgnore | ||
private Map<String, String> contentMap; | ||
|
||
private String contentMd5; | ||
|
||
private int order = 100; | ||
|
||
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss") | ||
private LocalDateTime created; | ||
|
||
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss") | ||
private LocalDateTime updated; | ||
|
||
public String getConfigId() { | ||
return configId; | ||
} | ||
|
||
public void setConfigId(String configId) { | ||
this.configId = configId; | ||
} | ||
|
||
public String getConfigType() { | ||
return configType; | ||
} | ||
|
||
public void setConfigType(String configType) { | ||
this.configType = configType; | ||
} | ||
|
||
public String getContent() { | ||
return content; | ||
} | ||
|
||
public void setContent(String content) { | ||
this.content = content; | ||
} | ||
|
||
public String getContentMd5() { | ||
return contentMd5; | ||
} | ||
|
||
public void setContentMd5(String contentMd5) { | ||
this.contentMd5 = contentMd5; | ||
} | ||
|
||
public Map<String, String> getContentMap() { | ||
return Collections.unmodifiableMap(this.contentMap); | ||
} | ||
|
||
public void setContentMap(Map<String, String> contentMap) { | ||
this.contentMap = contentMap; | ||
} | ||
|
||
@JsonIgnore | ||
public String getValue(String key) { | ||
if (this.contentMap == null) | ||
return null; | ||
|
||
return this.contentMap.get(key); | ||
} | ||
|
||
public LocalDateTime getCreated() { | ||
return created; | ||
} | ||
|
||
public void setCreated(LocalDateTime created) { | ||
this.created = created; | ||
} | ||
|
||
public LocalDateTime getUpdated() { | ||
return updated; | ||
} | ||
|
||
public void setUpdated(LocalDateTime updated) { | ||
this.updated = updated; | ||
} | ||
|
||
public int getOrder() { | ||
return order; | ||
} | ||
|
||
public void setOrder(int order) { | ||
this.order = order; | ||
} | ||
|
||
public void applyChanges(Collection<ConfigWatcher.ChangedConfigEntry> changedConfigs) { | ||
if (changedConfigs == null || changedConfigs.isEmpty()) | ||
return; | ||
|
||
for (ConfigWatcher.ChangedConfigEntry configEntry : changedConfigs) { | ||
String key = configEntry.getKey(); | ||
String value = configEntry.getNewValue(); | ||
this.contentMap.put(key, value); | ||
} | ||
} | ||
|
||
@Override | ||
public int compareTo(ConfigEntry o) { | ||
if (o == null) | ||
return 1; | ||
|
||
if (equals(o)) | ||
return 0; | ||
|
||
return Integer.compare(this.order, o.getOrder()); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) return true; | ||
if (o == null || getClass() != o.getClass()) return false; | ||
|
||
ConfigEntry that = (ConfigEntry) o; | ||
|
||
if (!configId.equals(that.configId)) return false; | ||
return configType.equals(that.configType); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
int result = configId.hashCode(); | ||
result = 31 * result + configType.hashCode(); | ||
return result; | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
...ig-project/distributed-config-common/src/main/java/com/acme/config/common/ConfigKind.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package com.acme.config.common; | ||
|
||
/** | ||
* @author <a href="mailto:maimengzzz@gmail.com">韩超</a> | ||
* @since 1.0.0 | ||
*/ | ||
public enum ConfigKind { | ||
JSON("JSON", "application/json"); | ||
|
||
private final String name; | ||
|
||
private final String mediaType; | ||
|
||
ConfigKind(String name, String mediaType) { | ||
this.name = name; | ||
this.mediaType = mediaType; | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public String getMediaType() { | ||
return mediaType; | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
...project/distributed-config-common/src/main/java/com/acme/config/common/ConfigManager.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package com.acme.config.common; | ||
|
||
/** | ||
* @author <a href="mailto:maimengzzz@gmail.com">韩超</a> | ||
* @since 1.0.0 | ||
*/ | ||
public interface ConfigManager { | ||
|
||
void saveConfig(ConfigEntry configEntry); | ||
|
||
ConfigEntry getConfig(String configId); | ||
|
||
void deleteConfig(String configId); | ||
|
||
ConfigService getConfigService(); | ||
|
||
} |
19 changes: 19 additions & 0 deletions
19
...project/distributed-config-common/src/main/java/com/acme/config/common/ConfigService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package com.acme.config.common; | ||
|
||
/** | ||
* @author <a href="mailto:maimengzzz@gmail.com">韩超</a> | ||
* @since 1.0.0 | ||
*/ | ||
public interface ConfigService { | ||
|
||
default String getProperty(String key) { | ||
return this.getProperty(key, null); | ||
} | ||
|
||
String getProperty(String key, String defaultValue); | ||
|
||
boolean registerWatcher(String configId, ConfigWatcher watcher); | ||
|
||
void deregisterWatcher(String configId, ConfigWatcher watcher); | ||
|
||
} |
55 changes: 55 additions & 0 deletions
55
...project/distributed-config-common/src/main/java/com/acme/config/common/ConfigWatcher.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package com.acme.config.common; | ||
|
||
import java.util.Collection; | ||
|
||
/** | ||
* @author <a href="mailto:maimengzzz@gmail.com">韩超</a> | ||
* @since 1.0.0 | ||
*/ | ||
@FunctionalInterface | ||
public interface ConfigWatcher { | ||
|
||
void onChange(String configId, Collection<ChangedConfigEntry> changedConfigs); | ||
|
||
|
||
class ChangedConfigEntry { | ||
private String key; | ||
|
||
private String oldValue; | ||
|
||
private String newValue; | ||
|
||
public ChangedConfigEntry() { | ||
} | ||
|
||
public ChangedConfigEntry(String key, String oldValue, String newValue) { | ||
this.key = key; | ||
this.oldValue = oldValue; | ||
this.newValue = newValue; | ||
} | ||
|
||
public String getKey() { | ||
return key; | ||
} | ||
|
||
public String getOldValue() { | ||
return oldValue; | ||
} | ||
|
||
public String getNewValue() { | ||
return newValue; | ||
} | ||
|
||
public void setKey(String key) { | ||
this.key = key; | ||
} | ||
|
||
public void setOldValue(String oldValue) { | ||
this.oldValue = oldValue; | ||
} | ||
|
||
public void setNewValue(String newValue) { | ||
this.newValue = newValue; | ||
} | ||
} | ||
} |
81 changes: 81 additions & 0 deletions
81
...istributed-config-common/src/main/java/com/acme/config/common/util/FlattenedMapUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package com.acme.config.common.util; | ||
|
||
import java.util.*; | ||
|
||
/** | ||
* @author <a href="mailto:maimengzzz@gmail.com">韩超</a> | ||
* @since 1.0.0 | ||
*/ | ||
public final class FlattenedMapUtils { | ||
|
||
|
||
/** | ||
* 比较两个map版本的差异,以newMap为主 | ||
* @param newMap | ||
* @param oldMap | ||
* @return | ||
*/ | ||
public static Collection<String> getDiffKeys(Map<String, String> newMap, Map<String, String> oldMap) { | ||
Set<String> diffKeys = new HashSet<>(); | ||
Set<String> newKeys = newMap.keySet(); | ||
Set<String> oldKeys = new HashSet<>(); | ||
oldKeys.addAll(oldMap.keySet()); | ||
for (String key : newKeys) { | ||
String oldValue = oldMap.get(key); | ||
String newValue = newMap.get(key); | ||
oldKeys.remove(key); | ||
if (newValue == null && oldValue == null)//同时为null | ||
continue; | ||
|
||
if (newValue.equals(oldValue)) | ||
continue; | ||
|
||
diffKeys.add(key); | ||
|
||
} | ||
diffKeys.addAll(oldKeys); | ||
return diffKeys; | ||
} | ||
|
||
/** | ||
* copy by com.alibaba.cloud:spring-cloud-starter-alibaba-nacos-config:AbstractPropertySourceLoader | ||
* @param result | ||
* @param dataMap | ||
* @param parentKey | ||
*/ | ||
public static void flattenedMap(Map<String, String> result, Map<String, Object> dataMap, | ||
String parentKey) { | ||
if (dataMap == null || dataMap.isEmpty()) { | ||
return; | ||
} | ||
Set<Map.Entry<String, Object>> entries = dataMap.entrySet(); | ||
for (Iterator<Map.Entry<String, Object>> iterator = entries.iterator(); iterator | ||
.hasNext();) { | ||
Map.Entry<String, Object> entry = iterator.next(); | ||
String key = entry.getKey(); | ||
Object value = entry.getValue(); | ||
|
||
String fullKey = (parentKey == null || parentKey.isEmpty()) ? key : key.startsWith("[") | ||
? parentKey.concat(key) : parentKey.concat(".").concat(key); | ||
|
||
if (value instanceof Map) { | ||
Map<String, Object> map = (Map<String, Object>) value; | ||
flattenedMap(result, map, fullKey); | ||
continue; | ||
} | ||
else if (value instanceof Collection) { | ||
int count = 0; | ||
Collection<Object> collection = (Collection<Object>) value; | ||
for (Object object : collection) { | ||
flattenedMap(result, | ||
Collections.singletonMap("[" + (count++) + "]", object), | ||
fullKey); | ||
} | ||
continue; | ||
} | ||
|
||
result.put(fullKey, value.toString()); | ||
} | ||
} | ||
|
||
} |
Oops, something went wrong.