Skip to content

Commit d37d787

Browse files
committed
[fit] 统一内存版注册中心和nacos注册中心的配置方式
1 parent 634ed23 commit d37d787

File tree

10 files changed

+342
-136
lines changed

10 files changed

+342
-136
lines changed

examples/fit-example/08-nacos-complicated-apps/app-assistant/src/main/resources/application.yml

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,12 @@ worker:
77
environment: 'local'
88
environment-sequence: 'local'
99

10-
nacos:
11-
serverAddr: 127.0.0.1:8848
10+
matata:
11+
registry:
12+
mode: 'NACOS'
13+
host: '127.0.0.1'
14+
port: 8848
15+
environment: 'local'
1216

1317
server:
1418
http:

examples/fit-example/08-nacos-complicated-apps/app-default-weather/src/main/resources/application.yml

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,12 @@ worker:
77
environment: 'local'
88
environment-sequence: 'local'
99

10-
nacos:
11-
serverAddr: 127.0.0.1:8848
10+
matata:
11+
registry:
12+
mode: 'NACOS'
13+
host: '127.0.0.1'
14+
port: 8848
15+
environment: 'local'
1216

1317
server:
1418
http:

framework/fit/java/fit-api/src/main/java/modelengine/fitframework/conf/runtime/MatataConfig.java

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,13 @@ interface Registry {
3636
*/
3737
String host();
3838

39+
/**
40+
* 获取 {@code 'matata.registry.mode'} 的配置项。
41+
*
42+
* @return 表示 {@code 'matata.registry.mode'} 的配置项的 {@link RegistryCenterMode}。
43+
*/
44+
RegistryCenterMode mode();
45+
3946
/**
4047
* 获取 {@code 'matata.registry.port'} 的配置项。
4148
*
@@ -103,6 +110,8 @@ interface Registry {
103110
*/
104111
SecureAccess secureAccess();
105112

113+
Nacos nacos();
114+
106115
/**
107116
* 表示 {@code 'matata.registry'} 中关于可用服务的配置项。
108117
*/
@@ -198,5 +207,66 @@ interface SecureAccess {
198207
*/
199208
String secretKey();
200209
}
210+
211+
/**
212+
* 表示 {@code 'matata.registry.nacos'} 前缀的配置项。
213+
*/
214+
interface Nacos {
215+
/**
216+
* 获取 {@code 'matata.registry.nacos.username'} 的配置项。
217+
*
218+
* @return 表示 {@code 'matata.registry.nacos.username'} 的配置项的 {@link String}。
219+
*/
220+
String username();
221+
222+
/**
223+
* 获取 {@code 'matata.registry.nacos.password'} 的配置项。
224+
*
225+
* @return 表示 {@code 'matata.registry.nacos.password'} 的配置项的 {@link String}。
226+
*/
227+
String password();
228+
229+
/**
230+
* 获取 {@code 'matata.registry.nacos.access-key'} 的配置项。
231+
*
232+
* @return 表示 {@code 'matata.registry.nacos.access-key'} 的配置项的 {@link String}。
233+
*/
234+
String accessKey();
235+
236+
/**
237+
* 获取 {@code 'matata.registry.nacos.secret-key'} 的配置项。
238+
*
239+
* @return 表示 {@code 'matata.registry.nacos.secret-key'} 的配置项的 {@link String}。
240+
*/
241+
String secretKey();
242+
243+
/**
244+
* 获取 {@code 'matata.registry.nacos.is-ephemeral'} 的配置项。
245+
*
246+
* @return 表示 {@code 'matata.registry.nacos.is-ephemeral'} 的配置项的 {@link Boolean}。
247+
*/
248+
Boolean isEphemeral();
249+
250+
/**
251+
* 获取 {@code 'matata.registry.nacos.weight'} 的配置项。
252+
*
253+
* @return 表示 {@code 'matata.registry.nacos.weight'} 的配置项的 {@link Float}。
254+
*/
255+
Float weight();
256+
257+
/**
258+
* 获取 {@code 'matata.registry.nacos.heartbeat-interval'} 的配置项。
259+
*
260+
* @return 表示 {@code 'matata.registry.nacos.heartbeat-interval'} 的配置项的 {@link Long}。
261+
*/
262+
Long heartbeatInterval();
263+
264+
/**
265+
* 获取 {@code 'matata.registry.nacos.heartbeat-timeout'} 的配置项。
266+
*
267+
* @return 表示 {@code 'matata.registry.nacos.heartbeat-timeout'} 的配置项的 {@link Long}。
268+
*/
269+
Long heartbeatTimeout();
270+
}
201271
}
202272
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package modelengine.fitframework.conf.runtime;
2+
3+
import static modelengine.fitframework.inspection.Validation.notBlank;
4+
5+
import modelengine.fitframework.util.StringUtils;
6+
7+
import java.util.Arrays;
8+
9+
/**
10+
* 注册中心地址监听模式枚举。
11+
*
12+
* @author 董智豪
13+
* @since 2025-08-04
14+
*/
15+
public enum RegistryCenterMode {
16+
/** 支持本地内存实现的注册中心。 */
17+
MEMORY("MEMORY"),
18+
19+
/** 支持 Nacos 实现的注册中心。 */
20+
NACOS("NACOS"),
21+
;
22+
23+
/** 注册中心模式标识字符串。 */
24+
private final String mode;
25+
26+
/**
27+
* 构造方法,校验并设置注册中心模式标识。
28+
*
29+
* @param mode 注册中心模式标识字符串
30+
*/
31+
RegistryCenterMode(String mode) {
32+
this.mode = notBlank(mode, "The registry center mode cannot be blank.");
33+
}
34+
35+
/**
36+
* 根据字符串获取对应的注册中心模式枚举。
37+
*
38+
* @param mode 注册中心监听模式标识字符串
39+
* @return 匹配的 {@link RegistryCenterMode} 枚举常量,未匹配返回 null
40+
*/
41+
public static RegistryCenterMode fromMode(String mode) {
42+
return Arrays.stream(RegistryCenterMode.values())
43+
.filter(registryCenterMode -> StringUtils.equals(registryCenterMode.mode, mode))
44+
.findFirst()
45+
.orElse(null);
46+
}
47+
}

