Spring Cloud Alibaba 学习笔记:Nacos、OpenFeign 和 Sentinel

写在前面

微服务是一个很大的话题。站在我现在的阶段,更适合先理解组件分别解决什么问题,而不是一上来写复杂架构。

这篇文章记录我对 Nacos、OpenFeign 和 Sentinel 的基础理解。

Nacos

Nacos 常见作用是服务注册发现和配置管理。

1
2
3
4
5
6
7
spring:
application:
name: user-service
cloud:
nacos:
discovery:
server-addr: localhost:8848

我的理解是:服务注册发现主要解决服务地址不固定的问题;配置中心主要解决配置散落、修改不方便的问题。

OpenFeign

OpenFeign 可以让远程 HTTP 调用写起来像接口调用。

1
2
3
4
5
@FeignClient(name = "user-service")
public interface UserClient {
@GetMapping("/users/{id}")
UserDTO getUserById(@PathVariable("id") Long id);
}

但远程调用毕竟不是本地方法,所以还要考虑超时、失败和兜底。

Sentinel

Sentinel 主要用于限流、降级和系统保护。

1
2
3
4
@SentinelResource(value = "queryUser", blockHandler = "queryUserBlock")
public UserDTO queryUser(Long userId) {
return userClient.getUserById(userId);
}

我目前理解它的重点是:当流量异常或依赖服务异常时,尽量保护系统不被拖垮。

小结

这些组件适合放在学习笔记里,而不是包装成大型项目经验。对我来说,先讲清楚它们解决什么问题,比堆组件名更重要。


Spring Cloud Alibaba 学习笔记:Nacos、OpenFeign 和 Sentinel
https://zxyblog.top/2024/09/25/Spring-Cloud-Alibaba学习笔记-Nacos-OpenFeign-Sentinel/
作者
zxy
发布于
2024年9月25日
许可协议