所有代码都在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基本不分家
<!--SpringCloud ailibaba nacos -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<!--SpringCloud ailibaba sentinel-datasource-nacos 后续做持久化用到-->
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-datasource-nacos</artifactId>
</dependency>
<!--SpringCloud ailibaba sentinel -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>
<!--openfeign-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<!-- SpringBoot整合Web组件+actuator -->
<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>
<!--日常通用jar包配置-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
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成功
2、因为Sentinel采用的懒加载说明,所以此时查看sentinel-dashboard里面还是什么都没有,需要进行请求,才会产生数据,现在我们请求testA和testB接口,执行一次访问即可,下面我们来查看结果
此时说明:sentinel8080正在监控微服务8401
简单的初始化案例演示完成