framework/fit/java/fit-builtin/plugins/fit-service-coordination-locator/src/main/java/modelengine/fit/service/locator/AddressRepository.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import modelengine.fitframework.broker.Target;
1717
import modelengine.fitframework.conf.runtime.CommunicationProtocol;
1818
import modelengine.fitframework.conf.runtime.MatataConfig;
19+
import modelengine.fitframework.conf.runtime.RegistryCenterMode;
1920
import modelengine.fitframework.conf.runtime.WorkerConfig;
2021
import modelengine.fitframework.log.Logger;
2122
import modelengine.fitframework.util.ObjectUtils;
@@ -53,20 +54,19 @@ public AddressRepository(List<FitServer> servers, WorkerConfig worker, MatataCon
5354
int port = matata.registry().port();
5455
int protocolCode = matata.registry().protocolCode();
5556
CommunicationProtocol protocol = matata.registry().protocol();
56-
if (port == 0) {
57-
log.debug("The registry port is not set, using the first endpoint of the fit server.");
57+
String host = matata.registry().host();
58+
59+
if(matata.registry().mode() != null && RegistryCenterMode.NACOS.equals(matata.registry().mode())) {
60+
log.debug("The registry mode is Nacos, using the local proxy registry center.");
5861
int size = fitServer.endpoints().size();
5962
greaterThan(size, 0, "The fit server must have at least one endpoint.");
6063
Endpoint endpoint = fitServer.endpoints().get(0);
6164
port = endpoint.port();
6265
protocolCode = endpoint.protocolCode();
6366
protocol = CommunicationProtocol.from(endpoint.protocol());
64-
}
65-
String host = matata.registry().host();
66-
if (StringUtils.isBlank(matata.registry().host())) {
67-
log.debug("The registry host is not set, using the worker host.");
6867
host = worker.host();
6968
}
69+
7070
boolean isRegistryLocalhost =
7171
isRegistryLocalhost(actualServers, worker.host(), worker.domain(), host, port, protocolCode);
7272
String registryWorkerId = isRegistryLocalhost ? worker.id() : host + ":" + port;

framework/fit/java/fit-builtin/plugins/fit-service-coordination-nacos/src/main/java/modelengine/fit/heartbeat/server/HeartbeatConfig.java

Lines changed: 0 additions & 46 deletions
This file was deleted.

framework/fit/java/fit-builtin/plugins/fit-service-coordination-nacos/src/main/java/modelengine/fit/service/server/NacosConfig.java

Lines changed: 0 additions & 58 deletions
This file was deleted.

0 commit comments

Comments
 (0)