Eureka build a service registry

Eureka Introduction

Eureka is an open source Netflix company's products, it is based on REST (Representational State Transfer) services, mainly for the AWS cloud. Eureka provides a complete Service Registry Service Discovery and realization, Spring is one of the most important components of the core Cloud system.

In simple terms, Eureka is an open source Netflix to provide service registry and discovery products, and provides a Java client. Of course, after the Spring Cloud Eureka efforts to improve, not just for the AWS cloud already, but can be applied in any scenario requires the use of registry.

Spring Cloud encapsulates Netflix Eureka module developed to optimize some configuration on the basis of Eureka, for some less rational logic is optimized, and provides a visual interface for easy viewing operational status of the service.

Eureka consists of two components: Eureka Eureka server and client. Eureka is the registry server. Eureka is a client java client, to simplify the interaction with the server, the load balancer as polling, to provide failover for the service and support.

Here Insert Picture Description

  • Eureka Server, assume the role of registry, providing registration and service discovery
  • Service Provider, service provider, the service itself is registered to Eureka Server, at the same time to check the operation status of the service through the heart
  • Service Consumer, service caller, obtain a list of registered services from Eureka, address and then find the corresponding service call

Build Eureka Server

Eureka was developed by Java language is made, Spring Cloud Spring Boot using the technique of Eureka is encapsulated. So its deployment is very simple, we only need to introduce the project Eureka corresponding Starter Pack

Establish Project

Here Insert Picture Description

Parent 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>
    <modules>
        <module>eureka-server01</module>
        <module>eureka-server02</module>
        <module>eureka-server03</module>
    </modules>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.5.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.ghgcn</groupId>
    <artifactId>cloud</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>cloud</name>
    <packaging>pom</packaging>
    <description>Demo project for Spring Boot</description>

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

    <dependencies>


        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </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>

Sub-module 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">
    <parent>
        <artifactId>cloud</artifactId>
        <groupId>com.ghgcn</groupId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>eureka-server01</artifactId>


    <dependencies>
        <!--注册中心-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>
    </dependencies>
</project>

Startup class

Here Insert Picture Description
@EnableEurekaServer
Notes, open the registry service discovery.

  • application.properties
#项目名称
spring.application.name=eureka serve
#项目端口
server.port=8001
#表示是否将自己注册到 Eureka Server,默认为 true
eureka.instance.hostname=eureka01
#表示是否将自己注册到 Eureka Server,默认为 true
eureka.client.register-with-eureka=true
#表示是否将自己注册到 Eureka Server,默认为 true
eureka.client.fetch-registry=true

Clusters

On the basis of a single example of Eureka on the above, to replicate the three were named as: eureka01, eureka02, eureka03, eureka04 three sample projects, using the example of these three projects to build Eureka Server clusters.

  • eureka01
#项目名称
spring.application.name=eureka serve
#项目端口
server.port=8001
#表示是否将自己注册到 Eureka Server,默认为 true
eureka.instance.hostname=eureka01
#集群
eureka.client.serviceUrl.defaultZone=http://eureka02:8002/eureka,http://eureka03:8003/eureka,http://eureka04:8004/eureka
#表示是否将自己注册到 Eureka Server,默认为 true
eureka.client.register-with-eureka=true
#表示是否将自己注册到 Eureka Server,默认为 true
eureka.client.fetch-registry=true


  • eureka02
#项目名称
spring.application.name=eureka serve
#项目端口
server.port=8002
#表示是否将自己注册到 Eureka Server,默认为 true
eureka.instance.hostname=eureka02
#集群
eureka.client.serviceUrl.defaultZone=http://eureka01:8001/eureka,http://eureka03:8003/eureka,http://eureka04:8004/eureka
#表示是否将自己注册到 Eureka Server,默认为 true
eureka.client.register-with-eureka=true
#表示是否将自己注册到 Eureka Server,默认为 true
eureka.client.fetch-registry=true



  • eureka03
#项目名称
spring.application.name=eureka serve
#项目端口
server.port=8003
#表示是否将自己注册到 Eureka Server,默认为 true
eureka.instance.hostname=eureka03
#集群
eureka.client.serviceUrl.defaultZone=http://eureka01:8001/eureka,http://eureka02:8002/eureka,http://eureka04:8004/eureka
#表示是否将自己注册到 Eureka Server,默认为 true
eureka.client.register-with-eureka=true
#表示是否将自己注册到 Eureka Server,默认为 true
eureka.client.fetch-registry=true



  • eureka04
#项目名称
spring.application.name=eureka serve
#项目端口
server.port=8003
#表示是否将自己注册到 Eureka Server,默认为 true
eureka.instance.hostname=eureka04
#集群
eureka.client.serviceUrl.defaultZone=http://eureka01:8001/eureka,http://eureka02:8002/eureka,http://eureka03:8003/eureka
#表示是否将自己注册到 Eureka Server,默认为 true
eureka.client.register-with-eureka=true
#表示是否将自己注册到 Eureka Server,默认为 true
eureka.client.fetch-registry=true
  • hosts file configuration
    needs of the local host configuration information. Windows in the
    C: \ Windows \ System32 \ drivers \ etc hosts \
    end or Linux / etc / hosts file to add the following information:
127.0.0.1 eureka01 eureka02 eureka03
10.18.200.132 eureka04

It was started four applications
Here Insert Picture Description

Here Insert Picture Description

Here Insert Picture Description

  • System Status, the main display of the system status, such as start time, etc.
  • DS Replicas, from where the service is to synchronize data
  • Instances currently registered with Eureka, registered in the list of examples of Eureka
  • General Info, the system operating environment, such as memory, cpu, etc.
  • Instance Info, basic information on this service, such as ip address, status, etc.

In the General Info module, you can see registered-replicas (already registered to copy) and available-replicas (valid copy) can see the information of the other two registration centers.

  • Center a copy of the registration is not available (unavailable-replicas)
  1. eureka.client.serviceUrl.defaultZone not to configure the address localhost address configuration should be configured as exemplified herein.
  2. Registration is not turned on each other
#表示是否将自己注册到 Eureka Server,默认为 true
eureka.client.register-with-eureka=true
#表示是否将自己注册到 Eureka Server,默认为 true
eureka.client.fetch-registry=true
  1. No host configuration information
  • UNKNOWN problem Eureka's
    service status registry (Eureka Server) in common are UP, DOWN, but sometimes there will be another state UNKNOWN, UNKNOWN or service name.
    UNKNOWN service name, because the name is not configured application examples in the project lead, configuration parameters
    spring.application.name or eureka.instance.appname, if these two parameters are not configured, the service name will appear UNKNOWN

Guess you like

Origin blog.csdn.net/ko0491/article/details/92789063