# note-spring-cloud **Repository Path**: xiaobowen-hz/note-spring-cloud ## Basic Information - **Project Name**: note-spring-cloud - **Description**: 一个关于学习和实践 Spring Cloud 组件及其生态的项目。 - **Primary Language**: Java - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2025-03-19 - **Last Updated**: 2025-04-23 ## Categories & Tags **Categories**: Uncategorized **Tags**: nacos, sentinel, openfeign, Gateway ## README # note:spring-cloud-alibaba #### 介绍 `spring-cloud-alibaba`组件笔记 #### 环境准备 一、Nacos(注册中心) 二、Sentinel 控制台(可选),下载请访问[这里](https://github.com/alibaba/Sentinel/releases) #### 使用说明 1. 配置文件`application.yaml`中的参数说明 - Nacos配置,用来注册和发现服务,**必须更改为真实配置** ```yaml spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848 spring.cloud.nacos.discovery.namespace=8c1d3470-bf46-4bd6-88d4-1c569934f1bc ``` - Sentinel 控制台,用来管理微服务流量,**必须更改为真实配置**;如果仅需要通过 Nacos 去配置限流规则,可以注释该配置 ```yaml spring.cloud.sentinel.transport.dashboard=localhost:8333 spring.cloud.sentinel.transport.port=8719 ``` - Sentinel 数据源,用于动态管理规则,从外部存储(如文件、数据库、配置中心等)加载规则,**必须更改为真实配置**。本服务默认使用 Nacos ```yaml spring.cloud.sentinel.datasource.* ``` - 服务端的 Sentinel 配置,用来启用服务降级功能。需要启用服务降级时无需更改 ```yaml feign.sentinel.enabled=true ``` - 网关路由配置,给网关配置服务的匹配规则。如果服务的名称等未修改过,则无需关注此配置,默认即可 ```yaml spring.cloud.gateway.routes.* ``` 2. Nacos 添加`Data Id`为`cloud-note-gateway-flow-rules`的配置文件,内容如下: ```json [ { "resource": "cloud-note-business", "limitApp": "default", "grade": 1, "count": 1, "strategy": 0, "controlBehavior": 0, "clusterMode": false } ] ``` 3. 启动服务:`BusinessApplication` `SystemApplication` `GatewayApplication` 4. 如果需要测试自定义的负载均衡器(随机策略已实现,不再是轮询效果表示已生效),可以额外启动服务:`BusinessBackApplication` 5. 启动`Sentinel`的控制台(可选) ```bash java -jar sentinel-dashboard-1.8.8.jar --server.port=8333 ``` - 账号:sentinel - 密码:sentinel 6. 测试API: - 远程接口:`http://localhost:8082/user/getWork` - 业务接口:`http://localhost:8083/work/name` - 系统接口:`http://localhost:8082/user/name` - 网关接口:`http://localhost:8081/business/work/name` - 流控控制台:`http://localhost:8333/#/login` #### 注意事项 1. 对于`META-INF\spring\org.springframework.boot.autoconfigure.AutoConfiguration.imports`。如果你的类已经标记了`@Component`则无需理会,否则需要将类的全限类名添加到该文件,如: ``` com.note.factory.RemoteWorkFallbackFactory ``` #### 目录结构 ``` ├── note-common - 公共模块 ├── note-domain-center - 实体中心 ├── note-gateway - 网关模块 │ ├── .../handler - 处理器 │ └── .../config - 配置类 ├── note-business - 业务模块 │ └── .../ - *** ├── note-business-back - 业务备用模块(测试负载均衡) │ └── .../ - *** ├── note-system - 系统模块 │ └── .../ - *** ├── note-remote-api - 内部远程接口 │ ├── .../config - 配置类 │ ├── .../factory - 回退工厂类 │ └── .../META-INF - 配置类声明 ```