springcloud-Micro-Service registration and Discovery Eureka

Source: Internet
Author: User

I. Introduction of Springcloud

Spring Cloud is an ordered collection of frames. It uses spring boot's development convenience to subtly simplify the development of distributed system infrastructures, such as service discovery registration, Configuration center, message bus, load balancer, circuit breakers, data monitoring, and so on, all of which can be started and deployed with spring boot development style. Spring Cloud does not reinvent the wheel, it simply combines the more mature, proven service frameworks currently developed by companies, masking the complexities of configuration and implementation through the spring boot style, and finally gives developers a set of easy-to-understand, Easy-to-deploy and maintainable Distributed system Development Kit.

Second, micro-service registration and Discovery-eureka

  Eureka is the Netflix Open Source Service discovery component, which itself is a rest-based service that contains both the server and the client, and integrates it into the springcloud to realize the discovery and registration of micro-service;
-Eureka's Github:https://github.com/netflix/eureka
  


Eureka Server
-Service side-no storage, memory retention, each service instance needs to send heartbeat to renew
-Client-Caches the Eureka registration information in memory, so you do not have to find the service every request to Eureka
-Eureka will do the registration service synchronization, so that the state is consistent, the client only need to access a Eureka
Service Provider
-Register (service registration), Renew (service renewal), Cancel (service downline) and other operations to Eureka Server
Service Consumer
-a list of registered services is obtained from Eureka Server, and consumer services

Three, the simple combat 1, the writing service end (Springcloud uses the Edgware SR3 version)

Add the following dependencies to the Pom.xml:

<parent>    <groupId>org.springframework.boot</groupId>    <artifactId> spring-boot-starter-parent</artifactid>    <version>1.5.13.RELEASE</version>  </ parent>  <dependencies>    <dependency>      <groupid>org.springframework.cloud</ groupid>      <artifactId>spring-cloud-starter-eureka-server</artifactId>    </dependency >  </dependencies>  <dependencyManagement>    <dependencies>      <dependency >        <groupId>org.springframework.cloud</groupId>        <artifactId> spring-cloud-dependencies</artifactid>        <version>Edgware.SR3</version>        <type> pom</type>        <scope>import</scope>      </dependency>    </ Dependencies>  </dependencyManagement>

Add the following configuration to the configuration file Application.xml:

server.port=8781#表示是否将自己注册在EurekaServer上, which is true by default. Because the current application is Eurekaserver, set to Falseeureka.client.register-with-eureka=false# Represents whether to obtain registration information from Eurekaserver, which is true by default. Single node does not need to synchronize the data of other Eurekaserver nodes Eureka.client.fetch-registry=false# Set Eureka address Eureka.client.service-url.defaultzone=http://Localhost:8781/eureka

Add the Startup class, plus the start Eureka annotations:

@SpringBootApplication @enableeurekaserver  Public class Application {    publicstaticvoid  main (string[] args) {        Springapplication.run (Application. class , args);}    }

To start the project, open http://localhost:8781/, as follows:

2. Write the client (the version used by Springcloud is consistent with the server)

Add Dependencies in Pom.xml:

<parent>    <groupId>org.springframework.boot</groupId>    <artifactId> spring-boot-parent</artifactid>    <version>1.5.13.RELEASE</version>  </parent>  <dependencies>    <dependency>      <groupId>org.springframework.cloud</groupId>      <artifactId>spring-cloud-starter-eureka</artifactId>    </dependency>  </ dependencies>  <dependencyManagement>    <dependencies>      <dependency>        < Groupid>org.springframework.cloud</groupid>        <artifactid>spring-cloud-dependencies</ artifactid>        <version>Edgware.SR3</version>        <type>pom</type>        < scope>Import</scope>      </dependency>    </dependencies>  </ Dependencymanagement>

In configuration file Application.xml, join the configuration to inject the URL of the configuration center:

server.port=8782#对该应用另起别名spring. Application.Name=client-8782#注入目标, configure the service center URL, Consistent with server-side configuration Eureka.client.service-url.defaultzone=http://localhost:8781/eureka/

Create the client startup class Clientapplication.java and join the client to be found annotated:

@SpringBootApplication @enablediscoveryclient  Public class clientapplication {    publicstaticvoid  main (string[] args) {        Springapplication.run (clientapplication. class , args);}    }

Launch the app, then open the previous Eureka registration page http://localhost:8781/, as follows:

Discover that the client you just started is already registered with the server.

When I shut down the client, I opened the Eureka registration page again and found a string of scarlet letters:

emergency! EUREKA may incorrectly claiming INSTANCES is up when the they ' RE not. Renewals is LESSER THAN THRESHOLD and HENCE the INSTANCES is not BEING EXPIRED JUST to be SAFE.

This is because Eureka has entered a self-protection mechanism, and by default, if Eurekaserver does not receive a heartbeat for a microservices instance for a certain amount of time, Eurekaserver will log off the instance (default 90s). But when the network fails, the microservices and eurekaserver can not communicate, so it will be very dangerous, because the microservices itself is very healthy, at this time should not unregister the microservices, and Eureka through the self-protection mechanism to prevent this situation, when the network health, The Eurekaserver node will automatically exit the self-protection mode;

When the client microservices are started again, the Refresh Service registry will find that the self-protection status has been canceled.

In summary, we can see the Eureka of the two components Eurekaserver and eurekaclient role:

    1. Eurekaserver provides the ability of service discovery, when each micro-service starts, it will register its own information to Eurekaserver (for example: IP, port, micro-service name, etc.), Eurekaserver will store this information;
    2. Eurekaclient is a Java client that simplifies the interaction with eurekaserver;
    3. After the micro-service Qidong, it will periodically (default 30s) send the heartbeat to the Eurekaserver to renew their "lease period";
    4. If Eurekaserver does not receive the heartbeat of a microservices instance for a certain amount of time, Eurekaserver will unregister the instance (default 90s);
    5. By default, Eurekaserver is also eurekaclient. Multiple Eurekaserver instances are replicated to each other to achieve the synchronization of data in the service registry;
    6. Eurekaclient also caches the information in the service registry;

In conclusion, Eureka improves the flexibility, scalability and usability of the system through the mechanism of heartbeat checking and client-side caching, so as a micro-service architecture, a service registration center is needed to manage the service.

3, Eureka High Availability

Copy the above server project, named: Eureka-keepalived-server-1,eureka-keepalived-server2;

Copy the above client project, named: eureka-keepalived-client;

To modify the Hosts file:

127.0.0.1    localhost Server1 server2

Modify the Application.properties configuration file (Note that you need to comment out Eureka.client.register-with-eureka, eureka.client.fetch-registry):

Configuration file for Eureka-keepalived-server-1:

server.port=8781spring.application.name=eureka-server-1spring.profiles.active=server1# Eureka.client.register-with-eureka=false#eureka. Client.fetch-registry=false #指定主机名eureka. Instance.hostname=server1# Sets the address of the eurekaserver and registers itselfwith the Server2eureka.client.service- Url.defaultzone=http://Server2:8782/eureka

Configuration file for Eureka-keepalived-server-2:

server.port=8782spring.application.name=eureka-server-2#指定hostnamespring. profiles.active= server2#eureka.client.register-with-eureka=false#eureka. Client.fetch-registry=  False#指定主机名eureka. Instance.hostname=server2# Set the address of the Eurekaserver, Register yourself on Server2 eureka.client.service-url.defaultzone=http://Server1:8781/eureka

Configuration file for Eureka-keepalived-client:

server.port=8783#对该应用另起别名spring. Application.Name=client-8783#注入目标, configure the service center URL, consistent with the server-side configuration, where you can write multiple "," delimited, such as http://server1:8781/eureka/,http://server2: 8782/eureka/ Eureka.client.service-url.defaultzone=http://server1:8781/eureka/

Launch two server Server2, Server1, Access, server1:8781/,server2:8782/, respectively, as follows:

Starting the client, we found that the client has the corresponding registration on the two servers, and our client actually only specify one server;

At this point, completed the high availability of Eureka;

Code: Https://gitee.com/lfalex/springcloud-example.git

Reference book: "Springcloud and Docker Service architecture combat" Zhou Li

springcloud-Micro-Service registration and Discovery Eureka

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.