Registration and discovery of Spring Cloud Services (Eureka)

Source: Internet
Author: User

Eureka Service Registration Center One, Eureka Server

Eureka Server is the registry of services, which is the basis for distributed services, let's see how this section is built.

First of all, Spring Cloud is based on spring boot, so our project is the Spring boot project. You need to introduce the most basic Spring boot jar package, as follows:

<dependencies>    <dependency>        <groupId>org.springframework.boot</groupId>        <artifactId>spring-boot-starter-web</artifactId>    </dependency>    <dependency>        <groupId>org.springframework.boot</groupId>        <artifactId> Spring-boot-starter-actuator</artifactid>    </dependency></dependencies>

  

Then, introduce the Eureka server's jar package:

<dependency>    <groupId>org.springframework.cloud</groupId>    <artifactId> Spring-cloud-starter-netflix-eureka-server</artifactid></dependency>

  

Next we write the Startup class:

@SpringBootApplication @enableeurekaserverpublic class Application {public    static void Main (string[] args) {        Springapplication.run (Application.class,args);    }}

  

Which @EnableEurekaServer marks this service as a Eureka Server.

Here's the most important application.yml configuration, let's start with the simplest single-case pattern.

Single-Case mode

In this mode, Eureka server is a single point, and if a failure occurs, the entire registry is not available and applies only to the test environment, as follows:

Spring:  Profiles:standaloneeureka:  instance:    hostname:localhost  client:    service-url:      defaultzone:http://localhost:8761    fetch-registry:false    register-with-eureka:falseserver:  port:8761

  

In singleton mode, we turn off the behavior of the client. One fetch-registry is to crawl the registered service, which register-with-eureka is to register itself with the other Eureka Server. These two items must be turned on when the cluster is configured so that the registered service is synchronized to the other nodes.

In singleton mode, eureka.instance.hostname must be localhost, and can defaultZone not use IP, to use eureka.instance.hostname and take the domain name resolution. Here we configure localhost and do not need to modify the Hosts file. This piece does not know why Spring Cloud is designed to do so, the small pieces in the building of the cluster is a long time to toss. We start the service, visit http://localhost:8761, and we can see the Eureka Administration page.

Cluster mode

We can implement the cluster by creating multiple Eureka server instances and having them register with each other. Mutual registration is what we mentioned earlier fetch-registry and register-with-eureka , they are true by default, so there is no need to configure, we need to set the URL of the other nodes can be, we take 3 nodes as an example:

Spring:  application:    name:eureka-server---Spring:  profiles:peer1eureka:  instance:    hostname : Peer1  Client:    service-url:      defaultzone:http://peer2:9200/eureka/,http://peer3:9300/eureka/server:  port:9100---Spring:  profiles:peer2eureka:  instance:    hostname:peer2  Client:    Service-url:      defaultzone:http://peer1:9100/eureka/,http://peer3:9300/eureka/server:  port:9200--- Spring:  Profiles:peer3eureka:  instance:    hostname:peer3  client:    service-url:      Defaultzone:http://peer1:9100/eureka/,http://peer2:9200/eureka/server:  port:9300

  

We have 3 instances on a single machine, Peer1, Peer2, Peer3, ports: 9100, 9200, 9300, respectively. Here we still have to pay attention to instance.hostname andservice-url

    • 3 instances of Instance.hostname cannot be duplicated, or cluster build fails
    • Service-url cannot be accessed directly using the ip+ port, otherwise it will fail

In the case of single machine, we need to configure hosts to parse Peer1, Peer2, Peer3

127.0.0.1 peer1127.0.0.1 peer2127.0.0.1 Peer3

  

Cluster Build Success:

DS replicas displays two replica nodes, Available-replicas displays two available replica nodes.

If on the real physical machine, we can not configure the hosts by the way is also possible, remember that this is the real physical machine, the IP of each machine is not the same. The configuration is as follows:

Spring:  application:    name:eureka-server---Spring:  profiles:peer1eureka:  instance:    Prefer-ip-address:true  Client:    service-url:      defaultzone:http://ip2:9200/eureka/,http://ip3:9300/ Eureka/server:  port:9100---Spring:  profiles:peer2eureka:  instance:    hostname: Peer2prefer-ip-address:true  Client:    service-url:      DEFAULTZONE:HTTP://IP1:9100/EUREKA/,HTTP://IP3 : 9300/eureka/server:  port:9200---Spring:  profiles:peer3eureka:  instance:    prefer-ip-address : True  client:    service-url:      defaultzone:http://ip1:9100/eureka/,http://ip2:9200/eureka/server:  port:9300

  

The name of the instance can be registered using IP, and of course each IP is different and will not be duplicated and will not cause a failure.

At this point, our Eureka server is finished, refer to GitHub address: Https://github.com/liubo-tech/spring-cloud-eureka

Registration and discovery of Spring Cloud Services (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.