Springcloud service registration and discovery Eureka

In Springcloudthe project, the service provider to provide services, consumer services, consumer services, if a lot of micro-services, you need a service management center to unify management services, and consumers when calling the service can go to the Administration Center to find We need to call a service call.
EurekaIn the Springcloudassembly as the most commonly used services on registration and discovery, the following major record about Eurekathe basic principles and methods of use.

What is Eureka

EurekaIs Netflixone of the core module, which uses CS architecture, based on the principle AP CAP theory, it is a high availability service registry for service discovery and failover.

Eureka architecture

Eureka Chart
Eureka principle summarized as follows:

  1. Eureka ServerAfter starting, when the micro service client ( Eureka Client/ Application Clienttime) starts, based on the configuration information of the service (service host IP, port, service name, etc.) is registered Eureka Serverin;

  2. When Eureka client ( Eureka Client/ Application Clienttime) starts, based on the configuration information of the service (service host IP, port, service name, etc.) is registered Eureka Serverin;

  3. Eureka Server to Eureka Eureka Server cluster each additional node synchronization service information through the Replicate replication;

  4. When the service consumer calls the micro-services, go to Eureka Server to find the services available;

  5. Eureka Server service by finding available information, and returned to Eureka Client call.

Eureka's self-protection mechanism

After Eureka Server starts, Eureka Client every 30s will be sent to Eureka Server heartbeat to indicate their availability status, if the heartbeat within a specified period (default 90s), did not receive a Client's heart, resulting in server did not receive the reason client heartbeat to be divided into two types:

  1. The Client has indeed down, and can not provide the service, this time Eureka Server reasonably should log out and delete the information in the registry of the Client;
  2. The Client service itself is normal, but due to network partitions leads not receive the Client's heartbeat collection Server, Client at this time if registration information deleted, it would be unreasonable and dangerous.

Based on the above two points, Eureka Server can not determine the heartbeat is not received due to the Client which causes, so Eureka Server will start the self-protection mechanism: no longer delete any registration and cancellation of micro-information services, even if the service is not really use. When the network returned to normal (Client can send all the normal heartbeat), Eureka Server will launch a self-protection.

Another self-protection mechanism of Eureka: When more than 15 minutes does not receive the 85% Client heartbeat, EurekaServer think network partition error, then:

  1. Not delete any registration information for a long time did not send heartbeat expired Client from the registration center;
  2. If the received new Client heartbeat, no longer synchronized nodes;
  3. If the network returned to normal (normal heartbeat received Client), the synchronization node information.

Eureka High Availability

Distributed database system meets the CAP theorem, due to network reasons partitions, P (Partition Tolerance partitions fault tolerance) is guaranteed and must be addressed, C (Consistant strong consistency) and A (Avalibale availability) can not be satisfied at the same time, it is necessary needed trade-off between the two. Eureka AP principle is implemented to ensure the high availability cluster.
Eureka each node in the cluster are equal, there is no master-slave (master and slave) relationship, after which a node is down, other nodes are not affected, continue to provide services, Eureka the load balancer will look other available nodes continue to provide services, unless all the nodes are unavailable.

The difference between Eureka and Zookeeper

Zookeeper CP is based on the principle of implementation, the cluster using master-slave mode, when the master node fails, the other slave nodes will be new mater election, the electoral process in general 30s-120s, during which time the entire cluster It is not available.
Eureka is achieved based on the principle of AP with high availability.

Eureka project in Springcloud

1. Eureka Server build

  1. pom.xml:
		<dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka-server</artifactId>
        </dependency>
  1. application.yml:
server:
	port: 7010
eureka:
  instance:
    hostname: eureka7001.com  		#服务主机名
  client:
    register-with-eureka: false		#不把自己注册进注册中心
    fetch-registry: false			#发现服务时不用检查自己
    service-url:
    	# 单机时只需指定自己
    	# defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka   
    	 #集群中不必指定自己,只需指定其他的节点的访问地址 ,中间用逗号分隔
      	defaultZone: http://eureka7002.com:7002/eureka,http://eureka7003.com:7003/eureka  

  1. Master Boot class to add annotations to enable EurekaServer
@SpringBootApplication
@EnableEurekaServer             // 启用EurekaServer
public class App {
    public static void main( String[] args )  {
        SpringApplication.run(App.class, args);
    }
}
  1. Access address configured your browser, you can access the registry page Eureka
    Eureka backend interface

Construction of Eureka Client

  1. pom.xml
<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>
  1. application.yml
eureka:
  client:
    service-url:
      defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka,http://eureka7003.com:7003/eureka
  instance:
    appname: aitao-sevice-usercenter8003    
    prefer-ip-address: true  # 可以显示微服务主机ip
  1. Start class to add @EnableEurekaClientcomments.
Released eight original articles · won praise 3 · Views 843

Guess you like

Origin blog.csdn.net/weixin_40203134/article/details/87941034