栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

【Spring Cloud Alibaba】sentinel环境搭建

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

【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

	
        
            
                org.springframework.cloud
                spring-cloud-dependencies
                Hoxton.SR12
                pom
                import
            
            
                com.alibaba.cloud
                spring-cloud-alibaba-dependencies
                2.2.5.RELEASE
                pom
                import
            
        
    
	
        8
        8
    

    
        
        
            com.alibaba.cloud
            spring-cloud-starter-alibaba-nacos-discovery
        
        
        
            com.alibaba.cloud
            spring-cloud-starter-alibaba-nacos-config
        
        
        
            com.alibaba.cloud
            spring-cloud-starter-alibaba-sentinel
        
        
        
            com.alibaba.csp
            sentinel-datasource-nacos
        

        
            org.springframework.boot
            spring-boot-starter-web
        
        
            org.springframework.boot
            spring-boot-starter-actuator
        
    
    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    

我使用的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;


@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中才会有该服务的信息)

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/885498.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号