Introduction to the

Eureka is a service discovery framework developed by Netflix. As a REST-based service, Eureka is mainly used to locate middle-tier services running in AWS domains for load balancing and middle-tier service failover.

SpringCloud integrates it into its sub-project spring-Cloud-Netflix to implement SpringCloud’s service discovery capabilities.

The implementation process

CAP principle:Explanation of CAP principle

Consistency, Availability, Partition tolerance cannot be achieved at the same time

  • Due to the unreliability of network communication, all P must be guaranteed.
  • C(consistency) requires that the data across our multiple servers be consistent, so we need to update the data
  • A(availability), which requires the server to return the result of A request as soon as it is received
  • Because the network is unreliable, C(consistency) is locked up during data updates, which contradicts A(availability) (no access during updates)

So in general it’s just CP and AP,

Zookeeper guarantees CP

If the master node loses contact due to a network fault, the remaining nodes select a leader node. The entire service is unavailable during the election of the Leader node

Eureka guarantees AP

Eureka nodes are equal. When a node loses contact, it automatically switches to the next node, but the information returned may not be the latest

There is a heartbeat mechanism in Eureka, that is, the service provider Server sends a heartbeat to Eureka every 30 seconds. If no heartbeat is received in 90 seconds, the Server is considered to be down and logged out. If more than 85% of the services are down, the network is faulty and the connection is maintained

Eureka registry for use

Import dependence

<dependencies>
    <! -- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-eureka -->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        <version>1.4.7. RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
</dependencies>
</project>
Copy the code

Writing configuration files there are several pieces of code in the EurekaClientConfigBean class

public static final String PREFIX = "eureka.client";
public static final String DEFAULT_URL = "http://localhost:8761" + DEFAULT_PREFIX;
public static final String DEFAULT_ZONE = "defaultZone";
private Map<String, String> serviceUrl = new HashMap<>();

	{
		this.serviceUrl.put(DEFAULT_ZONE, DEFAULT_URL);
	}

Copy the code

So you can set dfaultZone in the configuration file

server:
  port: 7001
eureka:
  instance:
    hostname: eureka1 #eureka server instance name
  client:
    register_with_eureka: false # If you want to register with the Eureka registry, false if you do not
    fetch-registry: false # indicates whether to obtain the registered service information from Eureka Server. False indicates that I am the registry. My role is to maintain the service instance and I do not need to retrieve the service
    service-url:
    # to http://eureka1:7001/eureka/ request service
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
Copy the code

Enable the Eureka function debug- @enableEurekaserver cannot be used

@EnableEurekaServer
@SpringBootApplication
public class EurekaService7001 {
    public static void main(String[] args) { SpringApplication.run(EurekaService7001.class,args); }}Copy the code

The results

Eureka service registration

  1. Add the dependent

We need to add the dependency to the POM file of the previous provider

 <dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    <version>3.0.2</version>
</dependency>
Copy the code
  1. Add the eureka configuration
eureka:
  client:
    service-url:
    Register with the registry
      defaultZone: http://eureka1:7001/eureka/
  instance:
    instance-id: providerdept-80801 # Change the default display information
Copy the code
  1. Add annotations
@SpringBootApplication
@EnableEurekaClient // Register with Eureka after startup
public class SApplication {
    public static void main(String[] args) { SpringApplication.run(SApplication.class,args); }}Copy the code

The results

In a formal scenario we tend to have multiple registries and multiple services

Each registry registers itself with other registries

server:
  port: 7001
eureka:
  instance:
    hostname: eureka1 #eureka server instance name
  client:
    register_with_eureka: false # If you want to register with the Eureka registry, false if you do not
    fetch-registry: false # indicates whether to obtain the registered service information from Eureka Server. False indicates that I am the registry. My role is to maintain the service instance and I do not need to retrieve the service
    service-url:
      defaultZone: http://eureka1:7003/eureka/,http://eureka2:7002/eureka/
Copy the code

The service provider registers with each registry

eureka:
  client:
    service-url:
      defaultZone: http://eureka1:7001/eureka/,http://eureka2:7002/eureka/,http://eureka3:7003/eureka/
  instance:
    instance-id: providerdept-80801
info:
  app.name: appname-lin
  company.name: companyname-lin
Copy the code

You need to open the hosts file on drive C, Windows, System32, drives, etc, etc. Add the configuration

127.0.0.1 eureka1
127.0.0.1 eureka2
127.0.0.1 eureka3
Copy the code

Obtaining service Information

When we click providerdept – 80801 jump to the corresponding port number under “http://xxx:xxx/actuator/info” which can show some relevant information service

Add the dependent

<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
Copy the code

Writing configuration files

eureka:
  client:
    service-url:
      defaultZone: http://eureka1:7001/eureka/
  instance:
    instance-id: providerdept-80801
Configure some information
info:
  app.name: appname-lin
  company.name: companyname-lin
Copy the code

Running results:

We can also get it through DicoveryClient

    @Autowired
    // Get specific microservice information
    DiscoveryClient client;
    @GetMapping("/dev/discovery")
    // Get the registered microservice,
    public Object discovery(a){
        // List of microservices
        List<String> services = client.getServices();
        List<ServiceInstance> instances = client.getInstances("PROVIDER-NAME");
        System.out.println("service------"+services);
        // Specific microservices
        for (ServiceInstance instance : instances){
            System.out.println(instance.getHost()+"\t"
                    +instance.getPort()+"\t"
                    +instance.getUri()+"\t"
                    +instance.getInstanceId()
            );
        }
        return this.client;
    }
Copy the code
@SpringBootApplication
@EnableEurekaClient // Register with Eureka after startup
@EnableDiscoveryClient// Get service information
public class SApplication {
    public static void main(String[] args) { SpringApplication.run(SApplication.class,args); }}Copy the code

debug

The @enableeurekaserver annotation is not available

Import dependency error

Original dependence:

<dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-eureka</artifactId>
        <version>1.4.7. RELEASE</version>
    </dependency>
Copy the code

Correct dependency

<dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
    </dependency>
Copy the code