Sentinel簡介


Sentinel是阿里開源的項目,提供了流量控制,熔斷降級,系統負載包含等多個維度來保障服務之間的穩定性。

Sentinel主要特性:

Sentinel與Hystrix的區別:

關於Sentinel與Hystrix的區別詳細:https://developer.aliyun.com/article/633786

總結:

 Hystrix常用的線程池隔離會造成線程上下文切換的overhead比較大;Hystrix使用的信號量對某個資源調用的並發數進行控制,效果不錯,但是無法對滿調用進行自動降級;Sentinel通過並發線程數的流量控制提供信號量隔離的功能。此外Sentinel支持熔斷降級維度更多,可對多種指標進行流控、熔斷,且提供了實時監控和控制面板,功能更為強大。

Sentinel分為兩個部分:

  • 核心庫(java客戶端)不依賴任何框架/庫,能夠運行於所有java運行時環境,同時對Dubbo/Spring Cloud等框架也有較好的支持。
  • 控制台(Dashboard)基於Spring Boot開發,打包后可以直接運行,不需要額外的Tomcat等應用容器。

1,下載sentinel Dashboard,https://github.com/alibaba/sentinel/releases

2,cmd 運行java -jar sentinel-dashboard-1.7.1.jar。

3,訪問http://localhost:8080,賬戶密碼都是sentinel。

 簡單使用

1,新建項目

2,POM文件

    <dependencies>
        <!--nacos客戶端 服務注冊-->
        <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> </dependency>
        <!--sentinel 流量監控,服務熔斷,服務降級等-->
        <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId> </dependency>
        <!--持久化-->
        <dependency> <groupId>com.alibaba.csp</groupId> <artifactId>sentinel-datasource-nacos</artifactId> </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!--web 監控-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
        </dependency>
    </dependencies>

3,YML文件

server:
  port: 6001
spring:
  application:
    name: sentienel-dashboard-service
 cloud: nacos: discovery: #nacos服務注冊中心地址 server-addr: localhost:8848 sentinel: transport: #sentinel dashboard 地址 dashboard: localhost:8080 #默認8179端口 port: 8179

#監控暴露 啟動服務訪問http://localhost:port/actuator/sentinel會返回json信息,說明已經整合好了Sentinel
management:
  endpoints:
    web:
      exposure:
        include: "*"

4,主啟動類

@SpringBootApplication
@EnableDiscoveryClient public class SentinelDashboard6001Main {
    public static void main(String[] args) {
        SpringApplication.run(SentinelDashboard6001Main.class,args);
    }
}

5,業務類

@RestController
public class SentinelController {
    @GetMapping("/testA")
    public String getA(){
        return "test A";
    }

    @GetMapping("/testB")
    public String getB(){
        return "test B";
    }
}

訪問http://localhost:6001/testB

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM