Skip to content

Commit 3e3c375

Browse files
committed
[fit] matata.registry.nacos配置改从插件中读取
1 parent 34fc5c5 commit 3e3c375

File tree

8 files changed

+97
-257
lines changed

8 files changed

+97
-257
lines changed

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

Lines changed: 0 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -110,13 +110,6 @@ interface Registry {
110110
*/
111111
SecureAccess secureAccess();
112112

113-
/**
114-
* 获取 {@code 'matata.registry.proxy'} 的配置项。
115-
*
116-
* @return 表示 {@code 'matata.registry.proxy'} 的配置项的 {@link Proxy}。
117-
*/
118-
Proxy proxy();
119-
120113
/**
121114
* 表示 {@code 'matata.registry'} 中关于可用服务的配置项。
122115
*/
@@ -212,66 +205,5 @@ interface SecureAccess {
212205
*/
213206
String secretKey();
214207
}
215-
216-
/**
217-
* 表示 {@code 'matata.registry.proxy'} 前缀的配置项。
218-
*/
219-
interface Proxy {
220-
/**
221-
* 获取 {@code 'matata.registry.proxy.username'} 的配置项。
222-
*
223-
* @return 表示 {@code 'matata.registry.proxy.username'} 的配置项的 {@link String}。
224-
*/
225-
String username();
226-
227-
/**
228-
* 获取 {@code 'matata.registry.proxy.password'} 的配置项。
229-
*
230-
* @return 表示 {@code 'matata.registry.proxy.password'} 的配置项的 {@link String}。
231-
*/
232-
String password();
233-
234-
/**
235-
* 获取 {@code 'matata.registry.proxy.access-key'} 的配置项。
236-
*
237-
* @return 表示 {@code 'matata.registry.proxy.access-key'} 的配置项的 {@link String}。
238-
*/
239-
String accessKey();
240-
241-
/**
242-
* 获取 {@code 'matata.registry.proxy.secret-key'} 的配置项。
243-
*
244-
* @return 表示 {@code 'matata.registry.proxy.secret-key'} 的配置项的 {@link String}。
245-
*/
246-
String secretKey();
247-
248-
/**
249-
* 获取 {@code 'matata.registry.proxy.is-ephemeral'} 的配置项。
250-
*
251-
* @return 表示 {@code 'matata.registry.proxy.is-ephemeral'} 的配置项的 {@link Boolean}。
252-
*/
253-
Boolean isEphemeral();
254-
255-
/**
256-
* 获取 {@code 'matata.registry.proxy.weight'} 的配置项。
257-
*
258-
* @return 表示 {@code 'matata.registry.proxy.weight'} 的配置项的 {@link Float}。
259-
*/
260-
Float weight();
261-
262-
/**
263-
* 获取 {@code 'matata.registry.proxy.heartbeat-interval'} 的配置项。
264-
*
265-
* @return 表示 {@code 'matata.registry.proxy.heartbeat-interval'} 的配置项的 {@link Long}。
266-
*/
267-
Long heartbeatInterval();
268-
269-
/**
270-
* 获取 {@code 'matata.registry.proxy.heartbeat-timeout'} 的配置项。
271-
*
272-
* @return 表示 {@code 'matata.registry.proxy.heartbeat-timeout'} 的配置项的 {@link Long}。
273-
*/
274-
Long heartbeatTimeout();
275-
}
276208
}
277209
}

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
/*---------------------------------------------------------------------------------------------
2+
* Copyright (c) 2024 Huawei Technologies Co., Ltd. All rights reserved.
3+
* This file is a part of the ModelEngine Project.
4+
* Licensed under the MIT License. See License.txt in the project root for license information.
5+
*--------------------------------------------------------------------------------------------*/
6+
17
package modelengine.fitframework.conf.runtime;
28

