Spring cloud Eureka infrastructure

1 Introduction

Eureka is developed by Netflix, a REST service based component for service registration and discovery

It mainly includes two components: Eureka Server and Eureka Client

Eureka Client: a Java client used to simplify the interaction with Eureka Server (usually the client and server in microservices)
Eureka Server: provides service registration and discovery capabilities (usually the registry in microservices)

When each microservice starts, it will register itself with Eureka Server through Eureka Client, and Eureka Server will store the information of the service

Synchronization: each Eureka Server is also Eureka Client (logically)
Multiple Eureka servers complete the synchronization of service registry through replication to form high availability of Eureka
 Identification: Eureka Client will cache information in Eureka Server
 Even if all Eureka Server nodes are down, service consumers can still use the information in the cache to find service providers (I have personally tested)
Renewal: microservice will periodically (default 30s) send heartbeat to Eureka Server to Renew (similar to heartbeat)
Renewal: Eureka Server will perform the failure service detection function once a period (60s by default)
It will check if there is no new microservice after a certain period of time (default 90s), and will log off the microservice node if it is found
 

2 Establishment of single registration center

pom file
    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Hoxton.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>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </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>
Startup class
@SpringBootApplication
//Start a service registry
@EnableEurekaServer
public class ServerApplication {

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

}
properties file
Insert a code slice here server.port=1111
spring.application.name=eureka-server

eureka.instance.hostname=localhost
# Set whether to register yourself as a client to the registry (defaulttrue)
eureka.client.register-with-eureka=false
# Set whether to get registration information from the registry (defaulttrue)
eureka.client.fetch-registry=false
#Whether to turn on self-protection mode, default totrue. 
eureka.server.enable-self-preservation=true
#Registered address
eureka.client.service-url.defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka/

Launch the application and visit http://localhost:1111 to see the eureka information panel

Establishment of multiple registration centers

First registry
server.port=1112
spring.application.name=eureka-server-dev

eureka.client.register-with-eureka=true
eureka.client.fetch-registry=true
eureka.client.service-url.defaultZone=http://localhost:1113/eureka/
Second registration center
server.port=1113
spring.application.name=eureka-server-test

eureka.client.register-with-eureka=true
eureka.client.fetch-registry=true
eureka.client.service-url.defaultZone=http://localhost:1112/eureka/

Just start the service

Service registrant

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

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

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

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka</artifactId>
            <version>1.4.7.RELEASE</version>
        </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>
Startup class
@SpringBootApplication

@EnableDiscoveryClient
public class ProductionApplication {

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

}

Service consumers

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

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

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!--For service consumption-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-ribbon</artifactId>
            <version>1.4.7.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka</artifactId>
            <version>1.4.7.RELEASE</version>
        </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>
properties
server.port=9999
spring.application.name=ribbon-service

eureka.client.service-url.defaultZone=http://localhost:1111/eureka/
Startup class
@SpringBootApplication
//Register as client
@EnableDiscoveryClient
@EnableEurekaClient
public class ConsumerApplication {

    /**
     * Enable client load balancing
     */
    @Bean
    @LoadBalanced
    RestTemplate restTemplate(){
        return new RestTemplate();
    }

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

}
Test class
@RestController
public class ConsumerController {

    @Autowired
    RestTemplate restTemplate;

    @RequestMapping("/run1")
    public void run(){
        String body = restTemplate.getForEntity("http://HELLO-SERVICE/run", String.class).getBody();
        System.out.println(body);
    }
}

ribbon is a client load balancer based on HTTP and TCP.

Published 4 original articles, won praise 3, visited 125
Private letter follow

Tags: Spring Java Junit REST

Posted on Wed, 15 Jan 2020 08:41:38 -0500 by kevinkhan