Spring Cloud-EureKa registration and found that the basic concepts and implementation services

0.5 development environment and rely mainly on

System : macOS Mojave 10.14.4
JDK : 1.8.0_202
Spring Cloud :Greenwich.SR1
Spring Boot : 2.1.4.RElEASE
IDE : IntelliJ IDEA 2019.1 Ultimate Edition

1. Eureka related concepts

1.1 What is Eureka

Eureka is produced Netflix tool for implementing service registration and discovery. Spring Cloud integrates Eureka, and provides support out of the box. Which, Eureka Eureka Server and can be subdivided into Eureka Client. If used dubbo students might say Zookeeper and the role of the same. Yes, in Spring Cloud ecology and its role is similar to Zookeeper.

1.2 CAP principles have to say

CAP principle, also known as CAP theorem means that in a distributed system Consistency (consistency), Availability (availability), Partition tolerance (partitions fault tolerance) , the three can not have both.
Due to the current network hardware there will be delays and packet loss problems, so we must partition tolerance need to achieve. Now there have been two cases AP and CP.

  • Zookeeper is CP
    when the tracking list to the registry, the registry can tolerate returned a few minutes before registration information, but can not accept the services directly down out is not available. That is, the Service registration requirements for availability than consistency. But zk is there such a situation, when the leader can not connect to other nodes or hang up, the remaining nodes will be leader election. The leader of the electoral time is too long, usually require tens of seconds, the entire cluster ZK are not available when the election , which led to paralysis during the electoral registration services. Registration election time caused by long-term use is not tolerated.
  • AP Eureka is implemented
    Eureka each node are equal, hang a few nodes will not affect the normal node, the remaining nodes can still provide registration and inquiry services. The Eureka client when registering with the Eureka or if you find a connection fails, it will automatically switch to the other nodes, as long as there is still a Eureka, we can guarantee registration services are available ( to ensure availability ), but found the information It may not be current ( not guarantee strong consistency ). In addition, there is a self-protection mechanism Eureka, if more than 85% in 15 minutes nodes are not normal heartbeat, then the client is considered Eureka and registration center of a network failure, the following appears Happening:
    1. Eureka no longer remove because for a long time and should not receive heartbeat expired registration service from the list
    2. Eureka is still able to accept registration and query requests for new services, but will not be synchronized to (that is, to ensure that the current node is still available) on other nodes
    3. When the network is stable, the current instance of the new registration information will be synchronized to the other nodes

1.3 Spring Cloud Eureka

  • Spring Cloud Eureka part of it is based Netflix Eureka package, integrated into the Spring Could ecology. (Spring Cloud give my personal opinion is the best open source projects integrated package form micro Services Architecture technology solutions start with a simple and easy excellent)
  • Spring Cloud Eureka has two components:
    1. Eureka Server registry services
    2. Eureka Client-side service implementation

2. Eureka Server combat

Stand-alone Eureka Server 2.1

  • Github : https://github.com/ilssio/eureka
  • Directly on the map
    Here Insert Picture DescriptionHere Insert Picture Description
    Here Insert Picture Description
    Here Insert Picture Description
  • After the Finish Maven will download the dependencies, you have to be patient, if the version inconsistencies students may have a look-dependent version of Maven I modify the files inside.
  • About version of the problem and Boot Spring Cloud, you can go https://spring.io/projects/spring-cloud look, there are the following correspondence.
    Here Insert Picture Description
  • pom.xml
<?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>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.4.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>io.ilss</groupId>
    <artifactId>eureka</artifactId>
    <version>1.0</version>
    <name>eureka</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Greenwich.SR1</spring-cloud.version>
    </properties>

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

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

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</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>
  • Notes @EnableEurekaServer increase
package io.ilss.eureka;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

@SpringBootApplication
@EnableEurekaServer
public class EurekaApplication {

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

}
  • Then start:

Here Insert Picture Description

  • After the start will always report the above error first whether direct access http: // localhost: 8080 / ports appear on this page represents a success.
    Here Insert Picture Description
  • Something about error:
    The error occurs because the end is a EurekaServer Client-side. He is the error message Can not execute request on any known server , the request can not be executed on any known service. We might as well put him on the register to the current project Server
  • application.yml (I was using yml configuration file, if you are using your own properties converted into xx.xx.xx = xxx)
eureka:
  instance:
    hostname: localhost
  client:
    #  是否将自己注册到Eureka Server,默认为true。 如果不想注册到server 设置为false
    register-with-eureka: false
    # 是否从Eureka Server获取注册信息,默认为true。
    serviceUrl:
      # service-url 是一个Map 大家不要直接陪在service-url后面会报错
      defaultZone: http://localhost:8761/eureka/
      # 具体的大家可以去看下 EurekaClientConfigBean 可以按住CMD然后点击service-url(windows应该是ctrl)

server:
  # 应用端口 Eureka默认端口是8761 我们就用8761吧
  port: 8761
spring:
  application:
    # 应用名称
    name: eurekaserver

  • Start again you can see eurekaserver registered to the current project
  • 另外提示一下,再次启动项目还是会包报错Cannot execute request on any known server 这是因为Server端还没有启动成功,Client注册不上去。Eureka是采用的心跳机制的,如果没注册上去会不断的报错。所以等Server端完全启动成功过后Client会自动注册上去 报错也就不会再出现。
    Here Insert Picture Description

2.1 Eureka Client 端

package io.ilss.client;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

@SpringBootApplication
@EnableEurekaClient
public class ClientApplication {

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

}
  • 修改applicateion.yml文件
eureka:
  instance:
    hostname: localhost
  client:
    serviceUrl:
      # service-url 是一个Map 大家不要直接陪在service-url后面会报错
      defaultZone: http://localhost:8761/eureka/
spring:
  application:
    name: client
server:
  port: 8080
  • 然后启动? 启动不了的!!!因为前面依赖只添加了eureka-client 依赖 启动过后 会自动结束 具体是为什么?自己百度去。我是加上了web组件的依赖pom文件如下:
<?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>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.4.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>io.ilss</groupId>
    <artifactId>client</artifactId>
    <version>1.0</version>
    <name>client</name>
    <description>client</description>

    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Greenwich.SR1</spring-cloud.version>
    </properties>

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

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

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

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</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>
  • 然后启动!
    Here Insert Picture Description
    成功注册!

3. Eureka的集群

3.1 2个Server的集群

  1. 打开配置
    Here Insert Picture Description
  • 配置参数 第一个
    Here Insert Picture Description
  • 配置参数 第二个
    Here Insert Picture Description
  • 修改yml 注释掉端口 修改注册的server端口为8762 然后启动EurekaApplication1
eureka:
  instance:
    hostname: localhost
  client:
#    #  是否将自己注册到Eureka Server,默认为true。 如果不想注册到server 设置为false
#    register-with-eureka: false
#    # 是否从Eureka Server获取注册信息,默认为true。
#    fetch-registry: false
    serviceUrl:
      # service-url 是一个Map 大家不要直接陪在service-url后面会报错
      defaultZone: http://localhost:8762/eureka/
      # 具体的大家可以去看下 EurekaClientConfigBean 可以按住CMD然后点击service-url(windows应该是ctrl)


#server:
#  # 应用端口 Eureka默认端口是8761 我们就用8761吧
#  port: 8761
spring:
  application:
    # 应用名称
    name: eurekaserver
  • 修改yml 注释掉端口 修改注册的server端口为8761 然后启动EurekaApplication2
eureka:
  instance:
    hostname: localhost
  client:
#    #  是否将自己注册到Eureka Server,默认为true。 如果不想注册到server 设置为false
#    register-with-eureka: false
#    # 是否从Eureka Server获取注册信息,默认为true。
#    fetch-registry: false
    serviceUrl:
      # service-url 是一个Map 大家不要直接陪在service-url后面会报错
      defaultZone: http://localhost:8761/eureka/
      # 具体的大家可以去看下 EurekaClientConfigBean 可以按住CMD然后点击service-url(windows应该是ctrl)
      
#server:
#  # 应用端口 Eureka默认端口是8761 我们就用8761吧
#  port: 8761
spring:
  application:
    # 应用名称
    name: eurekaserver
  • 此时如果你的client端没有关闭那么你可以在8762端看到如下:
    Here Insert Picture Description
  • 已注册的Client数据会同步到 集群的其他节点! 如果8761挂了,这个时候8762还是会有Client的数据,但是,如果8761挂了后,Client再启动,就注册不上去了,这个时候就该修改Client的注册地址了:改为
    defaultZone: http://localhost:8761/eureka/,http://localhost:8762/eureka/
    这样就保证 不管哪个节点挂了,都可以注册成功了。

3.2 3-N个Server的集群

  • For more clusters set up, you can own hands, how to do?
    We have not found in the two Server, Eureka is a mutual discovery. Cluster formation. Meaning that if 3 we can find twenty-two form a ring, the equivalent of my friend told his friend of my existence. Then a pass a. . . . . Then Client end will be accompanied
    we can dry:
    Here Insert Picture Description
  • Then try this!
Published 34 original articles · won praise 7 · views 8168

Guess you like

Origin blog.csdn.net/ilo114/article/details/89606737