- spring-cloud Hoxton.SR8
- spring-boot 2.3.3.RELEASE
- 注册中心 nacos、eureka
- 网关 spring gateway
- 断路器 Sentinel、hystrix、Resilience4J
- 远程调用 openfeign
- 未完待续...
demo项目
- nacos-service-provider (提供服务项目)
- nacos-service-consumer (调用服务项目)
- gateway (用到sentinel+nacos) nacos官方文档
-
启动nacos服务,参考nacos官网
docker安装
网址:http://ip:8848/nacos/ 账号:nacos 密码:nacos -
客户端项目引入依赖
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
注意:父项目引入
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring.cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>${spring.cloud.alibaba.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
- application.properties或者application.yml 配置注册到nacos,更多配置信息
server.port=8081
spring.application.name=nacos-provider
spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848
management.endpoints.web.exposure.include=*
demo项目
- gateway (网关项目,使用sentinel配合nacos扩展数据源配置限流规则)
java -Dserver.port=8080 -Dcsp.sentinel.dashboard.server=localhost:8080 -Dproject.name=sentinel-dashboard -jar xxx.jar
- 访问地址和账号密码 http://localhost:8080 账号:sentinel 密码:sentinel
- 配置yml
spring:
cloud:
sentinel:
transport:
port: 8719
dashboard: localhost:8080 #启动本项目后需要请求一次才能向sentinel控制台注册
- 文档
- 引入依赖
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-sentinel-gateway</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>
实现效果:nacos发布配置后限流效果自动生效
demo可查看gateway项目application-sentinel.yml配置文件:
spring:
cloud:
sentinel:
datasource:
ds2:
nacos:
server-addr: 192.168.67.129:8848
data-id: sentinel
group-id: Sentinel:Demo
data-type: json
rule-type: flow
- 项目(openfeign项目请求userService和orderService项目)
- 文档