Spring Cloud Netflix(一)

Spring Cloud Netflix(一)

Introduction

Spring Cloud NetflixIt is Netflixopen source, and the Springintegrated project to Spring Cloudthe mainly used to build large distributed projects.

Spring Cloud NetflixTo bind to the automatic configuration by Springthe project, using annotations can quickly enable the corresponding function.

Spring Cloud Netflix Mainly provide the following functions

  • Eureka: Service discovery
  • Hystrix:breaker
  • Zuul: Intelligent routing
  • Ribbon: Client load balancing

Eureka

Eureka It mainly provides service registration and service discovery functions, which is one of the core functions in the microservice architecture.

EurekaDivided into Serverand Client.

Client It is the application side and the service provided to the outside.

ServerA server, that registry, stores all registered Clientmetadata information. For example: host, port, health indicators, home page and other information.

ClientA heartbeat will be sent to Serverindicate that the service is normally available. If you Servercan not receive regular Clientheartbeat information will be Clientof Instanceremoved. However, if the heartbeat between the two service hang, then Serverin the Clientnot removed, there is a certain delay information.

1. Build Eureka-Server

1.1. Dependence

Eureka-ServerThe dependence is spring-cloud-starter-netflix-eureka-server.

pom.xml

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

1.2. Main category

@EnableEurekaServerAnnotations are used to start Eurekathe service.

EurekaServerApplication.java

package com.example;

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

/**
 * @author mw118
 * @version 1.0
 * @date 2021/1/8 22:26
 */
@EnableEurekaServer
@SpringBootApplication
public class EurekaServerApplication {

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

1.3. Configuration

server.portIt is used to indicate Eurekaaccess to the service interface.

eureka.client.register-with-eurekaOther not represent Eurekathe service registration, because only one of the current node.

eureka.client.fetch-registryIt indicates whether from another Eurekato obtain information on the registry service.

application.properties

server.port=8762

eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false

logging.level.com.netflix.eureka=OFF
logging.level.com.netflix.discovery=OFF

1.4. Start and access

Access localhost:8761, will show Spring Eurekathe web page information.

The red box will display the instance information of the service. At this time, no service has been registered on the node.

Eureka Home

2. Build Eureka-Client

2.1. Dependency

Eureka-ClientThe dependence is spring-cloud-starter-netflix-eureka-client.

pom.xml

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

2.2. Main class

@EnableDiscoveryClient The client performs service registration.

EurekaClientApplication.java

package com.example;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author mw118
 * @version 1.0
 * @date 2021/1/8 22:29
 */

@EnableDiscoveryClient
@SpringBootApplication
public class EurekaClientApplication {
    
    

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

2.3. Configuration

spring.application.name Indicate the name of the current application, the capital of the name will be used as the name of the application

eureka.client.serviceUrl.defaultZoneFor locating Eurekaregistry

application.properties

spring.application.name=eureka-client

eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka

2.4. Start

Start the project, and refresh the localhost:8761page. At this time, in the red box, the client service just started will be displayed.

The important information displayed is:

  • Application: Instance name, the environment variable of the client is used by default spring.application.name
  • Availability Zones: The number of instances, if multiple services with the same name are deployed, the corresponding number will be displayed
  • Status: Shows the current instance status (UP: indicated as available), as well as those accessed URL, URLwhich point to the instance/actuator/info

Eureka Home

2.5. Test the scenario where the client is closed

Will just start Eureka-Clientoff and wait 1 minute, refresh localhost:9761the page.

In the figure, although the Eureka-Clientservice has been shut down, but there is also the instance in the list of registration centers, and state UP.

FIG reason references the first red box, which is due to the Eurekaself-protection mechanism, if the heart rate is less than the expected threshold value is updated, Eurkathe service instance will not be removed, because the cause of the failure to prevent the network.

Error message

Resources

Code

References and citations

Guess you like

Origin blog.csdn.net/qq_38685141/article/details/112385545