39
import static modelengine.fitframework.inspection.Validation.notBlank;
@@ -47,6 +53,6 @@ public static RegistryConnectMode fromMode(String mode) {
4753
return Arrays.stream(RegistryConnectMode.values())
4854
.filter(registryConnectMode -> StringUtils.equals(registryConnectMode.mode, mode))
4955
.findFirst()
50-
.orElse(null);
56+
.orElse(DIRECT);
5157
}
5258
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public AddressRepository(List<FitServer> servers, WorkerConfig worker, MatataCon
5656
CommunicationProtocol protocol = matata.registry().protocol();
5757
String host = matata.registry().host();
5858

59-
if(matata.registry().mode() != null && RegistryConnectMode.PROXY.equals(matata.registry().mode())) {
59+
if (RegistryConnectMode.PROXY.equals(matata.registry().mode())) {
6060
log.debug("The registry mode is Nacos, using the local proxy registry center.");
6161
int size = fitServer.endpoints().size();
6262
greaterThan(size, 0, "The fit server must have at least one endpoint.");
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*
2+
* Copyright (c) 2025 Huawei Technologies Co., Ltd. All rights reserved.
3+
* This file is a part of the ModelEngine Project.
4+
* Licensed under the MIT License. See License.txt in the project root for license information.
5+
*/
6+
7+
package modelengine.fit.service.server;
8+
9+
import lombok.Data;
10+
11+
/**
12+
* Represents the configuration prefixed with {@code 'matata.registry.nacos.'}.
13+
*
14+
* @author 董智豪
15+
* @since 2025-08-06
16+
*/
17+
@Data
18+
public class NacosConfig {
19+
/**
20+
* Login username for Nacos authentication.
21+
* Required when Nacos server has authentication enabled.
22+
*/
23+
private String username;
24+
25+
/**
26+
* Login password for Nacos authentication.
27+
* Used together with username for authentication when connecting to secured Nacos server.
28+
*/
29+
private String password;
30+
31+
/**
32+
* Access key for Nacos authentication.
33+
* Used for access control in cloud environments or when using AK/SK authentication.
34+
*/
35+
private String accessKey;
36+
37+
/**
38+
* Secret key for Nacos authentication.
39+
* Used together with access key for AK/SK authentication mechanism.
40+
*/
41+
private String secretKey;
42+
43+
/**
44+
* Whether it is an ephemeral instance.
45+
* Ephemeral instances will be automatically removed from the registry after service deregistration.
46+
*/
47+
private Boolean isEphemeral;
48+
/**
49+
* Service weight.
50+
* Used for weight calculation during load balancing.
51+
*/
52+
private Float weight;
53+
/**
54+
* Heartbeat interval time (unit: milliseconds).
55+
* Defines the time interval for services to send heartbeats.
56+
*/
57+
private Long heartbeatInterval;
58+
59+
/**
60+
* Heartbeat timeout time (unit: milliseconds).
61+
* Defines the time after which a service is considered timed out when no heartbeat is received.
62+
*/
63+
private Long heartbeatTimeout;
64+
}

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

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -75,18 +75,20 @@ public class NacosRegistryServer implements RegistryService {
7575

7676
private final NamingService namingService;
7777
private final ObjectMapper objectMapper = new ObjectMapper();
78+
private final NacosConfig nacosConfig;
7879
private final MatataConfig matata;
7980
private final Notify notify;
8081
private final WorkerConfig worker;
8182
private final Map<String, com.alibaba.nacos.api.naming.listener.EventListener> serviceSubscriptions =
8283
new ConcurrentHashMap<>();
8384

84-
public NacosRegistryServer(Notify notify, WorkerConfig worker, Config config, MatataConfig matata)
85+
public NacosRegistryServer(Notify notify, WorkerConfig worker, Config config,MatataConfig matata)
8586
throws NacosException {
8687
notNull(config, "The configuration cannot be null.");
87-
this.matata = notNull(matata, "The matata config cannot be null.");
88+
this.matata = notNull(matata, "The matata configuration cannot be null.");
8889
this.notify = notNull(notify, "The registry listener cannot be null.");
8990
this.worker = notNull(worker, "The worker config cannot be null.");
91+
this.nacosConfig = config.get("matata.registry.nacos", NacosConfig.class);
9092
this.namingService = NamingFactory.createNamingService(getNacosProperties());
9193
}
9294

@@ -95,11 +97,11 @@ private Properties getNacosProperties() {
9597
String serverAddr = this.matata.registry().host() + ":" + this.matata.registry().port();
9698
notBlank(serverAddr, "The Nacos server address cannot be blank.");
9799
properties.put("serverAddr", serverAddr);
98-
properties.put("username", ObjectUtils.nullIf(this.matata.registry().proxy().username(), StringUtils.EMPTY));
99-
properties.put("password", ObjectUtils.nullIf(this.matata.registry().proxy().password(), StringUtils.EMPTY));
100+
properties.put("username", ObjectUtils.nullIf(this.nacosConfig.getUsername(), StringUtils.EMPTY));
101+
properties.put("password", ObjectUtils.nullIf(this.nacosConfig.getPassword(), StringUtils.EMPTY));
100102
properties.put("namespace", ObjectUtils.nullIf(this.matata.registry().environment(), StringUtils.EMPTY));
101-
properties.put("accessKey", ObjectUtils.nullIf(this.matata.registry().proxy().accessKey(), StringUtils.EMPTY));
102-
properties.put("secretKey", ObjectUtils.nullIf(this.matata.registry().proxy().secretKey(), StringUtils.EMPTY));
103+
properties.put("accessKey", ObjectUtils.nullIf(this.nacosConfig.getAccessKey(), StringUtils.EMPTY));
104+
properties.put("secretKey", ObjectUtils.nullIf(this.nacosConfig.getSecretKey(), StringUtils.EMPTY));
103105
return properties;
104106
}
105107

@@ -179,11 +181,11 @@ private List<Instance> createInstance(Worker worker, Application application, Fi
179181
*/
180182
private HashMap<String, String> buildInstanceMetadata(Worker worker, Application application, FitableMeta meta) {
181183
HashMap<String, String> metadata = new HashMap<>();
182-
if (this.matata.registry().proxy().heartbeatInterval() != null) {
183-
metadata.put(HEART_BEAT_INTERVAL, String.valueOf(this.matata.registry().proxy().heartbeatInterval()));
184+
if (this.nacosConfig.getHeartbeatInterval() != null) {
185+
metadata.put(HEART_BEAT_INTERVAL, String.valueOf(this.nacosConfig.getHeartbeatInterval()));
184186
}
185-
if (this.matata.registry().proxy().heartbeatTimeout() != null) {
186-
metadata.put(HEART_BEAT_TIMEOUT, String.valueOf(this.matata.registry().proxy().heartbeatTimeout()));
187+
if (this.nacosConfig.getHeartbeatTimeout() != null) {
188+
metadata.put(HEART_BEAT_TIMEOUT, String.valueOf(this.nacosConfig.getHeartbeatTimeout()));
187189
}
188190
try {
189191
metadata.put(WORKER_KEY, this.objectMapper.writeValueAsString(worker));
@@ -201,11 +203,11 @@ private HashMap<String, String> buildInstanceMetadata(Worker worker, Application
201203
* @param instance The service instance object.
202204
*/
203205
private void setInstanceProperties(Instance instance) {
204-
if (!this.matata.registry().proxy().isEphemeral()) {
206+
if (!this.nacosConfig.getIsEphemeral()) {
205207
instance.setEphemeral(false);
206208
}
207-
if (this.matata.registry().proxy().weight() != null) {
208-
instance.setWeight(this.matata.registry().proxy().weight());
209+
if (this.nacosConfig.getWeight() != null) {
210+
instance.setWeight(this.nacosConfig.getWeight());
209211
}
210212
}
211213

framework/fit/java/fit-builtin/plugins/fit-service-coordination-nacos/src/main/resources/application.yml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,10 @@ fit:
22
beans:
33
packages:
44
- 'modelengine.fit.service'
5-
- 'modelengine.fit.heartbeat'
5+
- 'modelengine.fit.heartbeat'
6+
7+
matata:
8+
registry:
9+
nacos:
10+
weight: 1
11+
isEphemeral: true

0 commit comments

Comments
 (0)