Skip to content

Commit

Permalink
feat(high-performance-topic): 实现 Nacos API 手动注册聊天服务
Browse files Browse the repository at this point in the history
- 初始化 Nacos Client
- 注册聊天服务到 Nacos
- 使用 Nacos 服务发现功能获取健康实例
- 在网关中动态获取聊天服务实例
- 更新待办事项列表,完成 Openfeign 替换 HTTP 的任务
  • Loading branch information
bkhech committed Jan 17, 2025
1 parent 69f8dab commit 8b6343a
Showing 1 changed file with 67 additions and 1 deletion.
68 changes: 67 additions & 1 deletion high-performance-topic/based-on-netty-cluster-chat.md
Original file line number Diff line number Diff line change
Expand Up @@ -826,7 +826,7 @@ docker network create --driver bridge --subnet 172.88.0.0/16 --gateway 172.88.0.

## 待办事项

- [ ] `Nacos api` 手动注册聊天服务,然后使用 openfeign 替换聊天服务中的 http
- [x] 使用 `Nacos api` 手动注册聊天服务,然后使用 `Openfeign` 替换聊天服务中的 http

- [ ] 各服务之间限流功能

Expand All @@ -849,6 +849,72 @@ docker network create --driver bridge --subnet 172.88.0.0/16 --gateway 172.88.0.

- [ ] 阿里云语音转文字 `api` 使用实践

### `Nacos api` 手动注册聊天服务

1. 初始化 `Nacos Client`

```java
/**
* NacosClient
*@author guowm
*@date 2024/12/18
*/
@Slf4j
public class NacosClient {

private static volatile NamingService namingService;

private static NamingService initNamingService() {
final Props properties = PropertiesUtil.getInstance();

properties.setProperty(PropertyKeyConst.NAMESPACE, properties.getProperty("nacos.namespace"));
properties.setProperty(PropertyKeyConst.SERVER_ADDR, properties.getProperty("nacos.serverAddr"));
properties.setProperty(PropertyKeyConst.USERNAME, properties.getProperty("nacos.username"));
properties.setProperty(PropertyKeyConst.PASSWORD, properties.getProperty("nacos.password"));

try {
// 初始化配置中心的 Nacos Java SDK
namingService = NacosFactory.createNamingService(properties);
} catch (Exception e) {
log.error("初始化 NamingService 失败:{},{}", e, e.getMessage());
throw new RuntimeException(e);
}
return namingService;

}

public static NamingService getNamingService() {
if (namingService == null) {
synchronized (NacosClient.class) {
if (namingService == null) {
namingService = initNamingService();
}
}
}
return namingService;
}
}
```

2. 注册聊天服务

```java
// 把 chat-server 注册到 nacos
NacosClient.getNamingService().registerInstance("chat-server", ZKUtil.getLocalIp(), nettPort);
```

3. 使用服务

```java
final Instance instance = NacosClient.getNamingService().selectOneHealthyInstance("main-service");
String isBlackUrl = String.format(properties.getProperty("chat.friend.isBlack.url"), instance.getIp(), instance.getPort());
String url = StrUtil.format("{}?friendId1st={}&friendId2nd={}", isBlackUrl, chatMsg.getSenderId(), chatMsg.getReceiverId());
```

4. 网关中获取动态聊天服务实例

![image-20250117120220050](https://raw.githubusercontent.com/guowenmeng/wodkshje/main/wm2025/01/17/20250117120222.png)

### 池化技术实现原理

> `org.apache.commons.pool2.impl.GenericObjectPoolConfig`
Expand Down

0 comments on commit 8b6343a

Please sign in to comment.