Skip to content

Commit 3a1e681

Browse files
committed
Add Service feature. Add Service, Change Service, Query Service
1 parent 7d0c3e7 commit 3a1e681

File tree

10 files changed

+206
-6
lines changed

10 files changed

+206
-6
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.longcoding.moon.controllers.internal;
2+
3+
import com.longcoding.moon.models.ehcache.ServiceInfo;
4+
import com.longcoding.moon.models.enumeration.SyncType;
5+
import com.longcoding.moon.models.internal.EnrollService;
6+
import com.longcoding.moon.services.internal.ServiceService;
7+
import lombok.extern.slf4j.Slf4j;
8+
import org.springframework.beans.factory.annotation.Autowired;
9+
import org.springframework.web.bind.annotation.*;
10+
11+
/**
12+
* Created by longcoding on 19. 6. 11..
13+
*/
14+
15+
@Slf4j
16+
@RestController
17+
@RequestMapping(value = "/internal/services")
18+
public class ServiceController {
19+
20+
@Autowired
21+
ServiceService serviceService;
22+
23+
@RequestMapping(method = RequestMethod.POST)
24+
public ServiceInfo createService(@RequestBody EnrollService enrollService) { return serviceService.createOrUpdateService(SyncType.CREATE, enrollService); }
25+
26+
@RequestMapping(value = "{serviceId}", method = RequestMethod.GET)
27+
public ServiceInfo getServiceInfo(@PathVariable int serviceId) { return serviceService.getServiceInfo(serviceId); }
28+
29+
@RequestMapping(value = "{serviceId}", method = RequestMethod.PUT)
30+
public ServiceInfo updateService(@RequestBody EnrollService enrollService) { return serviceService.createOrUpdateService(SyncType.UPDATE, enrollService); }
31+
32+
}

