[000-01-008].第08节:Sentinel 环境搭建

1.Sentinel的构成:

  • 核心库-后台默认的端口是8719
  • 控制台-前台默认的是8080端口

2.2.搭建Sentinel环境:

a.下载Sentinel:

  • 1.sentinel官方提供了UI控制台,方便我们对系统做限流设置。可以在GitHub下载

b.下载后运行Sentinel:

  • 1.提前配置好jdk:
  • 2.运行:将jar包放到任意非中文目录,执行命令:java -jar sentinel-dashboard-1.8.1.jar,sentinel默认使用8080端口:
  • 3.如果要修改Sentinel的默认端口、账户、密码,可以通过下列配置:
配置项 默认值 说明
server.port 8080 服务端口
sentinel.dashboard.auth.username sentinel 默认用户名
sentinel.dashboard.auth.password sentinel 默认密码
  • 4.例如修改端口:java -Dserver.port=8090 -jar sentinel-dashboard-1.8.1.jar

c.访问Sentinel管理页面:

  • 1.访问:访问http://localhost:8080页面,就可以看到sentinel的控制台了,需要输入账号和密码,默认都是:sentinel
  • 2.登录后,发现一片空白,什么都没有,这是因为我们还没有与微服务整合

2.3.微服务8401整合Sentinel:

a.启动Nacos-8848服务:

b.启动Sentinel8080

  • 1.java -jar sentinel-dashboard-1.8.6.jar

c.新建微服务8401模块:

  • 1.建model:建模块cloudalibaba-sentinel-service8401,将被哨兵纳入管控的8401微服务提供者
  • 2.更改pom:引入sentinel依赖
xml 复制代码
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>com.atguigu.cloud</groupId>
        <artifactId>mscloudV5</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>

    <artifactId>cloudalibaba-sentinel-service8401</artifactId>

    <properties>
        <maven.compiler.source>17</maven.compiler.source>
        <maven.compiler.target>17</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>


    <dependencies>
        <!--SpringCloud alibaba sentinel -->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
        </dependency>
        <!--nacos-discovery-->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>
        <!-- 引入自己定义的api通用包 -->
        <dependency>
            <groupId>com.atguigu.cloud</groupId>
            <artifactId>cloud-api-commons</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
        <!--SpringBoot通用依赖模块-->
        <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>
        <!--hutool-->
        <dependency>
            <groupId>cn.hutool</groupId>
            <artifactId>hutool-all</artifactId>
        </dependency>
        <!--lombok-->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.28</version>
            <scope>provided</scope>
        </dependency>
        <!--test-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>
  • 3.改yml:修改application.yaml文件,配置控制台,添加下面内容:
yaml 复制代码
server:
  port: 8401

spring:
  application:
    name: cloudalibaba-sentinel-service
  cloud:
    nacos:
      discovery:
        server-addr: localhost:8848         #Nacos服务注册中心地址
    sentinel:
      transport:
        dashboard: localhost:8080 #配置Sentinel dashboard控制台服务地址
        port: 8719 #默认8719端口,假如被占用会自动从8719开始依次+1扫描,直至找到未被占用的端口
  • 4.主启动:
java 复制代码
package com.atguigu.cloud;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

/**
 * @auther zzyy
 * @create 2023-11-27 18:17
 */
@EnableDiscoveryClient
@SpringBootApplication
public class Main8401
{
    public static void main(String[] args)
    {
        SpringApplication.run(Main8401.class,args);
    }
}
  • 4.业务类:
java 复制代码
package com.atguigu.cloudalibaba.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @auther zzyy
 * @create 2023-05-24 15:35
 */
@RestController
public class FlowLimitController
{

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

    @GetMapping("/testB")
    public String testB()
    {
        return "------testB";
    }
}
 
  • 5.启动服务并访问:

d.启动8401微服务后查看sentinel控制台

  • 1.服务启动,访问order-service的任意端点:打开浏览器,访问http://localhost:8088/order/101,这样才能触发sentinel的监控。然后再访问sentinel的控制台,查看效果:

相关推荐
choice of1 天前
Sentinel:阿里云高并发流量控制
笔记·spring cloud·sentinel
W.Buffer3 天前
SpringCloud-Sentinel实战与源码分析:从流量防护到底层实现
spring·spring cloud·sentinel
TM_soul3 天前
Sentinel安装部署
sentinel
不能再留遗憾了6 天前
【SpringCloud】Sentinel
spring·spring cloud·sentinel
Chan167 天前
流量安全优化:基于 Sentinel 实现网站流量控制和熔断
java·spring boot·安全·sentinel·intellij-idea·进程
@HNUSTer8 天前
基于 GEE 的 Sentinel-2 光谱、指数、纹理特征提取与 Sentinel-1 SAR 数据处理
云计算·sentinel·数据集·遥感大数据·gee·云平台·sar
@HNUSTer11 天前
基于 GEE 平台用 Sentinel-1 SAR 数据实现山区潜在滑坡检测
云计算·sentinel·数据集·遥感大数据·gee·云平台·sar
还是鼠鼠13 天前
《黑马商城》微服务保护-详细介绍【简单易懂注释版】
java·spring boot·spring·spring cloud·sentinel·maven
月夕·花晨16 天前
Gateway-过滤器
java·分布式·spring·spring cloud·微服务·gateway·sentinel
非凡ghost18 天前
Hard Disk Sentinel(固态硬盘监控) 多语便携版
windows·sentinel·软件需求