Construction of eureka Registration Center

Keywords: Spring Maven Apache Java

Is there a problem?

Briefly review what we have just written:

  • Service provider: a microservice that provides querying users according to their id.

  • Service consumer: a service caller who calls service provider remotely through RestTemplate.

What's the problem?

  • In consumer, we hard code the url address into the code, which is not convenient for later maintenance

  • consumer needs to remember the provider's address. If there is a change, it may not be notified, and the address will be invalid

  • consumer doesn't know the status of the provider, or the service is down

  • provider has only one service, not high availability

  • Even if provider s form clusters, consumer s need to realize load balancing by themselves

In fact, the above problems are the inevitable problems of distributed services:

  • Service management

    • How to automatically register and discover

    • How to realize state supervision

    • How to realize dynamic routing

  • How to realize load balancing for services

  • How to solve the problem of disaster recovery by service

  • How to realize unified configuration of services

All of the above questions will be answered in spring cloud.

 

Eureka registry

Understanding Eureka

First, let's solve the first problem, service management.

problem analysis

In the case just now, the service provider needs to disclose its address when providing external services. consumer needs to record the address of the service provider. If the address changes in the future, it needs to be updated in time. It doesn't matter when there are few services, but in today's increasingly complex Internet environment, a project will definitely be divided into dozens or even dozens of micro services. At this time, if you still manage the address manually, it is not only difficult to develop, but also very difficult to test, release and launch in the future, which is contrary to the idea of DevOps.

Net car

It's like before the advent of online car hailing, people can only call a taxi when they go out. Some private cars want to rent but they are not qualified. They are called black cars. Many people want to make an appointment, but there are too few taxis. There are many private cars that dare not be stopped, and the cars are full of streets, who knows which one is willing to carry people. One wants, one is willing to give, is the lack of introduction, lack of management ah.

At this time, an online car Hailing platform like didi appears. All private cars that want to carry passengers will register with Didi, and record your model (service type) and identity information (contact information). Such private cars can be found in didi at a glance.

At this time, to call for a car, just open the APP, input your destination, select the model (service type), Didi will automatically arrange a car to meet your needs in front of you, and serve you perfectly!

What does Eureka do?

Eureka is like Didi, who is responsible for managing and recording the information of service providers. Service callers don't need to find services themselves, but tell Eureka their needs, and Eureka will tell you the services that meet your needs.

At the same time, the "heartbeat" mechanism is used to monitor between the service provider and Eureka. When a service provider has problems, Eureka will naturally remove it from the service list.

This realizes the automatic registration, discovery and status monitoring of services.

Schematic diagram

Basic structure:

  • Eureka: it is the service registry (it can be a cluster) that exposes its address externally

  • Provider: register your own information (address, what service to provide) with Eureka after startup

  • Consumer: subscribe to Eureka service, Eureka will send the address list of all providers of the corresponding service to consumers, and update it regularly

  • Heartbeat (renewal): the provider periodically refreshes its status to Eureka via http

Introductory case

Building EurekaServer

Next, we create a project to start an EurekaServer:

Still use the quick build tool provided by spring:

Select dependency: EurekaServer service registry dependency, Eureka Discovery service provider and service consumer. Because, for eureka, both the service provider and the service consumer belong to the client

Complete Pom file:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.ji</groupId>
    <artifactId>eureka-server</artifactId>
    <version>1.0.0</version>
    <packaging>jar</packaging>

    <name>eureka-server</name>
    <description>Spring Cloud project</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.6.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

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

    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Finchley.SR2</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

Write application.yml configuration:

server:
  port: 10086 # port
spring:
  application:
    name: eureka-server # Application name, which will be displayed in Eureka
eureka:
  client:
    service-url: # The address of EurekaServer is now its own address. If it is a cluster, you need to add the address of other servers.
      defaultZone: http://127.0.0.1:${server.port}/eureka

Modify the boot class, and add the @ EnableEurekaServer annotation on the class:

@SpringBootApplication
@EnableEurekaServer // Declare that the current springboot application is an eureka Service Center
public class EurekaApplication {

    public static void main(String[] args) {
        SpringApplication.run(EurekaApplication .class, args);
    }
}

Start the service and access: http://127.0.0.1:10086

 

 

 

Published 1977 original articles, praised 46, visited 120000+
His message board follow

Posted by Vorotaev on Sat, 01 Feb 2020 02:13:45 -0800