Skip to content

Commit

Permalink
feat(metal): Support listen key change event.
Browse files Browse the repository at this point in the history
  • Loading branch information
yizzuide committed May 26, 2020
1 parent 3f49e1b commit 3206f72
Show file tree
Hide file tree
Showing 8 changed files with 102 additions and 10 deletions.
4 changes: 2 additions & 2 deletions Milkomeda/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

<properties>
<java.version>1.8</java.version>
<project.release.version>3.6.1-SNAPSHOT</project.release.version>
<project.release.version>3.6.2-SNAPSHOT</project.release.version>
<spring-boot.version>2.2.4</spring-boot.version>
<spring-cloud.version>Hoxton.RELEASE</spring-cloud.version>
<mybatis.starter.version>2.1.1</mybatis.starter.version>
Expand Down Expand Up @@ -67,7 +67,7 @@
<profile>
<id>sonatype-oss-release</id>
<properties>
<project.release.version>3.6.1</project.release.version>
<project.release.version>3.6.2</project.release.version>
</properties>
<build>
<plugins>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package com.github.yizzuide.milkomeda.metal;

import org.springframework.context.ApplicationEvent;

/**
* MetalChangeEvent
*
* @author yizzuide
* @since 3.6.2
* Create at 2020/05/26 15:29
*/
public class MetalChangeEvent extends ApplicationEvent {
private static final long serialVersionUID = -8409365025025128964L;
private String key;
private String oldVal;
private String newVal;

public MetalChangeEvent(Object source, String key, String oldVal, String newVal) {
super(source);
this.key = key;
this.oldVal = oldVal;
this.newVal = newVal;
}

public String getKey() {
return key;
}

public void setKey(String key) {
this.key = key;
}

public String getOldVal() {
return oldVal;
}

public void setOldVal(String oldVal) {
this.oldVal = oldVal;
}

public String getNewVal() {
return newVal;
}

public void setNewVal(String newVal) {
this.newVal = newVal;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
*
* @author yizzuide
* @since 3.6.0
* @version 3.6.1
* @version 3.6.2
* Create at 2020/05/21 18:36
*/
@Slf4j
Expand All @@ -40,7 +40,7 @@ public MetalContainer(MetalSource metalSource) {
public void init(Map<String, String> source) {
metalSource.putAll(source);
for (Map.Entry<String, String> entry : source.entrySet()) {
updateVNode(entry.getKey(), entry.getValue());
updateVNode(entry.getKey(), null, entry.getValue());
}
}

Expand Down Expand Up @@ -76,8 +76,7 @@ public void addVNode(Metal metal, Object target, Field field) {
* @param key 绑定key
* @param newVal 新值
*/
public void updateVNode(String key, String newVal) {
String oldVal = metalSource.get(key);
public void updateVNode(String key, String oldVal, String newVal) {
metalSource.put(key, newVal);
Set<VirtualNode> cacheSet = vNodeCache.get(key);
if (CollectionUtils.isEmpty(cacheSet)) {
Expand All @@ -89,6 +88,14 @@ public void updateVNode(String key, String newVal) {
}
}

/**
* 获取配置源
* @return MetalSource
*/
public MetalSource getSource() {
return metalSource;
}

@Data
public static class VirtualNode {
private Metal metal;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.github.yizzuide.milkomeda.metal;

import com.github.yizzuide.milkomeda.universe.context.ApplicationContextHolder;
import org.springframework.util.CollectionUtils;

import java.util.Map;
Expand All @@ -9,7 +10,7 @@
*
* @author yizzuide
* @since 3.6.0
* @version 3.6.1
* @version 3.6.2
* Create at 2020/05/21 23:19
*/
public class MetalHolder {
Expand Down Expand Up @@ -49,12 +50,23 @@ public static String getProperty(String key) {
return metalContainer.getProperty(key);
}


/**
* 合并配置源
* @param source 配置源
* @param source 配置源
* @since 3.6.1
*/
public static void merge(Map<String, String> source) {
merge(source, false);
}

/**
* 合并配置源
* @param source 配置源
* @param syncRemote 远程同步更新
* @since 3.6.2
*/
public static void merge(Map<String, String> source, boolean syncRemote) {
if (CollectionUtils.isEmpty(source)) {
return;
}
Expand All @@ -66,6 +78,10 @@ public static void merge(Map<String, String> source) {
if (oldVal != null && oldVal.equals(value)) {
continue;
}
if (syncRemote) {
remoteUpdateProperty(key, value);
continue;
}
updateProperty(key, value);
}
}
Expand All @@ -76,8 +92,12 @@ public static void merge(Map<String, String> source) {
* @param value 新值
*/
public static void updateProperty(String key, String value) {
String oldVal = metalContainer.getProperty(key);
if (oldVal != null && !oldVal.equals(value)) {
ApplicationContextHolder.get().publishEvent(new MetalChangeEvent(metalContainer.getSource(), key, oldVal, value));
}
synchronized(MetalHolder.class) {
metalContainer.updateVNode(key, value);
metalContainer.updateVNode(key, oldVal, value);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
*
* @author yizzuide
* @since 3.6.0
* @version 3.6.2
* Create at 2020/05/21 18:26
*/
public class MetalSource {
Expand All @@ -26,4 +27,8 @@ public void put(String key, String value) {
public String get(String key) {
return this.sourceMap.get(key);
}

public boolean isEmpty() {
return this.sourceMap.size() == 0;
}
}
2 changes: 1 addition & 1 deletion MilkomedaDemo/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<spring-cloud.version>Hoxton.RELEASE</spring-cloud.version>
<milkomeda.version>3.6.1-SNAPSHOT</milkomeda.version>
<milkomeda.version>3.6.2-SNAPSHOT</milkomeda.version>
<mybatis.starter>2.1.1</mybatis.starter>
<redission.version>3.12.5</redission.version>
<zookeeper.version>3.4.14</zookeeper.version>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package com.github.yizzuide.milkomeda.demo.metal;

import com.github.yizzuide.milkomeda.metal.Metal;
import com.github.yizzuide.milkomeda.metal.MetalChangeEvent;
import com.github.yizzuide.milkomeda.metal.MetalHolder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.event.EventListener;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

Expand Down Expand Up @@ -44,4 +46,10 @@ public String update(String name) {
MetalHolder.remoteUpdateProperty("platform", name);
return "OK";
}

// 监听平台名修改
@EventListener(condition = "event.key == 'platform'")
public void handlePlatformChange(MetalChangeEvent event) {
log.info("handlePlatformChange: {}", event);
}
}
4 changes: 4 additions & 0 deletions MilkomedaDemo/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -358,4 +358,8 @@ milkomeda:
match: size == 0
syntax: OGNL

# 分布式应用名配置(默认取当前应用名)
metal:
application-name: demo


0 comments on commit 3206f72

Please sign in to comment.