SpringCloud Alibaba之Sentinel案例初始化演示

所有代码都在github上:https://github.com/demonruin/cloud2020/tree/master

 

首先需要准备的为:nacos8848控制中心这里单点演示、sentinel-dashboard控制台8080、新建sentinel-service8401微服务

因为nacos8848是docker直接启动、sentinel-dashboard是jar包直接启动,下面只剩下新建一个sentinel-service8401了

1、新建modul为 cloudalibaba-sentinel-service8401,并在pom中添加相关依赖包,以后用alibaba这一套nacos和sentinel基本不分家

        
        
            com.alibaba.cloud
            spring-cloud-starter-alibaba-nacos-discovery
        
        
        
            com.alibaba.csp
            sentinel-datasource-nacos
        
        
        
            com.alibaba.cloud
            spring-cloud-starter-alibaba-sentinel
        
        
        
            org.springframework.cloud
            spring-cloud-starter-openfeign
        
        
        
            org.springframework.boot
            spring-boot-starter-web
        
        
            org.springframework.boot
            spring-boot-starter-actuator
        
        
        
            org.springframework.boot
            spring-boot-devtools
            runtime
            true
        

2、构建application.yml文件

server:
  port: 8401

spring:
  application:
    name: cloudalibaba-sentinel-service
  cloud:
    nacos:
      discovery:
        server-addr: localhost:8848 #Nacos服务注册中心地址
    sentinel:
      transport:
        dashboard: localhost:8080 #配置Sentinel dashboard地址
        #默认8719端口,假如被占用会自动从8719开始依次+1扫描,直至找到未被占用的端口
        port: 8719

#暴露监控端点
management:
  endpoints:
    web:
      exposure:
        include: '*'


3、构建主启动类,添加服务发现注解

/**
 * created by king on 2020/5/26 3:46 下午
 */
@SpringBootApplication
@EnableDiscoveryClient
public class SentinelServiceMain8401 {
    public static void main(String[] args) {
            SpringApplication.run(SentinelServiceMain8401.class,args);
        }
}

4、构建FlowLimitController业务类进行测试

@RestController
@Slf4j
public class FlowLimitController {

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

    @GetMapping("/testB")
    public String testB()
    {
        log.info(Thread.currentThread().getName()+"\t"+"...testB");
        return "------testB";
    }

}

5、启动nacos8848、sentinel-dashboard8080、cloudalibaba-sentinel-service8401微服务

6、查看是否服务注册成功,sentinel监控是否成功

1、服务注册nacos成功

SpringCloud Alibaba之Sentinel案例初始化演示_第1张图片

2、因为Sentinel采用的懒加载说明,所以此时查看sentinel-dashboard里面还是什么都没有,需要进行请求,才会产生数据,现在我们请求testA和testB接口,执行一次访问即可,下面我们来查看结果SpringCloud Alibaba之Sentinel案例初始化演示_第2张图片

SpringCloud Alibaba之Sentinel案例初始化演示_第3张图片

SpringCloud Alibaba之Sentinel案例初始化演示_第4张图片

此时说明:sentinel8080正在监控微服务8401

简单的初始化案例演示完成

 

你可能感兴趣的:(SpringCloud,SpringCloud,Alibaba,Springboot)