Skip to content

Commit

Permalink
分布式配置公共
Browse files Browse the repository at this point in the history
  • Loading branch information
Kurok1 committed May 17, 2023
1 parent a6a1c6c commit 4736691
Show file tree
Hide file tree
Showing 13 changed files with 528 additions and 0 deletions.
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>
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;
}
}
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;
}
}
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();

}
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);

}
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;
}
}
}
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());
}
}

}

0 comments on commit 4736691

Please sign in to comment.