【13】Nacos Service Registration and Configuration Center

First acquaintance with Nacos

Nacos (Dynamic Naming and Configuration Service) is Alibaba's open source platform for service discovery, configuration management and service management in microservice architecture.

Nacos is a combination of registration center + configuration center (Nacos=Eureka+Config+Bus)
official website: https://nacos.io Download address: https://github.com/alibaba/Nacos

Nacos features

  • Service discovery and health checks
  • Dynamic configuration management
  • Dynamic DNS service
  • Service and metadata management (from the perspective of the management platform, nacos also has a UI page, where you can see registered services and their instance information (
    metadata information), etc.), dynamic service weight adjustment, and dynamic service graceful offline. do it

Nacos service deployment

Registration center service deployment

  1. Download and decompress the installation package and execute the command to start (nacos-server-1.2.0.tar.gz)
linux/mac:sh startup.sh -m standalone
windows:cmd startup.cmd
  1. Access the nacos management interface: http://127.0.0.1:8848/nacos/#/login (default port 8848, account and password
    nacos/nacos)
    Insert image description here

Service provider registers with Nacos

Insert image description here

  • Introduce SCA dependency into parent pom
<dependency>
 <groupId>com.alibaba.cloud</groupId>
 <artifactId>spring-cloud-alibaba-dependencies</artifactId>
 <version>2.1.0.RELEASE</version>
 <type>pom</type>
 <scope>import</scope>
</dependency>
  • Introduce nacos client dependency into the service provider project (copy m-service-resume-8081 and modify it to m-service-resume-8082-nacos)
<dependency>
 <groupId>com.alibaba.cloud</groupId>
 <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
  • Modify application.yml and add nacos configuration information
server:
  port: 8082

spring:
  application:
    name: m-service-resume

  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8 &serverTimezone=UTC
    username: root
    password: root
  jpa:
    database: MySQL
    show-sql: true
    hibernate:
      naming:
        physical-strategy: org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl  #避免将驼峰命名转换为下划线命名
  cloud:
    nacos:
      discovery:
        server-addr: 127.0.0.1:8848

  • Start the service and observe the nacos console
    Insert image description here

Service consumers obtain services from Nacos

Insert image description here

  • Introduce nacos client dependency into the service consumer project (copy m-service-autodeliver-8092 and modify it to m-service-autodeliver-8093-nacos)
<dependency>
 <groupId>com.alibaba.cloud</groupId>
 <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
  • Modify application.yml and add nacos configuration information
server:
  port: 8093

spring:
  application:
    name: m-service-autodeliver
  cloud:
    nacos:
      discovery:
        server-addr: 127.0.0.1:8848

#针对的被调⽤⽅微服务名称,不加就是全局⽣效
m-service-autodeliver:
  ribbon:
    #请求连接超时时间
    ConnectTimeout: 2000
    #请求处理超时时间
    ReadTimeout: 5000
    #对所有操作都进⾏重试
    OkToRetryOnAllOperations: true
    ####根据如上配置,当访问到故障请求的时候,它会再尝试访问⼀次当前实例(次数由MaxAutoRetries配置),
    ####如果不⾏,就换⼀个实例进⾏访问,如果还不⾏,再换⼀次实例访问(更换次数由MaxAutoRetriesNextServer配置),
    ####如果依然不⾏,返回失败信息。
    MaxAutoRetries: 0 #对当前选中实例重试次数,不包括第⼀次调⽤
    MaxAutoRetriesNextServer: 0 #切换实例的重试次数
    NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RoundRobinRule #负载策略调整




# 开启Feign的熔断功能
feign:
  hystrix:
    enabled: false
  compression:
    request:
      enabled: true
      mime-types: text/html,application/xml,application/json # 设置压缩的数据类
    response:
      enabled: true

hystrix:
  command:
    default:
      execution:
        isolation:
          thread:
            ##########################################Hystrix的超时时长设置
            timeoutInMilliseconds: 15000
  • access test
    -Insert image description here

load balancing

  • Introduce nacos client dependency into the service consumer project and add new service m-service-resume-8083-nacos
    Insert image description here

Insert image description here
Insert image description here
The weight is 0, which is equivalent to the service being offline . If you access the service here, you can only access 8082.
Insert image description here

Detailed explanation of Nacos service

Detailed explanation of actual service details

Insert image description here

  • Protection threshold : floating point number between 0 and 1. It is actually a ratio value (current number of healthy service instances/current total
    number of service instances)
    Scenario:
    Under the general process, nacos is the service registration center, and service consumers need to obtain available instance information of a certain service from nacos. Service
    instances are divided into healthy and unhealthy states. When nacos returns the consumer instance information, it will return a healthy instance. At this time,
    there will be certain problems in some concurrency and large traffic scenarios.

If service A has 100 instances, 98 instances are unhealthy, and only 2 instances are healthy. If nacos only returns
information about these two healthy instances, then all subsequent consumer requests will be allocated to these two instances. Instances, when the traffic peak arrives, even two healthy instances
cannot bear it, and the entire service A cannot bear it, and the upstream microservices will also cause collapse, resulting in an avalanche effect.

The significance of the protection threshold is that
when the number of healthy instances of service A/total number of instances < protection threshold, it means that there are really not many healthy instances. At this time, the protection threshold will be triggered (status true) nacos will remove all instances of the service All information (healthy + unhealthy) is provided to consumers. Consumers may access unhealthy instances and the requests fail, but this is better than causing an avalanche. Some requests are sacrificed to ensure the availability of the entire system.

Guess you like

Origin blog.csdn.net/u014535922/article/details/130599759