Skip to content

Commit e386bed

Browse files
committed
[fit] java 版的默认使用内存版注册中心
1 parent 0ce4405 commit e386bed

File tree

3 files changed

+218
-3
lines changed
  • framework/fit/java/fit-builtin/plugins

3 files changed

+218
-3
lines changed

framework/fit/java/fit-builtin/plugins/fit-service-coordination-nacos/README.md

Lines changed: 212 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,34 @@
44

55
FIT 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. **取消 `fit-service-coordination-nacos` 插件中 `pom.xml` 文件的注释**
19+
- 文件位置:`framework/fit/java/fit-builtin/plugins/fit-service-coordination-nacos/pom.xml`
20+
- 找到 `<build>` 标签下的 `maven-antrun-plugin` 配置块
21+
- 删除 `<!--``-->` 注释标记,使插件配置生效
22+
2. **同时注释掉 `fit-service-coordination-simple` 插件中 `pom.xml` 文件的对应注释**
23+
- 文件位置:`framework/fit/java/fit-builtin/plugins/fit-service-coordination-simple/pom.xml`
24+
- 找到 `<build>` 标签下的 `maven-antrun-plugin` 配置块
25+
- 在配置块前后添加 `<!--``-->` 注释标记,使插件配置失效
26+
3. 这样会让 `fit-service-coordination-nacos` 的插件 jar 包打包到 `/build/plugins` 目录
27+
28+
#### 具体操作示例
29+
30+
**需要修改的配置块**:找到 `maven-antrun-plugin` 插件配置,位于 `<build><plugins>` 标签内。
31+
32+
- **启用 Nacos 插件**:删除 `fit-service-coordination-nacos/pom.xml` 中的 `<!--``-->`
33+
- **禁用 Simple 插件**:在 `fit-service-coordination-simple/pom.xml` 中添加 `<!--``-->`
34+
735
## 快速开始
836

