Skip to content

Commit

Permalink
- add support for publishing *RefreshEvent* when configuration changes
Browse files Browse the repository at this point in the history
- fix remove ServiceInstance#metadata bug
  • Loading branch information
Ahoo-Wang committed May 15, 2021
1 parent f86ca99 commit 5be1e55
Show file tree
Hide file tree
Showing 8 changed files with 111 additions and 9 deletions.
10 changes: 10 additions & 0 deletions discovery/src/main/resources/registry_register.lua
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,16 @@ if added == 1 then
redis.call("publish", serviceIdxKey, "register");
redis.call("sadd", serviceIdxKey, serviceId);
end

local instanceKeys = redis.call("hkeys", instanceKey)
if #instanceKeys > 0 then
for i, key in ipairs(instanceKeys) do
if string.find(key, '_', 1) == 1 and string.find(key, '__', 1) == nil then
redis.call("hdel", instanceKey, key);
end
end
end

redis.call("hmset", instanceKey, "instanceId", instanceId, "serviceId", serviceId, "schema", schema, "ip", ip, "port", port, "weight", weight, "ephemeral", ephemeral, unpack(ARGV, 8, #ARGV));
redis.call("publish", instanceKey, "register");
if not fixed then
Expand Down
9 changes: 9 additions & 0 deletions discovery/src/main/resources/registry_set_metadata.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,15 @@ local instanceId = ARGV[1];

local instanceKey = namespace .. ":svc_itc:" .. instanceId;

local instanceKeys = redis.call("hkeys", instanceKey)
if #instanceKeys > 0 then
for i, key in ipairs(instanceKeys) do
if string.find(key, '_', 1) == 1 and string.find(key, '__', 1) == nil then
redis.call("hdel", instanceKey, key);
end
end
end

local result = redis.call("hmset", instanceKey, unpack(ARGV, 2, #ARGV));
redis.call("publish", instanceKey, "set_metadata");
return result;
Expand Down
2 changes: 1 addition & 1 deletion discovery/src/test/resources/logback.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
</encoder>
</appender>

<root level="INFO">
<root level="WARN">
<appender-ref ref="CONSOLE"/>
</root>
</configuration>
6 changes: 3 additions & 3 deletions docker/rest-api/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# docker buildx build --push --platform linux/arm/v7 --build-arg GOVERN_VERSION=0.9.6 --build-arg JDK_VERSION=armv7l-centos-jre-11.0.11_9 -t ahoowang/govern-service:0.9.6-armv7 .
# docker buildx build --push --platform linux/amd64,linux/arm64 --build-arg GOVERN_VERSION=0.9.6 --build-arg JDK_VERSION=jre11u-centos-nightly -t ahoowang/govern-service:0.9.6 .
# docker buildx build --push --platform linux/arm/v7 --build-arg GOVERN_VERSION=0.9.8 --build-arg JDK_VERSION=armv7l-centos-jre-11.0.11_9 -t ahoowang/govern-service:0.9.8-armv7 .
# docker buildx build --push --platform linux/amd64,linux/arm64 --build-arg GOVERN_VERSION=0.9.8 --build-arg JDK_VERSION=jre11u-centos-nightly -t ahoowang/govern-service:0.9.8 .

ARG JDK_VERSION=jre11u-centos-nightly
ARG GOVERN_VERSION=0.9.6
ARG GOVERN_VERSION=0.9.8
ARG GOVERN_SERVICE_HOME=/govern-service
FROM adoptopenjdk/openjdk11:${JDK_VERSION} AS base

Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
group=me.ahoo.govern
version=0.9.6
version=0.9.8

description=Govern Service On Redis
website=https://github.com/Ahoo-Wang/govern-service
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,25 @@

import io.lettuce.core.AbstractRedisClient;
import io.lettuce.core.cluster.api.async.RedisClusterAsyncCommands;
import lombok.var;
import me.ahoo.govern.config.ConfigListenable;
import me.ahoo.govern.config.ConfigService;
import me.ahoo.govern.config.redis.ConsistencyRedisConfigService;
import me.ahoo.govern.config.redis.RedisConfigService;
import me.ahoo.govern.config.spring.cloud.refresh.GovernConfigRefresher;
import me.ahoo.govern.core.listener.MessageListenable;
import me.ahoo.govern.spring.cloud.GovernAutoConfiguration;
import me.ahoo.govern.spring.cloud.GovernProperties;
import me.ahoo.govern.spring.cloud.support.AppSupport;
import me.ahoo.govern.spring.cloud.support.RedisClientSupport;
import org.apache.logging.log4j.util.Strings;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.env.Environment;

/**
* {@link org.springframework.cloud.util.PropertyUtils#BOOTSTRAP_ENABLED_PROPERTY}
Expand All @@ -26,6 +33,14 @@
@AutoConfigureAfter(GovernAutoConfiguration.class)
public class GovernConfigBootstrapConfiguration {

public GovernConfigBootstrapConfiguration(GovernConfigProperties governConfigProperties, Environment environment) {
var configId = governConfigProperties.getConfigId();
if (Strings.isBlank(configId)) {
configId = AppSupport.getAppName(environment) + "." + governConfigProperties.getFileExtension();
}
governConfigProperties.setConfigId(configId);
}

@Bean
@ConditionalOnMissingBean
public RedisConfigService redisConfigService(
Expand All @@ -47,4 +62,13 @@ public ConsistencyRedisConfigService consistencyRedisConfigService(
public GovernPropertySourceLocator governPropertySourceLocator(GovernConfigProperties configProperties, ConfigService configService) {
return new GovernPropertySourceLocator(configProperties, configService);
}

@Bean
@ConditionalOnMissingBean
public GovernConfigRefresher governConfigRefresher(GovernProperties governProperties,
GovernConfigProperties configProperties,
ConfigListenable configListenable) {
return new GovernConfigRefresher(governProperties, configProperties, configListenable);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import me.ahoo.govern.config.ConfigService;
import me.ahoo.govern.core.Consts;
import me.ahoo.govern.core.NamespacedContext;
import me.ahoo.govern.spring.cloud.support.AppSupport;
import org.apache.logging.log4j.util.Strings;
import org.springframework.boot.env.OriginTrackedMapPropertySource;
import org.springframework.boot.env.PropertySourceLoader;
Expand Down Expand Up @@ -46,9 +45,6 @@ public GovernPropertySourceLocator(GovernConfigProperties configProperties, Conf
@Override
public PropertySource<?> locate(Environment environment) {
var configId = configProperties.getConfigId();
if (Strings.isBlank(configId)) {
configId = AppSupport.getAppName(environment) + "." + configProperties.getFileExtension();
}

var fileExt = Files.getFileExtension(configId);
if (Strings.isBlank(fileExt)) {
Expand All @@ -59,6 +55,7 @@ public PropertySource<?> locate(Environment environment) {
log.info("locate - configId:[{}] @ namespace:[{}]", configId, namespace);

var config = configService.getConfig(configId).join();

if (Objects.isNull(config)) {
log.warn("locate - can not find configId:[{}] @ namespace:[{}]", configId, namespace);
return new OriginTrackedMapPropertySource(getNameOfConfigId(configId), Collections.emptyMap());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package me.ahoo.govern.config.spring.cloud.refresh;

import lombok.extern.slf4j.Slf4j;
import me.ahoo.govern.config.ConfigChangedListener;
import me.ahoo.govern.config.ConfigListenable;
import me.ahoo.govern.config.NamespacedConfigId;
import me.ahoo.govern.config.spring.cloud.GovernConfigProperties;
import me.ahoo.govern.spring.cloud.GovernProperties;
import org.springframework.beans.BeansException;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.cloud.endpoint.event.RefreshEvent;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.ApplicationListener;

/**
* @author ahoo wang
*/
@Slf4j
public class GovernConfigRefresher implements ApplicationListener<ApplicationReadyEvent>, ApplicationContextAware {
private ApplicationContext applicationContext;
private final ConfigListenable configListenable;
private final GovernProperties governProperties;
private final GovernConfigProperties configProperties;
private final Listener listener;

public GovernConfigRefresher(
GovernProperties governProperties,
GovernConfigProperties configProperties,
ConfigListenable configListenable) {
this.configListenable = configListenable;
this.governProperties = governProperties;
this.configProperties = configProperties;
this.listener = new Listener();
}

@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.applicationContext = applicationContext;
}

/**
* Handle an application event.
*
* @param event the event to respond to
*/
@Override
public void onApplicationEvent(ApplicationReadyEvent event) {
configListenable.addListener(NamespacedConfigId.of(governProperties.getNamespace(), configProperties.getConfigId()), listener);
}

class Listener implements ConfigChangedListener {
@Override
public void onChange(NamespacedConfigId namespacedConfigId, String op) {
if (log.isInfoEnabled()) {
log.info("Refresh - Govern-Service - configId:[{}] - [{}]", configProperties.getConfigId(), op);
}
applicationContext.publishEvent(
new RefreshEvent(this, op, "Refresh Govern-Service config"));
}
}
}

0 comments on commit 5be1e55

Please sign in to comment.