Skip to content

Commit

Permalink
add ServiceDiscovery#getInstance api
Browse files Browse the repository at this point in the history
add ServiceDiscovery#getInstanceTtl api
  • Loading branch information
Ahoo-Wang committed May 7, 2021
1 parent 0785bb5 commit edfc9ce
Show file tree
Hide file tree
Showing 8 changed files with 76 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,8 @@ public interface ServiceDiscovery {

CompletableFuture<List<ServiceInstance>> getInstances(String namespace, String serviceId);

CompletableFuture<ServiceInstance> getInstance(String namespace, String serviceId, String instanceId);

CompletableFuture<Integer> getInstanceTtl(String namespace, String serviceId, String instanceId);

}
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,16 @@ public CompletableFuture<List<ServiceInstance>> getInstances(String namespace, S
.collect(Collectors.toList()));
}

@Override
public CompletableFuture<ServiceInstance> getInstance(String namespace, String serviceId, String instanceId) {
return delegate.getInstance(namespace, serviceId, instanceId);
}

@Override
public CompletableFuture<Integer> getInstanceTtl(String namespace, String serviceId, String instanceId) {
return delegate.getInstanceTtl(namespace, serviceId, instanceId);
}

@VisibleForTesting
public CompletableFuture<Void> addListener(String namespace, String serviceId) {
PatternTopic instanceTopic = getPatternTopic(namespace, serviceId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,16 @@ public CompletableFuture<List<ServiceInstance>> getInstances(String namespace, S
});
}

@Override
public CompletableFuture<ServiceInstance> getInstance(String namespace, String serviceId, String instanceId) {
return null;
}

@Override
public CompletableFuture<Integer> getInstanceTtl(String namespace, String serviceId, String instanceId) {
return null;
}

@Override
public CompletableFuture<Set<String>> getServices(String namespace) {
var serviceIdxKey = DiscoveryKeyGenerator.getServiceIdxKey(namespace);
Expand Down
37 changes: 37 additions & 0 deletions discovery/src/main/resources/discovery_get_instance.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
local namespace = KEYS[1];
local serviceId = KEYS[2];
local instanceId = KEYS[3];
local instanceIdxKey = namespace .. ':svc_itc_idx:' .. serviceId;

local function getInstanceKey(instanceId)
return namespace .. ":svc_itc:" .. instanceId;
end

local function ensureNotExpired(instanceIdxKey, instanceId)
local instanceKey = getInstanceKey(instanceId);
local instanceTtl = redis.call("ttl", instanceKey);
-- -2: The key doesn't exist | -1: The key is fixed | >0: ttl(second)
if instanceTtl == -2 then
redis.call("srem", instanceIdxKey, instanceId);
redis.call("del", instanceKey);
redis.call("publish", instanceKey, "expired");
end

return instanceTtl;
end

local instanceTtl = ensureNotExpired(instanceIdxKey, instanceId);
if instanceTtl ~= -2 then
local instanceKey = getInstanceKey(instanceId);
local instanceData = redis.call('hgetall', instanceKey);
if instanceTtl > 0 then
instanceData[#instanceData + 1] = "ttl_at";
local nowTime = redis.call('time')[1];
local ttlAt = nowTime + instanceTtl;
instanceData[#instanceData + 1] = tostring(ttlAt);
end
return instanceData;
end



16 changes: 9 additions & 7 deletions discovery/src/main/resources/discovery_get_instances.lua
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ end
local function ensureNotExpired(instanceIdxKey, instanceId)
local instanceKey = getInstanceKey(instanceId);
local instanceTtl = redis.call("ttl", instanceKey);

if instanceTtl < 0 then
-- -2: The key doesn't exist | -1: The key is fixed | >0: ttl(second)
if instanceTtl == -2 then
redis.call("srem", instanceIdxKey, instanceId);
redis.call("del", instanceKey);
redis.call("publish", instanceKey, "expired");
Expand All @@ -24,13 +24,15 @@ end

for index, instanceId in ipairs(instanceIds) do
local instanceTtl = ensureNotExpired(instanceIdxKey, instanceId);
if instanceTtl > 0 then
if instanceTtl ~= -2 then
local instanceKey = getInstanceKey(instanceId);
local instanceData = redis.call('hgetall', instanceKey);
instanceData[#instanceData + 1] = "ttl_at";
local nowTime = redis.call('time')[1];
local ttlAt = nowTime + instanceTtl;
instanceData[#instanceData + 1] = tostring(ttlAt);
if instanceTtl > 0 then
instanceData[#instanceData + 1] = "ttl_at";
local nowTime = redis.call('time')[1];
local ttlAt = nowTime + instanceTtl;
instanceData[#instanceData + 1] = tostring(ttlAt);
end
instancesIdx = instancesIdx + 1;
instances[instancesIdx] = instanceData;
end
Expand Down
6 changes: 3 additions & 3 deletions discovery/src/main/resources/service_stat.lua
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ end
local function ensureNotExpired(instanceIdxKey, instanceId)
local instanceKey = getInstanceKey(instanceId);
local instanceTtl = redis.call("ttl", instanceKey);

if instanceTtl < 0 then
-- -2: The key doesn't exist | -1: The key is fixed | >0: ttl(second)
if instanceTtl == -2 then
redis.call("srem", instanceIdxKey, instanceId);
redis.call("del", instanceKey);
redis.call("publish", instanceKey, "expired");
Expand All @@ -37,7 +37,7 @@ local function statService(serviceId)

for index, instanceId in ipairs(instanceIds) do
local instanceTtl = ensureNotExpired(instanceIdxKey, instanceId);
if instanceTtl > 0 then
if instanceTtl ~= -2 then
instanceCount = instanceCount + 1;
end
end
Expand Down
2 changes: 1 addition & 1 deletion docker/rest-api/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# docker build --build-arg JDK_VERSION=armv7l-centos-jdk-11.0.8_10-slim --build-arg GOVERN_VERSION=0.7.2 -t ahoowang/govern-service:0.7.2 .
ARG JDK_VERSION=alpine
ARG GOVERN_VERSION=0.7.2
ARG GOVERN_VERSION=0.8.1
ARG GOVERN_SERVICE_HOME=/govern-service
FROM adoptopenjdk/openjdk11:${JDK_VERSION} AS base

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

description=Govern Service On Redis
website=https://github.com/Ahoo-Wang/govern-service
Expand All @@ -8,3 +8,4 @@ vcs=https://github.com/Ahoo-Wang/govern-service.git

license_name=The Apache Software License, Version 2.0
license_url=https://www.apache.org/licenses/LICENSE-2.0.txt

0 comments on commit edfc9ce

Please sign in to comment.