937
### 1. Nacos 部署
@@ -31,7 +59,13 @@ sh startup.sh -m standalone
3159
startup.cmd -m standalone
3260
```
3361

34-
访问控制台:http://localhost:8848/nacos (默认用户名/密码:nacos/nacos)
62+
访问控制台:http://localhost:8848/nacos
63+
64+
**默认登录凭据**
65+
- 用户名:`nacos`
66+
- 密码:`nacos`
67+
68+
> ⚠️ **安全提醒**:默认凭据仅适用于开发环境。在生产环境中,请务必修改默认的用户名和密码以确保系统安全。
3569
3670
### 2. 插件配置
3771

@@ -51,7 +85,7 @@ startup.cmd -m standalone
5185
`application.yml` 中配置 Nacos 连接信息:
5286

5387
```yaml
54-
# Matata 服务配置
88+
# 基础配置示例
5589
matata:
5690
registry:
5791
# Nacos 服务器地址
@@ -61,6 +95,22 @@ matata:
6195
mode: 'PROXY'
6296
# 环境命名空间
6397
environment: local
98+
99+
# 完整配置示例(可选)
100+
matata:
101+
registry:
102+
host: 'localhost'
103+
port: 8848
104+
mode: 'PROXY'
105+
environment: 'local'
106+
# Nacos 特定配置
107+
nacos:
108+
username: 'nacos' # Nacos 用户名(可选)
109+
password: 'nacos' # Nacos 密码(可选)
110+
weight: 1.0 # 服务权重
111+
isEphemeral: true # 是否为临时实例
112+
heartbeatInterval: 5000 # 心跳间隔(毫秒)
113+
heartbeatTimeout: 15000 # 心跳超时(毫秒)
64114
```
65115
66116
## 配置参数说明
@@ -85,4 +135,163 @@ matata:
85135
| `matata.registry.nacos.weight` | Float | 否 | 1.0 | 服务权重 |
86136
| `matata.registry.nacos.isEphemeral` | Boolean | 否 | true | 是否为临时实例 |
87137
| `matata.registry.nacos.heartbeatInterval` | Long | 否 | 5000 | 心跳间隔(毫秒) |
88-
| `matata.registry.nacos.heartbeatTimeout` | Long | 否 | 15000 | 心跳超时(毫秒) |
138+
| `matata.registry.nacos.heartbeatTimeout` | Long | 否 | 15000 | 心跳超时(毫秒) |
139+
140+
## 使用示例
141+
142+
### 完整示例项目
143+
144+
FIT Framework 提供了一个完整的 Nacos 服务注册与发现示例项目,位于 `examples/fit-example/08-nacos-complicated-apps` 目录。
145+
146+
#### 项目结构
147+
148+
```
149+
08-nacos-complicated-apps/
150+
├── app-assistant/ # 客户端应用(助手服务)
151+
├── app-default-weather/ # 服务端应用(天气服务)
152+
├── service/ # 共享服务接口
153+
└── pom.xml # 父项目配置
154+
```
155+
156+
#### 示例说明
157+
158+
该示例演示了如何使用 Nacos 作为注册中心实现微服务之间的服务发现和调用:
159+
160+
1. **服务提供者**(`app-default-weather`):
161+
- 实现 `Weather` 接口
162+
- 注册到 Nacos 注册中心
163+
- 提供天气查询服务
164+
165+
2. **服务消费者**(`app-assistant`):
166+
- 通过 Nacos 发现天气服务
167+
- 调用远程天气服务
168+
- 提供 HTTP API 接口
169+
170+
#### 关键配置
171+
172+
**服务提供者配置**(`app-default-weather/src/main/resources/application.yml`):
173+
```yaml
174+
application:
175+
name: 'default-weather'
176+
177+
worker:
178+
id: 'default-weather'
179+
host: '127.0.0.1'
180+
environment: 'local'
181+
182+
matata:
183+
registry:
184+
mode: 'PROXY'
185+
host: '127.0.0.1'
186+
port: 8848
187+
environment: 'local'
188+
189+
server:
190+
http:
191+
port: 8081
192+
```
193+
194+
**服务消费者配置**`app-assistant/src/main/resources/application.yml`):
195+
```yaml
196+
application:
197+
name: 'assistant'
198+
199+
worker:
200+
id: 'assistant'
201+
host: '127.0.0.1'
202+
environment: 'local'
203+
204+
matata:
205+
registry:
206+
mode: 'PROXY'
207+
host: '127.0.0.1'
208+
port: 8848
209+
environment: 'local'
210+
211+
server:
212+
http:
213+
port: 8080
214+
```
215+
216+
#### 核心代码
217+
218+
**服务接口定义**(`service/src/main/java/modelengine/fit/example/Weather.java`):
219+
```java
220+
public interface Weather {
221+
@Genericable(id = "Weather")
222+
String get();
223+
}
224+
```
225+
226+
**服务实现**(`app-default-weather/src/main/java/modelengine/fit/example/DefaultWeather.java`):
227+
```java
228+
@Component
229+
public class DefaultWeather implements Weather {
230+
@Override
231+
@Fitable(id = "default-weather")
232+
public String get() {
233+
return "Default weather application is working.";
234+
}
235+
}
236+
```
237+
238+
**服务调用**(`app-assistant/src/main/java/modelengine/fit/example/controller/AssistantController.java`):
239+
```java
240+
@Component
241+
public class AssistantController {
242+
private final Weather weather;
243+
244+
public AssistantController(@Fit Weather weather) {
245+
this.weather = weather;
246+
}
247+
248+
@GetMapping(path = "/weather")
249+
public String getWeather() {
250+
return this.weather.get();
251+
}
252+
}
253+
```
254+
255+
#### 运行示例
256+
257+
1. **启动 Nacos 服务器**(确保在 8848 端口运行)
258+
259+
2. **启动服务提供者**:
260+
```bash
261+
cd examples/fit-example/08-nacos-complicated-apps/app-default-weather
262+
mvn clean package
263+
java -jar target/app-default-weather-1.0-SNAPSHOT.jar
264+
```
265+
266+
3. **启动服务消费者**:
267+
```bash
268+
cd examples/fit-example/08-nacos-complicated-apps/app-assistant
269+
mvn clean package
270+
java -jar target/app-assistant-1.0-SNAPSHOT.jar
271+
```
272+
273+
4. **测试服务调用**:
274+
```bash
275+
curl http://localhost:8080/weather
276+
```
277+
278+
#### 依赖配置
279+
280+
在 `pom.xml` 中添加必要的依赖:
281+
282+
```xml
283+
<!-- FIT 核心依赖 -->
284+
<dependency>
285+
<groupId>org.fitframework</groupId>
286+
<artifactId>fit-starter</artifactId>
287+
<version>3.6.0-SNAPSHOT</version>
288+
</dependency>
289+
290+
<!-- Nacos 注册中心插件 -->
291+
<dependency>
292+
<groupId>org.fitframework.plugin</groupId>
293+
<artifactId>fit-service-coordination-nacos</artifactId>
294+
<version>3.6.0-SNAPSHOT</version>
295+
<scope>runtime</scope>
296+
</dependency>
297+
```

framework/fit/java/fit-builtin/plugins/fit-service-coordination-nacos/pom.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@
7272

7373
<build>
7474
<plugins>
75+
<!-- 注释掉 maven-antrun-plugin,不再复制 jar 包到 build/plugins 目录 -->
76+
<!--
7577
<plugin>
7678
<groupId>org.apache.maven.plugins</groupId>
7779
<artifactId>maven-antrun-plugin</artifactId>
@@ -90,6 +92,7 @@
9092
</execution>
9193
</executions>
9294
</plugin>
95+
-->
9396
</plugins>
9497
</build>
9598
</project>

framework/fit/java/fit-builtin/plugins/fit-service-coordination-simple/pom.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@
6363

6464
<build>
6565
<plugins>
66+
<!-- 注释掉 maven-antrun-plugin,不再复制 jar 包到 build/plugins 目录 -->
67+
<!--
6668
<plugin>
6769
<groupId>org.apache.maven.plugins</groupId>
6870
<artifactId>maven-antrun-plugin</artifactId>
@@ -81,6 +83,7 @@
8183
</execution>
8284
</executions>
8385
</plugin>
86+
-->
8487
</plugins>
8588
</build>
8689
</project>

0 commit comments

Comments
 (0)