Nacos

官网

nacos 的安装启动

下载地址

启动命令

1
2
cd nacos/bin  #进入bin目录
sh startup.sh -m standalone #standalone代表单机运行,非集群模式

访问地址

服务发现-基本使用

生产者配置

  1. 引入依赖

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    <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>
    <dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
    </dependency>
  2. application.yaml 配置

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    server:
    port: 9001
    spring:
    application:
    name: cloud-nacos-provider

    cloud:
    nacos:
    discovery:
    server-addr: 192.168.112.128:8848
    management:
    endpoints:
    web:
    exposure:
    include: "*"
  3. 启动类配置使用@EnableDiscoveryClient 注解

消费者配置

  1. 引入依赖

    1
    2
    3
    4
    5
    6
    7
    8
    9
    <dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
    </dependency>
    <!--这里使用loadbalancer 做负载均衡-->
    <dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-loadbalancer</artifactId>
    </dependency>
  2. application.yaml 配置

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    server:
    port: 83

    spring:
    application:
    name: cloud-nacos-consumer
    sleuth:
    sampler:
    probability: 1
    zipkin:
    base-url: http://localhost:9411
    cloud:
    nacos:
    discovery:
    server-addr: 192.168.112.128:8848
    management:
    endpoints:
    web:
    exposure:
    include: "*"

    service:
    url: http://cloud-nacos-provider
  3. 手动注入restTemplate,在启动类上配置@EnableDiscoveryClient注解

配置中心-基本使用

基本使用

  1. 引入依赖

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    <dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
    </dependency>
    <dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
    </dependency>
    <!--必须添加这个依赖-->
    <dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-bootstrap</artifactId>
    </dependency>
  2. bootstrap.yaml

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    server:
    port: 3377
    spring:
    application:
    name: cloud-nacos-config

    cloud:
    nacos:
    discovery:
    server-addr: 192.168.112.128:8848
    config:
    server-addr: 192.168.112.128:8848 # 配置nacos config server地址
    file-extension: yaml #配置文件的扩展名,默认为properties
    refresh-enabled: true # 自动刷新默认开启,如果要关闭设置为false

    # 会到服务器的default namespace -> DEFAULT_GROUP -> cloud-nacos-config.yaml 的文件


配置自动刷新

  1. cloud.nacos.config.refresh-enabled=true 默认就是开启的。
  2. 在需要刷新的类上添加@RefreshScope 注解

profile粒度配置

关于粒度的配置必须放在bootstrap.yaml文件里面

1
2
3
spring:
profiles:
active: dev

测试后,放在普通的application.yaml文件里面也可以生效

namespace 和 group 自定义配置

namespace配置

  1. 创建命名空间

    image-20220325152453581

  2. 添加配置在自定义命名空间下

    image-20220325152617200

  3. bootstrap.yaml配置

    1
    2
    3
    4
    5
    6
    7
    8
    spring:
    cloud:
    nacos:
    config:
    #配置命名空间
    namespace: 7ab3b839-b579-402c-992b-bb2ea46a7172
    #配置分组
    group: TEST_GROUP

dataid 扩展

这里不做详细介绍,用到的话,去官网查怎么用

官网

配置优先级

Spring Cloud Alibaba Nacos Config 目前提供了三种配置能力从 Nacos 拉取相关的配置。

  • A: 通过 spring.cloud.nacos.config.shared-configs[n].data-id 支持多个共享 Data Id 的配置
  • B: 通过 spring.cloud.nacos.config.extension-configs[n].data-id 的方式支持多个扩展 Data Id 的配置
  • C: 通过内部相关规则(应用名、应用名+ Profile )自动生成相关的 Data Id 配置

当三种方式共同使用时,他们的一个优先级关系是:A < B < C

关闭nacos-config功能

1
spring.cloud.nacos.config.enabled = false

nacos集群

sentinel

Sentinel控制台

sentinel控制台下载链接

控制台启动命令:

1
2
# 默认是8080端口
java -Dserver.port=8080 -Dcsp.sentinel.dashboard.server=localhost:8080 -Dproject.name=sentinel-dashboard -jar sentinel-dashboard.jar

注意

控制台是懒加载的,只有当你访问一次应用后,控制台才会显示该引用的信息。

