SpringCloud Alibaba之Sentinel案例初始化演示

本文详细介绍了如何使用Sentinel实现微服务限流,包括搭建Sentinel控制台、配置Nacos作为服务注册中心,以及在Spring Cloud Alibaba框架下集成Sentinel进行流量控制的过程。

所有代码都在github上:https://github.com/demonruin/cloud2020/tree/master

 

首先需要准备的为:nacos8848控制中心这里单点演示、sentinel-dashboard控制台8080、新建sentinel-service8401微服务

因为nacos8848是docker直接启动、sentinel-dashboard是jar包直接启动,下面只剩下新建一个sentinel-service8401了

1、新建modul为 cloudalibaba-sentinel-service8401,并在pom中添加相关依赖包,以后用alibaba这一套nacos和sentinel基本不分家

        <!--SpringCloud ailibaba nacos -->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>
        <!--SpringCloud ailibaba sentinel-datasource-nacos 后续做持久化用到-->
        <dependency>
            <groupId>com.alibaba.csp</groupId>
            <artifactId>sentinel-datasource-nacos</artifactId>
        </dependency>
        <!--SpringCloud ailibaba sentinel -->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
        </dependency>
        <!--openfeign-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>
        <!-- SpringBoot整合Web组件+actuator -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <!--日常通用jar包配置-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>

2、构建application.yml文件

server:
  port: 8401

spring:
  application:
    name: cloudalibaba-sentinel-service
  cloud:
    nacos:
      discovery:
        server-addr: localhost:8848 #Nacos服务注册中心地址
    sentinel:
      transport:
        dashboard: localhost:8080 #配置Sentinel dashboard地址
        #默认8719端口,假如被占用会自动从8719开始依次+1扫描,直至找到未被占用的端口
        port: 8719

#暴露监控端点
management:
  endpoints:
    web:
      exposure:
        include: '*'


3、构建主启动类,添加服务发现注解

/**
 * created by king on 2020/5/26 3:46 下午
 */
@SpringBootApplication
@EnableDiscoveryClient
public class SentinelServiceMain8401 {
    public static void main(String[] args) {
            SpringApplication.run(SentinelServiceMain8401.class,args);
        }
}

4、构建FlowLimitController业务类进行测试

@RestController
@Slf4j
public class FlowLimitController {

    @GetMapping("/testA")
    public String testA()
    {
        return "------testA";
    }

    @GetMapping("/testB")
    public String testB()
    {
        log.info(Thread.currentThread().getName()+"\t"+"...testB");
        return "------testB";
    }

}

5、启动nacos8848、sentinel-dashboard8080、cloudalibaba-sentinel-service8401微服务

6、查看是否服务注册成功,sentinel监控是否成功

1、服务注册nacos成功

2、因为Sentinel采用的懒加载说明,所以此时查看sentinel-dashboard里面还是什么都没有,需要进行请求,才会产生数据,现在我们请求testA和testB接口,执行一次访问即可,下面我们来查看结果

此时说明:sentinel8080正在监控微服务8401

简单的初始化案例演示完成

 

<think>我们被要求使用中文回答关于Spring Cloud和Spring Cloud Alibaba的选择问题,并在最后提出3-5个相关问题。 首先,我们需要分析两者的区别和应用场景,然后给出选择建议。 回答结构: 1. 简要介绍两者 2. 对比核心差异(如功能、生态、社区支持等) 3. 给出选择建议(根据具体场景) 4. 最后输出“§§相关问题§§”以及3-5个相关问题 注意:在回答中不能出现换行分散,且在最后必须完整输出“§§相关问题§§”并紧跟问题列表。 </think>Spring Cloud 与 Spring Cloud Alibaba 的选择需结合技术需求与业务场景,以下是核心对比和决策建议: --- ### **核心技术栈对比** | **维度** | Spring Cloud Netflix | Spring Cloud Alibaba | |------------------|----------------------------------|------------------------------------| | **服务发现** | Eureka(停止维护) | Nacos(动态服务发现+健康检查) | | **配置中心** | Spring Cloud Config(功能基础) | Nacos(动态配置推送+版本管理) | | **流量治理** | Zuul/Hystrix(停止维护) | Sentinel(熔断降级+热点防护) | | **分布式事务** | 无原生方案 | Seata(AT/TCC模式) | | **国产化支持** | 英文文档为主,社区响应慢 | 中文文档完善,阿里云深度整合 | --- ### **选型决策树** ```mermaid graph TD A[需求场景] --> B{是否需要国产化支持?} B -->|是| C[选择Spring Cloud Alibaba] B -->|否| D{是否需强流量治理?} D -->|是| C D -->|否| E[传统Spring Cloud+三方组件] ``` --- ### **具体场景推荐** 1. **新项目/国产化要求** ✅ 必选Spring Cloud Alibaba- 集成Nacos+Sentinel+Seata全家桶 - 阿里云服务(OSS/Redis)开箱即用 2. **存量Netflix系统迁移** ️ 逐步替换: -Nacos替代Eureka/Config -Sentinel替代Hystrix - 保留Spring Cloud Commons标准接口 3. **国际化混合云部署** ⚖️ 混合方案: - 国内用Alibaba组件(监管合规) - 海外区域用Consul+Spring Cloud Gateway --- ### **迁移成本示例** ```java // 传统Spring Cloud → Alibaba改造点 @EnableEurekaClient → @EnableDiscoveryClient // 通用服务发现注解 @EnableHystrix → @SentinelResource // 熔断注解迁移 @RefreshScope → Nacos自动配置刷新 // 移除手动刷新 ```
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值