[SpringCloud learning] Eureka service registration and discovery, Ribbon load balancing, CAP principle

Eureka service registration and discovery

Eureka is a sub-module of Netflix and one of the core modules. Eureka is a Rest-based service, which is used to locate services to realize cloud middle layer service discovery and failover. With service registration and discovery, you only need to use the service identifier to access the service without modifying the service call Configuration file, similar to Dubbo's registration center, such as zookeeper.

  • Eureka consists of two components: Eureka Server and Eureka Client

  • Eureka Server provides service registration service. After each node is started, the service node information can be seen intuitively in the interface

  • Eureka Client is a java client that simplifies the interaction with EurekaServer. The client also has a built-in load balancer that uses a round-robin load algorithm. After the application starts, it will send a heartbeat to EurekaServer (the default period is 30 seconds). If Eureka Server does not receive a node's heartbeat within multiple heartbeat cycles, EurekaServer will remove the service node from the service registry (the default cycle is 90 seconds)

  • three roles

    • Eureka Server: Provides service registration and discovery. Similar to ZK
    • Service Provider: Register its own service in Eureka so that consumers can find it.
    • Service Consumer: The service consumer obtains the registered service list from Eureka to find the consumer service.

Eureka has a self-protection mechanism. When a microservice is unavailable, Eureka will not clean it up immediately. In the self-protection mode, EurekaServer will protect the information in the service registration and no longer log out any service instances. There is a heartbeat detection mechanism (90s). When the microservice re-registers during this period, Eureka will exit the protection mechanism.

Disable the self-protection mechanism (recommended not to close)eureka.server.enable-self-preservation=false

Routine:

1. Import pom dependencies

2. Add configuration

3. Turn on the function Enable

The registration center is a server. Relatively speaking, the service provider is a client, who registers with the registration center. The service consumer is also a client, and discovers services in the registry.

1. Configure the Eureka registry

1. Import pom.xml dependencies

<!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-netflix-eureka-server -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
            <version>3.0.1</version>
        </dependency>

2. Add configuration

server:
  port: 7001

eureka:
  instance:
    hostname: eureka7001.com
  client:
    register-with-eureka: false #向eureka注册中心注册自己, 这里是实现注册中心,所以不用注册自己
    fetch-registry: false #false表示自己是注册中心
    service-url: #监控页面地址
      defaultZone: http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/

​ Addresses of other registration centers monitored by defaultZone, you don’t need to write your own address, other registration centers will register themselves.

3. Enable the function @EnableEurekaServer
Please add a picture description

Cluster environment configuration
Please add a picture description
Registration center cluster, one registration center collapses, other registration centers can still be used, consumers only need to switch addresses

Each registration center needs to register with other registration centers to form an interconnection

The service provider Client registers the service to all registration centers.

When a consumer requests to call, it is still the same as before, using Rest to call the address remotely, but instead of calling the IP address of the service, it looks for the interface corresponding to the service name in the registration center.

Here, in order to simulate the cluster, three Eureka registration centers are created, and the configuration is to change the port number. If one of the registration centers collapses, consumers can still call from other registration centers.

Please add a picture description

Second, configure the service provider

1. Import pom.xml dependencies

 <!--加入Eureka依赖-->
        <!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-netflix-eureka-client -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
            <version>3.0.1</version>
        </dependency>

        <!--actuator用于完善监控信息-->
        <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-actuator -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
            <version>2.6.4</version>
        </dependency>

2. Modify the configuration

#eureka配置,服务注册到哪里
eureka:
  client:
    service-url:
      defaultZone: http://eureka7001.com:7001eureka/,http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/
  instance:
    instance-id: springcloud-provide-dept8001 #修改eureka上的默认描述信息

#info配置
info:
  app.name: freeze-app
  company: Tencent

​ When info information is used for company development, other members of the team may need to see service information

Please add a picture description

3. Enable function @EnableEurekaClient

Please add a picture description

In order to reflect the load balancing mechanism, three service providers are created here, corresponding to three databases respectively. The field of db_source in the table corresponds to the database name, and other information is the same.

Please add a picture description

3. Configure service consumers

1. Configure pom.xml

 <!--Ribbon 新版本Eureka已经包含Ribbon-->
        <!--Eureka-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
            <version>3.0.1</version>
        </dependency>

​ The Ribbon dependencies used later are already included in the new version of eureka client, so there is no need to import Ribbon (500 error will occur)

2. Add configuration

#Eureka
eureka:
  client:
    register-with-eureka: false #消费者不用向Eureka注册自己,只需要去拿服务
    service-url:
      defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/

Here we need to add the addresses of all registration centers, and discover services from each registration center.

3. Turn on the function @EnableEurekaClient is the same as the service provider

4. Modify the address of the service called by the Controller. The IP address of the previously used service provider is called directly (no registration center is used). When using Ribbon, the address of the service request should be a variable (services can be requested from different registration centers). So access through the service name (the service name of the same service must be consistent)

//用Ribbon时,服务请求的地址应该是一个变量,通过服务名来访问
    //private static final String REST_URL_PREFIX="http://localhost:8001";//前缀固定的
    private static final String REST_URL_PREFIX="http://SPRINGCLOUD-PROVIDER-DEPT";

5. RestTemplat adds a load balancing annotation @LoadBalanced default polling mechanism. You can customize the load balancing mechanism

Please add a picture description

When customizing the Ribbon load balancing strategy, this custom class cannot be placed under the current package and subpackages scanned by @ComponentScan, otherwise the configuration class we customized will be shared by all Ribbon clients, that is, our The purpose of specialization cannot be achieved.

4. Test the load balancing mechanism

Each time the service is invoked, the interfaces of different service providers will be requested in turn.

Ribbon

Spring Cloud Ribbon is a set of client load balancing tools based on Netflix Ribbon

Load balancing: evenly distribute user requests to multiple services to achieve HA (high availability)

Simple classification of load balancing:

  • Centralized LB
    • That is, an independent LB facility is used between the consumer and the provider of the service, such as Nginx: a reverse proxy server, which is responsible for forwarding the access request to the provider of the service through a certain strategy
  • Process LB
    • Integrate LB logic into the consumer, the consumer learns which addresses are available from the service registry, and then selects a suitable server from these addresses
    • Ribbon belongs to the process LB, it is just a class library, integrated in the process of the consumer, through which the consumer obtains the address of the service provider

After the integration of Ribbon and Eureka, the client can call directly without caring about the IP address and port provided by the service. When the original stand-alone, the service address was written directly.

Please add a picture description

CAP principle

C (Consistency) strong consistency

A (Availability) Availability

P (Partition tolerance) partition fault tolerance

CAP three into two: CA CP AP cannot meet three conditions at the same time

Partition fault tolerance P must be guaranteed in a distributed system, so it can only be weighed in A and C

Zookeeper is guaranteed CP Eureka is guaranteed AP

Eureka can well deal with the loss of some nodes caused by network failures, without paralyzing the entire service like zk

Guess you like

Origin blog.csdn.net/adminguojieBin/article/details/123510582