好久没写,老规矩,直接上代码。
这里不详细讲解整合过程,相信大家根据官网可以很快的上手。重点说下遇到的坑!
yml文件:
server:
port: 1006
spring:
application:
name: order-server
cloud:
sentinel:
datasource:
ds:
nacos:
username: nacos
password: xxx
server-addr: xxxxx:8848
namespace: xxxx
dataId: ${spring.application.name}-sentinel
groupId: SpringCloudAlibaba
rule-type: flow
data-type: json
transport:
# 端口配置会在应用对应的机器上启动一个 Http Server,该 Server 会与 Sentinel 控制台做交互
port: 8718
dashboard: localhost:10002
nacos:
discovery:
enabled: true
server-addr: xxx:8848
username: nacos
password: xxx
group: SpringCloudAlibaba
namespace: xxx
config:
enabled: true
server-addr: xxx:8848
username: nacos
password: xxx
encode: UTF-8
namespace: xxx
## shared-configs和 extension-configs 是两种不同的方式引入
shared-configs:
- dataId: ping.yml
group: SpringCloudAlibaba
refresh: true
- dataId: jdbc-business.yml
group: SpringCloudAlibaba
refresh: true
file-extension: yml
config-retry-time: 30000
timeout: 30000
feign:
client:
config:
item-server:
# 相当于Request.Options 连接超时时间
connectTimeout: 5000
# 相当于Request.Options 读取超时时间
readTimeout: 100
# 配置Feign的日志级别,相当于代码配置方式中的Logger
loggerLevel: FULL
# Feign的错误解码器,相当于代码配置方式中的ErrorDecoder
errorDecoder: com.rhb.pojo.config.feign.SimpleErrorDecoder
# 配置重试,相当于代码配置方式中的Retryer
retryer: com.rhb.pojo.config.feign.SimpleRetryer
# 配置拦截器,相当于代码配置方式中的RequestInterceptor
requestInterceptors:
- com.rhb.pojo.config.feign.FooRequestInterceptor
- com.rhb.pojo.config.feign.BarRequestInterceptor
# 是否对404错误解码
decode404: false
encode: com.rhb.pojo.config.feign.SimpleEncoder
decoder: com.rhb.pojo.config.feign.SimpleDecoder
# contract: com.rhb.pojo.config.feign.SimpleContract
compression:
request:
# 配置请求GZIP压缩
enabled: true
# 配置压缩支持的MIME TYPE
mime-types: text/xml,application/xml,application/json
# 配置压缩数据大小的下限
min-request-size: 2048
# 配置响应GZIP压缩
response:
enabled: true
启动类配置:
@EnableFeignClients
@EnableDiscoveryClient
@SpringBootApplication
public class OrderApplication {
public static void main(String[] args) {
SpringApplication.run(OrderApplication.class,args);
log.info("Order Server start ... ");
}
}
干货总结:
相关官网:
> https://github.com/alibaba/spring-cloud-alibaba/wiki/Sentinel
> https://github.com/alibaba/Sentinel/wiki/%E6%B3%A8%E8%A7%A3%E6%94%AF%E6%8C%81
1. 客户端启动(控制台)
官网下载相应jar包,添加相应的启动参数,以java -jar的形式启动就行。
2. 异常:Failed to fetch metric from
原因: sentinel想客户端发送心跳失败。(可以使用参数:csp.sentinel.heartbeat.client.ip)
解决: 将sentinel和客户端放置在双方可以ping通的网段就可以(这个地方不用过多担心,正式服务器一般都在
同一局域网内,这样就保证了网络问题。如果出现虚拟机或者虚拟化docker部署,需要指定相应的网卡信息)
3. 启动脚本
nohup java -Dserver.port=10001 -Dcsp.sentinel.dashboard.server=localhost:10002 -Dproject.name=sentinel-dashboard -Dsentinel.dashboard.auth.username=sentinel -Dsentinel.dashboard.auth.password=25infinc1PQAKSZv -jar sentinel-dashboard-1.8.1.jar >nohup.out 2>&1 &
4. nacos持久化规则
> 为什么持久化?
防止sentinel的server端重启后丢失规则配置。
> 相关参数详解?
resource:资源名,即限流规则的作用对象(一般指url路径)
limitApp:流控针对的调用来源,若为 default 则不区分调用来源
grade:限流阈值类型(QPS 或并发线程数);
- 0代表根据并发数量来限流,
- 1代表根据QPS来进行流量控制
count:限流阈值
strategy:调用关系限流策略
controlBehavior:流量控制效果
- 0-2(直接拒绝、Warm Up、匀速排队)
clusterMode:是否为集群模式(boolean类型)
> 接入的时候,nacos配置不生效(没有自动注入流控规则)
a. 查看官网: https://github.com/alibaba/spring-cloud-alibaba/issues/759
b. 查看源码:
AbstractDataSource.loadConfig()->SentinelConverter.convert()
检测到AbstractDataSource.property参数(及加载的yml文件参数内容)缺少namespace参数。
c. 尝试添加namespace参数,试运行成功。
##使用版本:
- sentinel 1.8.1
- jdk 1.8.0_152
- nacos 2.0.2
$$ 不要骂我蠢,官网就是这样没有namespace的,或者说没有明确必须有的。