springcloud (2): registration center Eureka

Eureka is an open source product of Netflix that provides service registration and discovery. It provides a complete implementation of Service Registry and Service Discovery. It is also one of the most important and core components in the springcloud system.

Background introduction

Service Center

The service center, also known as the registration center, manages various service functions including service registration, discovery, fuse, load, downgrade, etc., such as various functions of the dubbo admin background.

What will happen to the calling relationship of the service center? Draw a few diagrams to help you understand

Project A calls Project B

Call project A to request project B normally

With the service center, any service can not be directly removed, it needs to be called through the service center

Project A calls project B, and project B is calling project C

At this time, the calling steps will be two steps: the first step, project A first requests the project B server from the service center, and then project B requests the project C service from the service center.

The above projects are just two or three simple calls between each other, but if there are more than 20 projects and 30 projects, at the end of 2015, our distributed projects reached more than 20. Draw a picture to describe the number of projects. The mutual calling relationship between the ten projects is all lines. Any change in one of the projects will involve several projects to restart, which is very troublesome and error-prone. To obtain services through the service center, you do not need to pay attention to the IP address of the project you are calling. It consists of several servers. You can directly go to the service center to obtain the available services to call each time.

Since various services are registered in the service center, there are many advanced functional conditions. For example, several services provide the same service for load balancing; monitor the success rate of server calls to do circuit breakers, remove fault points in the service list; monitor service call time to set different weights for different servers, and so on.

Before I say Eureka, let me gossip about Netflix

Netflix

The following introduction comes from Baidu Encyclopedia:

Netflix is ​​an American company that provides Internet on-demand streaming media playback in the United States and Canada, and customizes DVD and Blu-ray disc online rental services. Founded in 1997 and headquartered in Los Gatos, California, the company started subscription services in 1999. In 2009, the company offered as many as 100,000 DVD movies and had 10 million subscribers. On February 25, 2007, Netflix announced that it had sold its billionth DVD. According to a report by IHS, in 2011, Netflix's online movie sales accounted for 45% of the total online movie sales for US users.

The first time I saw this word was at the beginning of various American dramas or movies. Representative American dramas shot by Netflix include "House of Cards", "Narcos" and "Stranger Things". Later, when I researched springcloud, I found Netflix, and I wondered if they were the same company. After checking the email suffix on github, it was determined that it was indeed the same company. In fact, springcloud's microservices are based on Netflix's open source products.

Netflix's open source framework components have been proven in production for many years in Netflix's large-scale distributed microservice environment, and are gradually being accepted by the community as standard components for constructing microservice frameworks. Spring Cloud open source products are mainly based on the further encapsulation of Netflix open source components, which is convenient for Spring developers to build a basic framework for microservices. For some companies that intend to build a microservice framework system, making full use of or referring to Netflix's open source microservice components (or Spring Cloud) and making necessary enterprise customizations on this basis is undoubtedly a shortcut to a microservice architecture.

Eureka

According to the official introduction:

    Eureka is a REST (Representational State Transfer) based service that is primarily used in the AWS cloud for locating services for the purpose of load balancing and failover of middle-tier servers.

    Eureka is a REST-based service, mainly used in the AWS cloud, to locate services for load balancing and failover of middle-tier servers.

Spring Cloud encapsulates the Eureka module developed by Netflix to implement service registration and discovery. Eureka adopts the design architecture of CS. Eureka Server acts as a server for service registration function, it is the service registration center. Other microservices in the system use the Eureka client to connect to the Eureka Server and maintain the heartbeat connection. In this way, the maintainers of the system can monitor whether the microservices in the system are running normally through the Eureka Server. Some other modules of Spring Cloud (such as Zuul) can discover other microservices in the system through Eureka Server and execute related logic.

Eureka consists of two components: Eureka server and Eureka client. Eureka server is used as service registry server. The Eureka client is a java client that simplifies interaction with the server, acts as a round robin load balancer, and provides failover support for services. Netflix uses an additional client in its production environment that provides weighted load balancing based on traffic, resource utilization, and error status.

Use a picture to recognize the following:

The above figure briefly describes the basic architecture of Eureka, which consists of 3 roles:

1、Eureka Server

  • Provide service registration and discovery

2、Service Provider

  • service provider
  • Register its own service with Eureka so that service consumers can find it

3、Service Consumer

  • service consumer
  • Get a list of registered services from Eureka to be able to consume services

case practice

Eureka Server

Spring cloud has helped me implement a service registry, and we only need a few simple steps to complete it.

1. Add dependencies to pom

<dependencies>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-eureka-server</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>

2. Add @EnableEurekaServerannotations to the startup code

