4.springcloudalibaba sentinel v1.8.6版本服务搭建


前言

前面完成了gateway项目部署并且测试,现在部署搭建sentinel服务并且测试。


一、sentinel服务端安装

1.1 服务端下载

下载地址
这里选择的是目前最新的sentinel版本
在这里插入图片描述
直接下载启动jar包,使用命令安装服务
在这里插入图片描述

1.2 启动sentinel服务

java -Dserver.port=8480 -Dcsp.sentinel.dashboard.server=192.168.184.131:8480 -Dproject.name=sentinel-dashboard -Dsentinel.dashboard.auth.username=sentinel -Dsentinel.dashboard.auth.password=123456 -jar sentinel-dashboard-1.8.6.jar &
# 指定控制台的端口为8480
-Dserver.port=8480 
# 指定要被哪个控制台监控(这里指定的是自己监控自己)
-Dcsp.sentinel.dashboard.server=192.168.184.131:8480 
# 指定实例名称(名称会在控制台左侧以菜单显示)
-Dproject.name=sentinel-dashboard 
# 设置登录的帐号为:sentinel 
-Dsentinel.dashboard.auth.username=sentinel 
# 设置登录的密码为:123456
-Dsentinel.dashboard.auth.password=123456 

使用设置的账号密码登录如下图所示启动成功
在这里插入图片描述

二、客户端使用sentinel

2.1.pom增加sentinel包

        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
        </dependency>

完整pom如下

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.springcloudalibaba</groupId>
    <artifactId>server</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>server</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>8</java.version>
        <spring-boot.version>2.6.13</spring-boot.version>
        <spring-cloud.version>2021.0.5</spring-cloud.version>
        <spring-cloud-alibaba.version>2021.0.5.0</spring-cloud-alibaba.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!-- SpringCloud Alibaba Nacos -->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>

        <!-- SpringCloud Alibaba Nacos Config -->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-bootstrap</artifactId>
        </dependency>
    </dependencies>
    <dependencyManagement>
    <dependencies>
    <!-- SpringCloud 微服务 -->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-dependencies</artifactId>
        <version>${spring-cloud.version}</version>
        <type>pom</type>
        <scope>import</scope>
    </dependency>

    <!-- SpringCloud Alibaba 微服务 -->
    <dependency>
        <groupId>com.alibaba.cloud</groupId>
        <artifactId>spring-cloud-alibaba-dependencies</artifactId>
        <version>${spring-cloud-alibaba.version}</version>
        <type>pom</type>
        <scope>import</scope>
    </dependency>

    <!-- SpringBoot 依赖配置 -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-dependencies</artifactId>
        <version>${spring-boot.version}</version>
        <type>pom</type>
        <scope>import</scope>
    </dependency>
    </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>2.6.0</version>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

2.2 增加配置

    sentinel:
        #取消懒加载
      eager: true
        #sentinel服务地址
      transport:
        dashboard: 192.168.184.131:8480

完整的配置如下

# Tomcat
server:
  port: 8081

# Spring
spring:
  application:
    # 应用名称
    name: server
  profiles:
    # 环境配置
    active: dev
  main:
    allow-bean-definition-overriding: true
  cloud:
    nacos:
      discovery:
        # 服务注册地址
        server-addr: 192.168.184.130:8848
        group: alibaba
        namespace: 7dd9fa65-9c9d-484f-94f8-d621ca05d0e5
        register-enabled: true
      config:
        # 配置中心地址
        server-addr: 192.168.184.130:8848
        # 配置文件格式
        file-extension: yml
        group: ${spring.cloud.nacos.discovery.group}
        namespace: ${spring.cloud.nacos.discovery.namespace}
        shared-configs[0]:
          data-id: application-${spring.profiles.active}.${spring.cloud.nacos.config.file-extension} # 配置文件名-Data Id
          group: ${spring.cloud.nacos.discovery.group}   # 默认为DEFAULT_GROUP
          refresh: false
    sentinel:
        #取消懒加载
      eager: true
        #sentinel服务地址
      transport:
        dashboard: 192.168.184.131:8480
logging:
  level:
    com.alibaba.nacos.client: info

2.3 启动服务

启动服务后,查看控制台,发现server服务已经可以看到监控情况。
在这里插入图片描述
可以看到server服务已经在监控下了

三、验证

3.1 给hello接口增加流控规则

接口如下

package com.springcloudalibaba.gateway.control;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class Control {

    @RequestMapping(value = "/hello")
    public String index() {
        System.out.println("testname: ");
        return "testName";
    }


}

给hello接口增加流控规则
资源名为请求接口路径,qps为1s请求数
在这里插入图片描述

3.2 测试结果如下

在这里插入图片描述
可以看到流控规则已经生效

总结

1.sentinel启动使用,比较简单,后面还可以配置流控规则持久化,这里就不演示了,大概知道了sentinel如何使用。
2.sentinel不仅仅可以限流,还可以熔断,配置接口白名单,黑名单。
3.使用@sentinelResource注解还可以自定义异常返回,更加灵活的使用sentinel配置异常返回。

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值