[Eureka] SpringCloud service registration and discovery

What is 1. Eureka

  Eureka is a sub-module of Netflix, is one of the core modules. Rest-based Eureka is a service for location services, the intermediate layer in order to achieve the cloud service discovery and failover. Service registration and discovery is very important for micro-services architecture, the service has been found and registered, just use the identifier of the service, you can access to the service without the need to modify the configuration file of a service call. Similar to the registry zookeeper Dubbo


2. Principle

  Eureka using the CS design architecture, has two components: Eureka Server and Client Eureka
  Eureka Server: provide service registry services, namely as a service function registration server, service registry, and other micro-system, customer service, use of Eureka end Eureka Client to connect to Eureka Server and maintain a heartbeat connection, maintenance personnel can such a system to monitor each micro system service is functioning properly by Eureka Server. Spring Cloud other modules (e.g. Zuul) Eureka Server can be found by other services in the system, and associated logic
  Eureka Client: is a java client, a simplified Eureka server interaction, the local equivalent of our program services, clients also have a built-in load polling algorithm load balancer, after starting the application, will send a heartbeat to the Eureka Server (default period is 30 seconds), if a plurality of Eureka Server heartbeat is not received within a period to beat a node, Eureka Server will remove this service node (default 90 seconds) from the service registry


3. Self-protection mechanism

  By default, if EurekaServer within a certain time (default 90 seconds) does not receive the heartbeat a widget service instance, the cancellation will EurekaServer instance. But when the network partition failure, between micro and EurekaServer service can not communicate, above behaviors may become very dangerous, because the service itself is actually a micro health, then this should not be written off this micro-services. Eureka solved by "limp home mode" this question: When EurekaServer node losing too much in a short time a client (network partition failure may have occurred), then the node goes into self-protection mode, Once in this mode, EurekaServer service will protect the information in the registry, no longer deletes the service data in the registry (ie not written off any micro-services). When the network fault recovery (i.e., the heart rate is restored to be above a threshold), the node will automatically exit EurekaServer self-protection mode. Its design philosophy is: rather reserved error of service registration information, do not blindly write off any possible health service instance. Live Is Better Than alive.


4. project combat

4.1 Eureka server

  1. Add rely Eureka

     <dependency>
    	<groupId>org.springframework.cloud</groupId>
    	<artifactId>spring-cloud-starter-eureka-server</artifactId>
     </dependency>
    
  2. Master Boot class above, add notes about Eureka

    @SpringBootApplication
    @EnableEurekaServer // EurekaServer服务器端启动类,接受其它微服务注册进来
    public class EurekaServer7001_App
    {
    	public static void main(String[] args)
    	{
    		SpringApplication.run(EurekaServer7001_App.class, args);
    	}
    }
    
  3. Profiles

    server: 
      port: 7001  // eureka server 端口号
     
    eureka: 
      instance:
        hostname: eureka7001.com  #eureka服务端的实例名称
      client: 
        register-with-eureka: false     #false表示不向注册中心注册自己。
        fetch-registry: false     #false表示自己端就是注册中心,我的职责就是维护服务实例,并不需要去检索服务
        service-url: 
          #单机 defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/       #设置与Eureka Server交互的地址查询服务和注册服务都需要依赖这个地址(单机)。
          defaultZone: http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/  // 集群
    

  This eureka server to build a successful, other services can be registered (is not that simple)

4.2 Eureka Client

  1. Add dependent

      <dependency>
    	  <groupId>org.springframework.cloud</groupId>
    	   <artifactId>spring-cloud-starter-eureka</artifactId>
      </dependency>
      <dependency>
    	   	<groupId>org.springframework.cloud</groupId>
    	   	<artifactId>spring-cloud-starter-config</artifactId>
       </dependency>
    
  2. Configuring startup items, add @EnableEurekaClient on the main register to start on the Eureka

    @SpringBootApplication
    @EnableEurekaClient //本服务启动后会自动注册进eureka服务中
    @EnableDiscoveryClient //服务发现
    public class DeptProvider8001_App
    {
    	public static void main(String[] args)
    	{
    		SpringApplication.run(DeptProvider8001_App.class, args);
    	}
    }
    
  3. Profiles

    eureka:
      client: #客户端注册进eureka服务列表内
        service-url: 
          #defaultZone: http://localhost:7001/eureka
           defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/      
      instance:
        instance-id: microservicecloud-dept8001
        prefer-ip-address: true     #访问路径可以显示IP地址     
    

 This will expose this service to register on the Eureka

5. Eureka and the difference zookeeper

The difference between the former first two concepts:

The traditional ACID are:

  1. A (Atomicity) atomicity
  2. C (Consistency) consistency
  3. I (Isolation) independence
  4. D (Durability) Persistence

CAP means:

  1. C (Consistency) strong consistency
  2. A (Availability) Availability
  3. P (Partition tolerance) partitions fault tolerance
    CAP can only guarantee two points, can not meet at the same time

5.1 Zookeeper ensure that CP

  When the query service list to the registry, the registry can tolerate returned a few minutes before registration information, but can not accept the services directly down out is not available. That Service registration requirements for availability than consistency , but zk be a situation where, when the master node with other nodes because the network lost contact failure, the remaining nodes will be re-election leader, but the leader election time is too long , 30 ~ 120s, and the entire cluster zk are not available during the elections, which led to paralysis during the electoral registration service, under the cloud deployment environment, due to network problems led to the loss of master zk cluster node is greater probability will happen thing, although the service will eventually recover, but the timing of the elections led to the registration of long-term use is not tolerated

5.2 Eureka ensure that AP

  Priority to ensuring the availability. Eureka each node are equal , hang a few nodes will not affect the normal node, the remaining nodes can still provide registration and inquiry services. The Eureka Eureka clients registered with the connection failure is found, it will automatically switch to the other nodes, as long as there is still a Eureka, we can guarantee registration services are available (to ensure availability) , but queried the information may not be current ( It does not guarantee strong consistency) . In addition, there is a self-protection mechanism Eureka, if more than 85% in 15 minutes nodes are not normal heartbeat, then the client is considered Eureka and registration center of a network failure, the following appears Happening:

  1. Eureka no longer be removed for a long time and should not receive a heartbeat expired registration service from the list
  2. Eureka is still able to accept registration and query requests for new services, but will not be synchronized to (that is, to ensure that the current node is still available) on other nodes
  3. When the network is stable, the current instance of the new registration information will be synchronized to the other nodes

Therefore, Eureka can be very good to deal with network failures lead to some nodes lost contact, but not as paralysis of the entire registration services like zookeeper

Guess you like

Origin blog.csdn.net/wrs120/article/details/91470223