集成nacos+sentinel

大致步骤如下

  1. 启动Nacos8848成功

2.编写微服务

3.启动Sentinel8080

  1. 启动微服务

5.启动8401微服务查看sentinel控制台

编写微服务

pom

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!-- 注册中心nacos -->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>

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

application.yml

server:
  port: 8005

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

controller

@RestController
public class FlowLimitController {
    @GetMapping("testA")
    public String testA(){
        return "----------------- testA ------------------";
    }

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

SpringBoot项目启动入口

@SpringBootApplication
@EnableDiscoveryClient       // 将微服务注册到注册中心
public class SentinelService
{
    public static void main( String[] args )
    {
        SpringApplication.run(SentinelService.class, args);
    }
}

编写完成后,必须得先访问一下接口,再去sentinel控制台中才能看到监控的微服务(因为sentinel采用的是懒加载)。


image.png
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容