19
19
import java .util .concurrent .TimeUnit ;
20
20
21
21
/**
22
- * Description for this class
22
+ * 用自己手写的注册中心
23
23
*
24
+ * @link <a href="https://github.com/ipipman/registry-man">...</a>
24
25
* @Author IpMan
25
26
* @Date 2024/4/21 13:17
26
27
*/
27
28
@ Slf4j
28
29
public class IpManRegistryCenter implements RegistryCenter {
29
30
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
+
30
37
@ Value ("${registry-ipman.servers}" )
31
38
String server ;
32
39
@@ -47,19 +54,12 @@ public void start() {
47
54
// 定期将服务实例上报给注册中心, 避免被注册中心认为服务已死, 5s一次
48
55
heathChecker = new IpManRegistryExecutor (5 , 5 , TimeUnit .SECONDS );
49
56
heathChecker .executor (() -> RENEWS .keySet ().forEach (
50
- // 根据所有实例, 找到对应服务, 触发renew进行服务健康状态上报, 做探活
51
57
instance -> {
58
+ // 根据所有实例, 找到对应服务, 触发renew进行服务健康状态上报, 做探活
52
59
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 );
61
61
Long timestamp = HttpInvoker .httpPost (
62
- JSON .toJSONString (instance ), reNewsPath (services ), Long .class );
62
+ JSON .toJSONString (instance ), getReNewPath (services ), Long .class );
63
63
log .info (" ====>>>> [IpMan-Registry] : renew instance {} for {} at {}" , instance , services , timestamp );
64
64
} catch (Exception e ) {
65
65
log .error (" ====>>>> [IpMan-Registry] call registry leader error" );
@@ -100,7 +100,6 @@ public List<InstanceMeta> fetchAll(ServiceMeta service) {
100
100
return instances ;
101
101
}
102
102
103
-
104
103
@ Override
105
104
public void subscribe (ServiceMeta service , ChangedListener listener ) {
106
105
// 每隔5s, 去注册中心获取最新版本号,如果版本号大于当前版本, 就从注册中心同步最新实例的信息
@@ -130,23 +129,44 @@ public void unsubscribe() {
130
129
131
130
}
132
131
132
+
133
+
133
134
private String regPath (ServiceMeta service ) {
134
- return server + "/reg? service=" + service . toPath ( );
135
+ return path ( REG_PATH , service );
135
136
}
136
137
137
138
private String unRegPath (ServiceMeta service ) {
138
- return server + "/unreg? service=" + service . toPath ( );
139
+ return path ( UN_REG_PATH , service );
139
140
}
140
141
141
142
private String findAllPath (ServiceMeta service ) {
142
- return server + "/findall? service=" + service . toPath ( );
143
+ return path ( FIND_ALL_PATH , service );
143
144
}
144
145
145
146
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 ();
147
160
}
148
161
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 ;
151
171
}
152
172
}
0 commit comments