Skip to content

Commit 7e6508e

Browse files
committed
refactor registry path
1 parent 181a60e commit 7e6508e

File tree

1 file changed

+38
-18
lines changed

1 file changed

+38
-18
lines changed

rpcman-core/src/main/java/cn/ipman/rpc/core/registry/ipman/IpManRegistryCenter.java

Lines changed: 38 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,21 @@
1919
import java.util.concurrent.TimeUnit;
2020

2121
/**
22-
* Description for this class
22+
* 用自己手写的注册中心
2323
*
24+
* @link <a href="https://github.com/ipipman/registry-man">...</a>
2425
* @Author IpMan
2526
* @Date 2024/4/21 13:17
2627
*/
2728
@Slf4j
2829
public class IpManRegistryCenter implements RegistryCenter {
2930

31+
private final static String REG_PATH = "/reg";
32+
private final static String UN_REG_PATH = "/unreg";
33+
private final static String FIND_ALL_PATH = "/findall";
34+
private final static String VERSION_PATH = "/version";
35+
private final static String RENEW_PATH = "/renew";
36+
3037
@Value("${registry-ipman.servers}")
3138
String server;
3239

@@ -47,19 +54,12 @@ public void start() {
4754
// 定期将服务实例上报给注册中心, 避免被注册中心认为服务已死, 5s一次
4855
heathChecker = new IpManRegistryExecutor(5, 5, TimeUnit.SECONDS);
4956
heathChecker.executor(() -> RENEWS.keySet().forEach(
50-
// 根据所有实例, 找到对应服务, 触发renew进行服务健康状态上报, 做探活
5157
instance -> {
58+
// 根据所有实例, 找到对应服务, 触发renew进行服务健康状态上报, 做探活
5259
try {
53-
StringBuilder sb = new StringBuilder();
54-
for (ServiceMeta service : RENEWS.get(instance)) {
55-
sb.append(service.toPath()).append(",");
56-
}
57-
String services = sb.toString();
58-
if (services.endsWith(","))
59-
services = services.substring(0, services.length() - 1);
60-
60+
List<ServiceMeta> services = RENEWS.get(instance);
6161
Long timestamp = HttpInvoker.httpPost(
62-
JSON.toJSONString(instance), reNewsPath(services), Long.class);
62+
JSON.toJSONString(instance), getReNewPath(services), Long.class);
6363
log.info(" ====>>>> [IpMan-Registry] : renew instance {} for {} at {}", instance, services, timestamp);
6464
} catch (Exception e) {
6565
log.error(" ====>>>> [IpMan-Registry] call registry leader error");
@@ -100,7 +100,6 @@ public List<InstanceMeta> fetchAll(ServiceMeta service) {
100100
return instances;
101101
}
102102

103-
104103
@Override
105104
public void subscribe(ServiceMeta service, ChangedListener listener) {
106105
// 每隔5s, 去注册中心获取最新版本号,如果版本号大于当前版本, 就从注册中心同步最新实例的信息
@@ -130,23 +129,44 @@ public void unsubscribe() {
130129

131130
}
132131

132+
133+
133134
private String regPath(ServiceMeta service) {
134-
return server + "/reg?service=" + service.toPath();
135+
return path(REG_PATH, service);
135136
}
136137

137138
private String unRegPath(ServiceMeta service) {
138-
return server + "/unreg?service=" + service.toPath();
139+
return path(UN_REG_PATH, service);
139140
}
140141

141142
private String findAllPath(ServiceMeta service) {
142-
return server + "/findall?service=" + service.toPath();
143+
return path(FIND_ALL_PATH, service);
143144
}
144145

145146
private String versionPath(ServiceMeta service) {
146-
return server + "/version?service=" + service.toPath();
147+
return path(VERSION_PATH, service);
148+
}
149+
150+
private String getReNewPath(List<ServiceMeta> serviceList){
151+
return path(RENEW_PATH, serviceList);
152+
}
153+
154+
private String path(final String context, List<ServiceMeta> serviceList) {
155+
return server + context + "?services=" + args(serviceList);
156+
}
157+
158+
private String path(String context, ServiceMeta service) {
159+
return server + context + "?service=" + service.toPath();
147160
}
148161

149-
private String reNewsPath(String services) {
150-
return server + "/renews?services=" + services;
162+
private String args(List<ServiceMeta> serviceList){
163+
StringBuilder sb = new StringBuilder();
164+
for (ServiceMeta service : serviceList) {
165+
sb.append(service.toPath()).append(",");
166+
}
167+
String services = sb.toString();
168+
if (services.endsWith(","))
169+
services = services.substring(0, services.length() - 1);
170+
return services;
151171
}
152172
}

0 commit comments

Comments
 (0)