2. Alibaba Sentinel IDEA 简单使用

本文介绍了如何在IDEA中创建一个微服务项目,并集成Nacos和Sentinel实现服务发现和流量控制。通过添加相关依赖,配置文件设置服务发现和Sentinel Dashboard地址,然后创建测试接口。启动项目后,Sentinel会按需加载,通过访问接口激活其监控功能。在Sentinel Dashboard上可以观察到服务的流量情况。
摘要由CSDN通过智能技术生成

项目概述

  1. 需要启动nacos , 192.168.43.11:8848
  2. 需要启动sentinel-dashbord http://192.168.43.11:31808/#/login
  3. IDEA创建微服务项目,模块sentinel-service
  4. 启动微服务项目
  5. 然后去sentinel-dashbord查看情况

步骤1和步骤2都提前完成了,需要的查看其他博文

IDEA创建微服务项目

模块sentinel-service

  1. 导入依赖pom.xml
<!-- 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>
  1. 配置文件
server.port=8401
spring.application.name=sentinel-service
spring.cloud.nacos.discovery.server-addr=192.168.43.11:8848
# 配置Sentinel dashboard地址
spring.cloud.sentinel.transport.dashboard=192.168.43.11:31808
# 默认8719端口,键入被占用会自动从8719+1,直到找到未被占用的端口
spring.cloud.sentinel.transport.port=8719
# 暴露所有端点
management.endpoint.web.exposure.include=*
  1. 添加测试流量入口
@RestController
public class FlowLimitController {
    @GetMapping("/testA")
    public String testA(){
        return "-----testA";
    }

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

  1. Application入口
@SpringBootApplication
@EnableDiscoveryClient  //启用服务注册发现客户端,这样才能通过nacos获取和注册服务
public class SentinelServiceApplication {
    public static void main(String[] args) {
        SpringApplication.run(SentinelServiceApplication.class, args);
    }
}

查看结果

当以上的这些配置配置好以后,我们就可以进行测试了,那我们的测试方式就是,首先保证Nacos和Sentinel都是启动状态,然后再来启动项目,按照我们的理解这个时候,就应该在Sentinel的dashboard上能体现出它监控的服务,但是此时并没有,原因是因为Sentinel本身采用的是懒加载机制,所以我们需要首先访问服务对应的接口,Sentinel才能工作。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值