src/main/java/com/longcoding/moon/helpers/Constant.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ public class Constant {
5959

6060
public static final String REDIS_KEY_APP_UPDATE = "internal:sync:update:apps";
6161
public static final String REDIS_KEY_API_UPDATE = "internal:sync:update:apis";
62+
public static final String REDIS_KEY_SERVICE_UPDATE = "internal:sync:update:services";
6263
public static final String REDIS_KEY_APP_WHITELIST_UPDATE = "internal:sync:update:whitelist";
6364

6465
public static final String CONTENT_TYPE_EXTRACT_DELIMITER = ";";

src/main/java/com/longcoding/moon/helpers/cluster/DBClusterRepository.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,13 +96,19 @@ public boolean modifyAppInfo(AppInfo appInfo) {
9696
}
9797

9898
@Override
99-
public boolean setServiceInfo(ServiceInfo serviceInfo) {
99+
public ServiceInfo setServiceInfo(ServiceInfo serviceInfo) {
100100
boolean exists = serviceInfoRepository.existsById(serviceInfo.getServiceId());
101-
if (!exists) serviceInfoRepository.saveAndFlush(serviceInfo);
102-
return true;
101+
if (!exists) return serviceInfoRepository.saveAndFlush(serviceInfo);
102+
else return serviceInfo;
103103
}
104104

105105
@Override
106+
public boolean modifyServiceInfo(ServiceInfo serviceInfo) {
107+
serviceInfoRepository.saveAndFlush(serviceInfo);
108+
return true;
109+
}
110+
111+
@Override
106112
public ServiceInfo getServiceInfo(int serviceId) {
107113
Optional<ServiceInfo> serviceInfoOpt = serviceInfoRepository.findById(serviceId);
108114
return serviceInfoOpt.orElseThrow(() -> new GeneralException(ExceptionType.E_1004_RESOURCE_NOT_FOUND));

src/main/java/com/longcoding/moon/helpers/cluster/IClusterRepository.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@ public interface IClusterRepository {
2828

2929
boolean modifyAppInfo(AppInfo appInfo);
3030

31-
boolean setServiceInfo(ServiceInfo serviceInfo);
31+
ServiceInfo setServiceInfo(ServiceInfo serviceInfo);
32+
33+
boolean modifyServiceInfo(ServiceInfo serviceInfo);
3234

3335
ServiceInfo getServiceInfo(int serviceId);
3436

src/main/java/com/longcoding/moon/helpers/cluster/RedisClusterRepository.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,10 +119,18 @@ public boolean modifyAppInfo(AppInfo appInfo) {
119119
}
120120

121121
@Override
122-
public boolean setServiceInfo(ServiceInfo serviceInfo) {
122+
public ServiceInfo setServiceInfo(ServiceInfo serviceInfo) {
123123
try (Jedis jedis = jedisFactory.getInstance()) {
124-
return jedis.hsetnx(Constant.REDIS_KEY_INTERNAL_SERVICE_INFO, String.valueOf(serviceInfo.getServiceId()), JsonUtil.fromJson(serviceInfo)) == 1;
124+
String totalServices = jedis.hlen(Constant.REDIS_KEY_INTERNAL_SERVICE_INFO).toString();
125+
serviceInfo.setServiceId(Integer.valueOf(totalServices));
126+
jedis.hsetnx(Constant.REDIS_KEY_INTERNAL_SERVICE_INFO, String.valueOf(serviceInfo.getServiceId()), JsonUtil.fromJson(serviceInfo));
125127
}
128+
return serviceInfo;
129+
}
130+
131+
@Override
132+
public boolean modifyServiceInfo(ServiceInfo serviceInfo) {
133+
return hset(Constant.REDIS_KEY_INTERNAL_SERVICE_INFO, String.valueOf(serviceInfo.getServiceId()), JsonUtil.fromJson(serviceInfo)) == 0;
126134
}
127135

128136
@Override
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.longcoding.moon.models.cluster;
2+
3+
import com.longcoding.moon.models.ehcache.ServiceInfo;
4+
import com.longcoding.moon.models.enumeration.SyncType;
5+
import lombok.AllArgsConstructor;
6+
import lombok.Data;
7+
import lombok.NoArgsConstructor;
8+
9+
/**
10+
* Created by longcoding on 19. 6. 11..
11+
*/
12+
13+
@Data
14+
@AllArgsConstructor
15+
@NoArgsConstructor
16+
public class ServiceSync {
17+
18+
SyncType type;
19+
ServiceInfo serviceInfo;
20+
21+
}
22+
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.longcoding.moon.models.internal;
2+
3+
import lombok.Data;
4+
5+
import java.io.Serializable;
6+
7+
/**
8+
* Created by longcoding on 19. 6. 11..
9+
*/
10+
11+
@Data
12+
public class EnrollService implements Serializable, Cloneable {
13+
14+
private int serviceId;
15+
private String serviceName;
16+
private String servicePath;
17+
private String outboundServiceHost;
18+
private boolean skipApiTransform;
19+
20+
private String minutelyCapacity;
21+
private String dailyCapacity;
22+
23+
}
24+

src/main/java/com/longcoding/moon/schedulers/ClusterSync.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.longcoding.moon.helpers.*;
44
import com.longcoding.moon.helpers.cluster.ClusterSyncUtil;
5+
import com.longcoding.moon.models.cluster.ServiceSync;
56
import com.longcoding.moon.models.cluster.WhitelistIpSync;
67
import com.longcoding.moon.models.cluster.ApiSync;
78
import com.longcoding.moon.models.cluster.AppSync;
@@ -60,6 +61,9 @@ private void clusterSync() {
6061

6162
//Application Whitelist Sync
6263
appWhitelistSync();
64+
65+
//Service Information Sync
66+
serviceInfoSync();
6367
}
6468

6569
/**
@@ -126,4 +130,17 @@ private void apiInfoSync() {
126130
}
127131
}
128132

133+
private void serviceInfoSync() {
134+
try(Jedis jedisClient = jedisFactory.getInstance()) {
135+
Set<String> targetKeys = jedisClient.keys(String.join("-", Constant.REDIS_KEY_SERVICE_UPDATE, HttpHelper.getHostName() + serverPort + "*"));
136+
targetKeys.forEach(redisKey -> {
137+
String serviceSyncInString = jedisClient.get(redisKey);
138+
ServiceSync serviceSync = JsonUtil.fromJson(serviceSyncInString, ServiceSync.class);
139+
log.info("Found New Update Service Information - method: {}, serviceId: {}", serviceSync.getType().getDescription(), serviceSync.getServiceInfo().getServiceId());
140+
boolean result = syncService.syncServiceInfoToCache(serviceSync);
141+
if (result) jedisClient.del(redisKey);
142+
});
143+
}
144+
}
145+
129146
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package com.longcoding.moon.services.internal;
2+
3+
import com.longcoding.moon.helpers.Constant;
4+
import com.longcoding.moon.helpers.JsonUtil;
5+
import com.longcoding.moon.helpers.cluster.ClusterSyncUtil;
6+
import com.longcoding.moon.helpers.cluster.IClusterRepository;
7+
import com.longcoding.moon.models.cluster.ServiceSync;
8+
import com.longcoding.moon.models.ehcache.ServiceInfo;
9+
import com.longcoding.moon.models.enumeration.RoutingType;
10+
import com.longcoding.moon.models.enumeration.SyncType;
11+
import com.longcoding.moon.models.internal.EnrollService;
12+
import lombok.extern.slf4j.Slf4j;
13+
import org.springframework.beans.factory.annotation.Autowired;
14+
import org.springframework.stereotype.Service;
15+
16+
/**
17+
* Created by longcoding on 19. 6. 11..
18+
*/
19+
20+
@Slf4j
21+
@Service
22+
public class ServiceService {
23+
24+
@Autowired
25+
IClusterRepository clusterRepository;
26+
27+
@Autowired
28+
ClusterSyncUtil clusterSyncUtil;
29+
30+
public ServiceInfo createOrUpdateService(SyncType syncType, EnrollService enrollService) {
31+
ServiceInfo serviceInfo = convertedEnrollServiceToServiceInfo(enrollService);
32+
33+
ServiceInfo storedServiceInfo = clusterRepository.setServiceInfo(serviceInfo);
34+
35+
if (SyncType.CREATE == syncType) clusterRepository.setServiceInfo(storedServiceInfo);
36+
else if (SyncType.UPDATE == syncType) clusterRepository.modifyServiceInfo(storedServiceInfo);
37+
ServiceSync serviceSync = new ServiceSync(syncType, storedServiceInfo);
38+
clusterSyncUtil.setexInfoToHealthyNode(Constant.REDIS_KEY_SERVICE_UPDATE, Constant.SECOND_OF_HOUR, JsonUtil.fromJson(serviceSync));
39+
return storedServiceInfo;
40+
}
41+
42+
public ServiceInfo getServiceInfo(int serviceId) {
43+
return clusterRepository.getServiceInfo(serviceId);
44+
}
45+
46+
private ServiceInfo convertedEnrollServiceToServiceInfo(EnrollService enrollService) {
47+
return ServiceInfo.builder()
48+
.serviceId(enrollService.getServiceId())
49+
.serviceName(enrollService.getServiceName())
50+
.servicePath(enrollService.getServicePath())
51+
.routingType(enrollService.isSkipApiTransform()? RoutingType.SKIP_API_TRANSFORM: RoutingType.API_TRANSFER)
52+
.outboundServiceHost(enrollService.getOutboundServiceHost())
53+
.dailyCapacity(enrollService.getDailyCapacity())
54+
.minutelyCapacity(enrollService.getMinutelyCapacity())
55+
.build();
56+
}
57+
58+
}
59+

src/main/java/com/longcoding/moon/services/sync/SyncService.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.longcoding.moon.services.sync;
22

3+
import com.longcoding.moon.models.cluster.ServiceSync;
34
import com.longcoding.moon.models.cluster.WhitelistIpSync;
45
import com.longcoding.moon.helpers.APIExposeSpecification;
56
import com.longcoding.moon.helpers.HttpHelper;
@@ -8,6 +9,7 @@
89
import com.longcoding.moon.models.ehcache.ApiInfo;
910
import com.longcoding.moon.models.ehcache.AppInfo;
1011
import com.longcoding.moon.models.ehcache.ServiceInfo;
12+
import com.longcoding.moon.models.ehcache.ServiceRoutingInfo;
1113
import com.longcoding.moon.models.enumeration.MethodType;
1214
import com.longcoding.moon.models.enumeration.SyncType;
1315
import lombok.extern.slf4j.Slf4j;
@@ -227,4 +229,31 @@ private boolean deleteApi(ApiInfo apiInfo) {
227229
return true;
228230
}
229231

232+
public boolean syncServiceInfoToCache(ServiceSync serviceSyncInfo) {
233+
234+
SyncType crudType = serviceSyncInfo.getType();
235+
if (SyncType.CREATE == crudType) {
236+
return createOrUpdateService(SyncType.CREATE, serviceSyncInfo.getServiceInfo());
237+
} else if (SyncType.UPDATE == crudType) {
238+
return createOrUpdateService(SyncType.UPDATE, serviceSyncInfo.getServiceInfo());
239+
}
240+
241+
return false;
242+
}
243+
244+
private boolean createOrUpdateService(SyncType syncType, ServiceInfo serviceInfo) {
245+
246+
String servicePath = serviceInfo.getServicePath().startsWith("/")? serviceInfo.getServicePath().substring(1) : serviceInfo.getServicePath();
247+
248+
if (SyncType.CREATE == syncType) {
249+
apiExposeSpec.getServiceTypeCache().put(servicePath, new ServiceRoutingInfo(serviceInfo.getServiceId(), serviceInfo.getRoutingType()));
250+
apiExposeSpec.getServiceInfoCache().put(serviceInfo.getServiceId(), serviceInfo);
251+
} else if (SyncType.UPDATE == syncType) {
252+
apiExposeSpec.getServiceTypeCache().replace(servicePath, new ServiceRoutingInfo(serviceInfo.getServiceId(), serviceInfo.getRoutingType()));
253+
apiExposeSpec.getServiceInfoCache().replace(serviceInfo.getServiceId(), serviceInfo);
254+
}
255+
256+
return true;
257+
}
258+
230259
}

0 commit comments

Comments
 (0)