springcold-eureka project

1 eureka introduction

1. Eureka is a service discovery framework developed by Netflix. It is a REST-based service. It is mainly used to locate middle-tier services running in the AWS domain to achieve load balancing and middle-tier service failover. SpringCloud integrates it in its subproject spring-cloud-netflix to realize SpringCloud's service discovery function.

2. Eureka consists of two components: Eureka Server and Eureka Client.

3. Eureka Server provides service registration service. After each node is started, it will be registered in Eureka Server, so that the service registry in Eureka Server will store the information of all available service nodes, and the information of service nodes can be displayed intuitively in the interface. See.

4. Eureka Client is a java client that simplifies the interaction with Eureka Server. The client is also a built-in load balancer that uses the round-robin load algorithm.

5. After the application starts, it will send a heartbeat to Eureka Server. The default period is 30 seconds. If Eureka Server does not receive a heartbeat from a node within multiple heartbeat periods, Eureka Server will send this from the service registry. Service node removal (90 seconds by default).

6. Eureka Servers complete data synchronization through replication. Eureka also provides a client caching mechanism. Even if all Eureka Servers are down, the client can still use the information in the cache to consume APIs of other services. To sum up, Eureka ensures the high availability, flexibility and scalability of the system through mechanisms such as heartbeat check and client cache.

2 The parent class project pom file introduces dependencies

    <dependencyManagement>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>2.2.2.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Hoxton.SR12</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>       
    </dependencyManagement>

3 Create eureka server project

3.1 eureka-Server project 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">
    <parent>
        <artifactId>springcloud_demo</artifactId>
        <groupId>org.example</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>cloud_eureka</artifactId>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </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-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
        </dependency>

    </dependencies>
</project>

3.2 eureka module yml file

server:
  port: 8070

eureka:
  instance:
    hostname: eureka01
  client:
     ##配置不获取注册信息
    fetch-registry: false
   #不注册自己到Eureka注册中心 
    register-with-eureka: false
    service-url:
    #配置数据复制的peer节点
      defaultZone: http://localhost:8070/eureka/
      #关闭自我保护
  server:
    enable-self-preservation: false

3.3 eureka module startup class

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

Start the project
Open http://localhost:8070
insert image description here

4 Create a client to register the service in eureka

4.1 The client pom file introduces dependencies

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

4.2 client-side yml file configuration

eureka:
  client:
  #是否获取注册信息
    fetch-registry: true
    #是否注册到eureka中
    register-with-eureka: true
    #注册地址
    service-url:
      # defaultZone: http://localhost:8070/eureka,http://localhost:8071/eureka
      defaultZone: http://localhost:8070/eureka
  instance:
    instance-id: provider01:8082
    prefer-ip-address: true

4.3 Create a startup class

@SpringBootApplication
@EnableEurekaClient
public class ProviderApplication {
    
    
    public static void main(String[] args) {
    
    
        SpringApplication.run(ProviderApplication.class, args);
    }


}

Startup project
insert image description here

Guess you like

Origin blog.csdn.net/qq_46645840/article/details/128904830