spring cloud alibaba之sentinel

好久没写,老规矩,直接上代码。

这里不详细讲解整合过程,相信大家根据官网可以很快的上手。重点说下遇到的坑!

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的,或者说没有明确必须有的。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring CloudSpring Cloud Alibaba都是基于Spring Framework的开源框架,用于构建分布式系统和微服务架构。它们都提供了一系列的组件和工具来简化微服务开发和管理。 Spring Cloud是一个由Pivotal团队维护的项目,它集成了Netflix开源的一些组件,如Eureka作为注册中心、Ribbon作为负载均衡器、Feign作为服务调用工具等。同时,Spring Cloud还提供了其他功能,如Config Server用于动态管理配置、Gateway用于构建API网关等。 而Spring Cloud Alibaba则是阿里巴巴开源的项目,它在Spring Cloud的基础上进行了扩展和定制,增加了一些阿里巴巴自己的组件和工具。比如,它使用Nacos作为注册中心和配置中心,使用Sentinel作为熔断降级工具。 总的来说,Spring CloudSpring Cloud Alibaba都是用于构建微服务架构的框架,它们的区别在于Spring Cloud集成了Netflix组件,而Spring Cloud Alibaba集成了阿里巴巴的一些组件。根据具体的需求和技术栈选择使用哪个框架可以更好地满足开发和管理微服务的需求。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [Spring Cloud AlibabaSpring Cloud的区别](https://blog.csdn.net/weixin_43888891/article/details/126653270)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值