@SpringBootApplication
@EnableEurekaServer
public class SpringCloudEurekaApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringCloudEurekaApplication.class, args);
    }
}

3. Configuration file

By default, the service registry will also try to register itself as a client, so we need to disable its client registration behavior by application.propertiesadding the following configuration:

spring.application.name=spring-cloud-eureka

server.port=8000
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false

eureka.client.serviceUrl.defaultZone=http://localhost:${server.port}/eureka/
  • eureka.client.register-with-eureka: Indicates whether to register itself with Eureka Server, the default is true.
  • eureka.client.fetch-registry: Indicates whether to obtain registration information from Eureka Server, the default is true.
  • eureka.client.serviceUrl.defaultZone: Set the address for interacting with Eureka Server. Both query services and registration services need to rely on this address. The default is http://localhost:8761/eureka; multiple addresses can be used, separated.

After starting the project, visit: http://localhost:8000/, you can see the following page, which has not found any services

cluster

For such a critical service as the registry, if it is a single point, it will be devastating to encounter failures. In a distributed system, the service registry is the most important basic part, and it should be in a state where services can be provided at any time. To maintain its availability, using a cluster is a good solution. Eureka achieves high-availability deployment by registering with each other, so we only need to configure Eureke Server with other available serviceUrls to achieve high-availability deployment.

Dual Node Registry

For the first time, we tried the construction of a dual-node registry.

1. Create application-peer1.properties as the configuration of peer1 service center, and point serviceUrl to peer2

spring.application.name=spring-cloud-eureka
server.port=8000
eureka.instance.hostname=peer1

eureka.client.serviceUrl.defaultZone=http://peer2:8001/eureka/

2. Create application-peer2.properties as the configuration of the peer2 service center, and point serviceUrl to peer1

spring.application.name=spring-cloud-eureka
server.port=8001
eureka.instance.hostname=peer2

eureka.client.serviceUrl.defaultZone=http://peer1:8000/eureka/

3. Host conversion

Add the following configuration to the hosts file

127.0.0.1 peer1  
127.0.0.1 peer2 

4. Package start

Execute the following commands in sequence

#Pack
mvn clean package
# Start eureka with peer1 and peer2 configuration information respectively
java -jar spring-cloud-eureka-0.0.1-SNAPSHOT.jar --spring.profiles.active=peer1
java -jar spring-cloud-eureka-0.0.1-SNAPSHOT.jar --spring.profiles.active=peer2

After the startup is completed in sequence, the browser input: The http://localhost:8000/rendering is as follows:

According to the figure, it can be seen that the registration center DS Replicas of peer1 already has the relevant configuration information of peer2, and it appears in available-replicas. We manually stop peer2 to observe, and find that peer2 will move to the unavailable-replicas column, indicating that peer2 is unavailable.

At this point, the configuration of the dual node has been completed.

eureka cluster usage

In production, we may need three or more registries to ensure the stability of the service. The principle of configuration is actually the same. Point the registries to other registries respectively. Only the configuration of three clusters is introduced here. In fact, it is similar to the two-node registry. Each registry can point to the other two nodes, and use application.yml to configure.

The application.yml configuration details are as follows:

---
spring:
  application:
    name: spring-cloud-eureka
  profiles: peer1
server:
  port: 8000
eureka:
  instance:
    hostname: peer1
  client:
    serviceUrl:
      defaultZone: http://peer2:8001/eureka/,http://peer3:8002/eureka/
---
spring:
  application:
    name: spring-cloud-eureka
  profiles: peer2
server:
  port: 8001
eureka:
  instance:
    hostname: peer2
  client:
    serviceUrl:
      defaultZone: http://peer1:8000/eureka/,http://peer3:8002/eureka/
---
spring:
  application:
    name: spring-cloud-eureka
  profiles: peer3
server:
  port: 8002
eureka:
  instance:
    hostname: peer3
  client:
    serviceUrl:
      defaultZone: http://peer1:8000/eureka/,http://peer2:8001/eureka/

Start the eureka registry with the configuration parameters of peer1, peer2, and peer3 respectively.

java -jar spring-cloud-eureka-0.0.1-SNAPSHOT.jar --spring.profiles.active=peer1
java -jar spring-cloud-eureka-0.0.1-SNAPSHOT.jar --spring.profiles.active=peer2
java -jar spring-cloud-eureka-0.0.1-SNAPSHOT.jar --spring.profiles.active=peer3

After the startup is completed in sequence, the browser input: The http://localhost:8000/rendering is as follows:

You can see the relevant information of peer2 and peer3 in peer1. So far the eureka cluster has been completed

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324834104&siteId=291194637