基本使用

  1. 引入依赖

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    <!--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>
  2. application.yaml 配置

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    server:
    port: 8401

    spring:
    application:
    name: cloudalibaba-sentinel-service
    sleuth:
    sampler:
    probability: 0.5
    zipkin:
    base-url: http://localhost:9411
    cloud:
    nacos:
    discovery:
    server-addr: 192.168.112.128:8848
    sentinel:
    transport:
    port: 8719 #尝试端口,会从8719开始尝试,不行就+1
    dashboard: localhost:8080 #配置控制台的地址
    management:
    endpoints:
    web:
    exposure:
    include: "*"

限流规则

熔断降级

在条件满足后,会熔断,超过熔断时间后,会慢慢放行。

热点规则

需要配置blockerhandler,错误信息自定义

如果不添加,默认显示的是程序的错误信息。

只有qps一种检验方式,是检验方法参数的次数,只要带了那个参数就会被计算进入检验,第几个参数应该是通过在方法上的生命顺序确定的。

必须使用@SentinelResource(value = "testHostKey"),不添加这个在路径上添加热点规则,不生效的。

参数是从0开始

限流不处理程序异常错误,他只会处理不满足规则的错误。

熔断处理程序异常错误。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
@GetMapping("/testHostKey")
@SentinelResource(value = "testHostKey",blockHandler = "testHostKeyHandler")
public String testHostKey(@RequestParam(name = "p1",required = false) String p1,
@RequestParam(name = "p2", required = false) String p2) {
log.info("testHostKey:" + p1 + ", " + p2);
// int i = 10/0;
return "testHostKey";
}

public String testHostKeyHandler(String p1, String p2, BlockException e) {
log.info("testHostKey:" + p1 + ", " + p2);
System.out.println(e);
return "testHostKeyHandler 苦苦";
}

系统规则

@sentinelResource 注解的name可以和路径相同吗

sentinelresource注解官网

fallback,相当于hystrix中的服务降级,用于处理程序异常时的兜底方法。

blockhandler,处理sentinel控制台违规异常

exceptionToIgnore ,会忽略异常,因此不会走fallback方法,但是仍然会被计算进入控制台规则中。

情况一:

当配置了fallback 和 blockhandler,两种配置同时生效,最后显示的是blockhandler的配置。

sentinel 整合 rest+balancer

因为ribbon现在已经被弃用,所以这里使用rest+balancer,

这里使用和demo中的没有什么不同,重点看下接下来整合openfeign的配置。

sentinel 整合openFeign

官网

  1. 引入依赖

    1
    2
    3
       


  2. application.yaml

    1
    2
    3
    # feign 开启对 sentinel 的支持


规则持久化

之前的限流规则是临时的,应用重启后,配置的规则就会消失。

官方推荐持久化到nacos中。

这个持久化不是sentinel中的配置保存到nacos中,而是在nacos中保存信息,sentinel去读取,所以在sentinel中对规则的修改不会持保存到nacos中。

  1. 引入依赖

    1
    2
    3
    4
    <dependency>
    <groupId>com.alibaba.csp</groupId>
    <artifactId>sentinel-datasource-nacos</artifactId>
    </dependency>
  2. application.yaml 配置

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    server:
    port: 8401

    spring:
    application:
    name: cloudalibaba-sentinel-service
    sleuth:
    sampler:
    probability: 0.5
    enabled: false
    zipkin:
    base-url: http://localhost:9411
    cloud:
    nacos:
    discovery:
    server-addr: 192.168.112.128:8848
    sentinel:
    transport:
    port: 8719
    dashboard: localhost:8080
    datasource:
    ds1:
    nacos:
    server-addr: 192.168.112.128:8848
    dataId: ${spring.application.name}
    groupId: DEFAULT_GROUP
    data-type: json
    rule-type: flow
    management:
    endpoints:
    web:
    exposure:
    include: "*"
  3. 在nacos配置中心中添加配置。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    文件的dataId,groupId,文件类型和yaml配置文件中的相同。
    [
    {
    "resource":"/rateLimit/byUrl",
    "limitApp":"default",
    "grade":1,
    "count":1,
    "strategy":0,
    "controlBehavior":0,
    "clusterMode":false
    }
    ]

    配置信息的解释:

    resource: 资源名称
    limitApp: 来源应用
    grade: 阈值类型,0表示线程,1表示QPS
    count: 单机阈值
    strategy: 流控模式,0表示直接,1表示关联,2表示链路
    controlBehavior: 流控效果,0表示快速失败,1表示Warm Up,2表示排队等待
    clusterMode: 是否集群

    seata

    alibaba 解决分布式事务的 一套框架。

    seata官网

    seata1.4使用教程