【Spring Cloud Alibaba】sentinel环境搭建

一、环境准备

从github下载sentinel
在这里插入图片描述
这里下载可能非常的慢,可以右键复制链接地址,然后在迅雷里面去下载,非常的快。下载下来以后,上传到linux服务器直接启动。
启动方式一:java -jar sentinel-dashboard-1.8.4.jar需要保证8080端口没有被占用
启动方式二:java -jar -Dserver.port 8081 sentinel-dashboard-1.8.4.jar 自己指定一个端口
启动方式三:nohup java -jar -Dserver.port=9081 sentinel-dashboard-1.8.4.jar >> ./sentinel.log 2>&1 &后台启动,并且日志输出到当前目录的sentinel.log文件中
我采用第三种方式启动nohup java -jar -Dserver.port=9081 sentinel-dashboard-1.8.4.jar >> ./sentinel.log 2>&1 &
在这里插入图片描述
浏览器访问 http://192.168.101.41:9081
在这里插入图片描述
登录进去:sentinel/sentinel
在这里插入图片描述
这样sentinel控制台就算是部署成功了,接下来写程序连接到sentinel中

二、客户端程序搭建

依赖文件引入pom.xml

	<dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Hoxton.SR12</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <dependency>
                <groupId>com.alibaba.cloud</groupId>
                <artifactId>spring-cloud-alibaba-dependencies</artifactId>
                <version>2.2.5.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
	<properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>

    <dependencies>
        <!--nacos注册中心配置-->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>
        <!--nacos配置中心配置-->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
        </dependency>
        <!--sentinel熔断限流降级-->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
        </dependency>
        <!--sentinel持久化-->
        <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>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

我使用的nacos作为注册中心和配置中心参见【Spring Cloud Alibaba】nacos注册中心实践以及【Spring Cloud Alibaba】nacos配置中心实践也可以不用,直接将nacos相关的依赖删除即可。
下面是bootstrop.yml的配置

spring:
  application:
    name: nacos-cluster
  cloud:
    nacos:
      discovery:
        server-addr: 192.168.101.41:8858  #nacos作为注册中心地址
      config:
        server-addr: 192.168.101.41:8858 #nacos作为配置中心地址
        file-extension: yml # 指定yaml的格式的配置
    sentinel:
      transport:
        # 配置sentinel dashboard地址
        dashboard: 192.168.101.41:9081
        # 默认8719端口(与sentinel交换数据),假如被占用会自动从8719开始依次+1扫描,直至找到未被占用的端口
        port: 8719
        # 当前服务的部署主机地址
        client-ip: 192.168.101.39

下面是application.yml的配置

spring:
  profiles:
    active: dev
server:
  port: 8809

management:
  endpoints:
    jmx:
      exposure:
        include: '*'

启动程序

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

普通的controller

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

/**
 * @author 隐市高手
 * @date 2022/5/14 15:04
 */
@RestController
public class LimitController {

    @GetMapping("/get/sentinel1")
    public String get1() {
        return "sentinel test 一";
    }

    @GetMapping("/get/sentinel2")
    public String get2() {
        return "sentinel test 二";
    }
}

最后结构如下图
在这里插入图片描述

三、验证sentinel监控结果

启动程序,分别访问/get/sentinel1/get/sentinel2两个接口(如果只是启动了程序,没有任何接口的访问,在sentinel中将没有这个服务的任何信息,这是一个懒加载模式,只有访问过了,在sentinel中才会有该服务的信息)
在这里插入图片描述
在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值