44
55FIT Service Coordination Nacos 插件是 FIT Framework 的服务协调插件,提供基于 Nacos 的服务注册与发现功能。该插件将 FIT 框架与 Alibaba Nacos 注册中心集成。
66
7+ ## 重要说明
8+
9+ 由于 ` fit-service-coordination-nacos ` 与 ` fit-service-coordination-simple ` 两种注册中心插件存在冲突,我们选择默认使用内存版注册中心。
10+
11+ ### 不同启动方式的插件使用
12+
13+ - ** IDEA 启动方式和 ` java -jar ` 启动方式** :只需要按需引入依赖即可,无需额外配置。
14+
15+ - ** ` fit start ` 命令启动方式** :
16+ - 默认只有 ` fit-service-coordination-simple ` 插件在 ` /build/plugins ` 目录
17+ - 如果需要使用 Nacos 版注册中心,操作步骤:
18+ 1 . ** 整体编译项目** :
19+ ``` bash
20+ cd framework/fit/java
21+ mvn clean package
22+ ```
23+ 2. ** 放入 build/plugins 目录** :
24+ ` ` ` bash
25+ cp fit-builtin/plugins/fit-service-coordination-nacos/target/fit-service-coordination-nacos-3.6.0-SNAPSHOT.jar ../../../build/plugins/
26+ ` ` `
27+ 3. ** 移除 Simple 插件** :
28+ ` ` ` bash
29+ rm ../../../build/plugins/fit-service-coordination-simple-3.6.0-SNAPSHOT.jar
30+ ` ` `
31+
32+ > ** 说明** :` build/plugins` 目录中只能有一个注册中心插件,Nacos 和 Simple 不能同时存在。
33+
734# # 快速开始
835
936# ## 1. Nacos 部署
@@ -31,7 +58,13 @@ sh startup.sh -m standalone
3158startup.cmd -m standalone
3259` ` `
3360
34- 访问控制台:http://localhost:8848/nacos (默认用户名/密码:nacos/nacos)
61+ 访问控制台:http://localhost:8848/nacos
62+
63+ ** 默认登录凭据** :
64+ - 用户名:` nacos`
65+ - 密码:` nacos`
66+
67+ > ⚠️ ** 安全提醒** :默认凭据仅适用于开发环境。在生产环境中,请务必修改默认的用户名和密码以确保系统安全。
3568
3669# ## 2. 插件配置
3770
@@ -51,7 +84,7 @@ startup.cmd -m standalone
5184在 ` application.yml` 中配置 Nacos 连接信息:
5285
5386` ` ` yaml
54- # Matata 服务配置
87+ # 基础配置示例
5588matata:
5689 registry:
5790 # Nacos 服务器地址
@@ -61,6 +94,22 @@ matata:
6194 mode: ' PROXY'
6295 # 环境命名空间
6396 environment: local
97+
98+ # 完整配置示例(可选)
99+ matata:
100+ registry:
101+ host: ' localhost'
102+ port: 8848
103+ mode: ' PROXY'
104+ environment: ' local'
105+ # Nacos 特定配置
106+ nacos:
107+ username: ' nacos' # Nacos 用户名(可选)
108+ password: 'nacos' # Nacos 密码(可选)
109+ weight: 1.0 # 服务权重
110+ isEphemeral: true # 是否为临时实例
111+ heartbeatInterval: 5000 # 心跳间隔(毫秒)
112+ heartbeatTimeout: 15000 # 心跳超时(毫秒)
64113` ` `
65114
66115# # 配置参数说明
@@ -85,4 +134,163 @@ matata:
85134| ` matata.registry.nacos.weight` | Float | 否 | 1.0 | 服务权重 |
86135| ` matata.registry.nacos.isEphemeral` | Boolean | 否 | true | 是否为临时实例 |
87136| ` matata.registry.nacos.heartbeatInterval` | Long | 否 | 5000 | 心跳间隔(毫秒) |
88- | `matata.registry.nacos.heartbeatTimeout` | Long | 否 | 15000 | 心跳超时(毫秒) |
137+ | ` matata.registry.nacos.heartbeatTimeout` | Long | 否 | 15000 | 心跳超时(毫秒) |
138+
139+ # # 使用示例
140+
141+ # ## 完整示例项目
142+
143+ FIT Framework 提供了一个完整的 Nacos 服务注册与发现示例项目,位于 ` examples/fit-example/08-nacos-complicated-apps` 目录。
144+
145+ # ### 项目结构
146+
147+ ```
148+ 08-nacos-complicated-apps/
149+ ├── app-assistant/ # 客户端应用(助手服务)
150+ ├── app-default-weather/ # 服务端应用(天气服务)
151+ ├── service/ # 共享服务接口
152+ └── pom.xml # 父项目配置
153+ ```
154+
155+ #### 示例说明
156+
157+ 该示例演示了如何使用 Nacos 作为注册中心实现微服务之间的服务发现和调用:
158+
159+ 1. **服务提供者**(`app-default-weather`):
160+ - 实现 `Weather` 接口
161+ - 注册到 Nacos 注册中心
162+ - 提供天气查询服务
163+
164+ 2. **服务消费者**(`app-assistant`):
165+ - 通过 Nacos 发现天气服务
166+ - 调用远程天气服务
167+ - 提供 HTTP API 接口
168+
169+ #### 关键配置
170+
171+ **服务提供者配置**(`app-default-weather/src/main/resources/application.yml`):
172+ ```yaml
173+ application:
174+ name: 'default-weather'
175+
176+ worker:
177+ id: 'default-weather'
178+ host: '127.0.0.1'
179+ environment: 'local'
180+
181+ matata:
182+ registry:
183+ mode: 'PROXY'
184+ host: '127.0.0.1'
185+ port: 8848
186+ environment: 'local'
187+
188+ server:
189+ http:
190+ port: 8081
191+ ```
192+
193+ ** 服务消费者配置** (` app-assistant/src/main/resources/application.yml ` ):
194+ ``` yaml
195+ application :
196+ name : ' assistant'
197+
198+ worker :
199+ id : ' assistant'
200+ host : ' 127.0.0.1'
201+ environment : ' local'
202+
203+ matata :
204+ registry :
205+ mode : ' PROXY'
206+ host : ' 127.0.0.1'
207+ port : 8848
208+ environment : ' local'
209+
210+ server :
211+ http :
212+ port : 8080
213+ ` ` `
214+
215+ #### 核心代码
216+
217+ **服务接口定义**(` service/src/main/java/modelengine/fit/example/Weather.java`):
218+ ` ` ` java
219+ public interface Weather {
220+ @Genericable(id = "Weather")
221+ String get();
222+ }
223+ ` ` `
224+
225+ **服务实现**(`app-default-weather/src/main/java/modelengine/fit/example/DefaultWeather.java`):
226+ ` ` ` java
227+ @Component
228+ public class DefaultWeather implements Weather {
229+ @Override
230+ @Fitable(id = "default-weather")
231+ public String get() {
232+ return "Default weather application is working.";
233+ }
234+ }
235+ ` ` `
236+
237+ **服务调用**(`app-assistant/src/main/java/modelengine/fit/example/controller/AssistantController.java`):
238+ ` ` ` java
239+ @Component
240+ public class AssistantController {
241+ private final Weather weather;
242+
243+ public AssistantController(@Fit Weather weather) {
244+ this.weather = weather;
245+ }
246+
247+ @GetMapping(path = "/weather")
248+ public String getWeather() {
249+ return this.weather.get();
250+ }
251+ }
252+ ` ` `
253+
254+ # ### 运行示例
255+
256+ 1. **启动 Nacos 服务器**(确保在 8848 端口运行)
257+
258+ 2. **启动服务提供者**:
259+ ` ` ` bash
260+ cd examples/fit-example/08-nacos-complicated-apps/app-default-weather
261+ mvn clean package
262+ java -jar target/app-default-weather-1.0-SNAPSHOT.jar
263+ ` ` `
264+
265+ 3. **启动服务消费者**:
266+ ` ` ` bash
267+ cd examples/fit-example/08-nacos-complicated-apps/app-assistant
268+ mvn clean package
269+ java -jar target/app-assistant-1.0-SNAPSHOT.jar
270+ ` ` `
271+
272+ 4. **测试服务调用**:
273+ ` ` ` bash
274+ curl http://localhost:8080/weather
275+ ` ` `
276+
277+ # ### 依赖配置
278+
279+ 在 `pom.xml` 中添加必要的依赖:
280+
281+ ` ` ` xml
282+ <!-- FIT 核心依赖 -->
283+ <dependency>
284+ <groupId>org.fitframework</groupId>
285+ <artifactId>fit-starter</artifactId>
286+ <version>3.6.0-SNAPSHOT</version>
287+ </dependency>
288+
289+ <!-- Nacos 注册中心插件 -->
290+ <dependency>
291+ <groupId>org.fitframework.plugin</groupId>
292+ <artifactId>fit-service-coordination-nacos</artifactId>
293+ <version>3.6.0-SNAPSHOT</version>
294+ <scope>runtime</scope>
295+ </dependency>
296+ ` ` `
0 commit comments