Spring Cloud Netflix(一)
Introduction
Spring Cloud Netflix
It is Netflix
open source, and the Spring
integrated project to Spring Cloud
the mainly used to build large distributed projects.
Spring Cloud Netflix
To bind to the automatic configuration by Spring
the project, using annotations can quickly enable the corresponding function.
Spring Cloud Netflix
Mainly provide the following functions
Eureka
: Service discoveryHystrix
:breakerZuul
: Intelligent routingRibbon
: 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.
Eureka
Divided into Server
and Client
.
Client
It is the application side and the service provided to the outside.
Server
A server, that registry, stores all registered Client
metadata information. For example: host, port, health indicators, home page and other information.
Client
A heartbeat will be sent to Server
indicate that the service is normally available. If you Server
can not receive regular Client
heartbeat information will be Client
of Instance
removed. However, if the heartbeat between the two service hang, then Server
in the Client
not removed, there is a certain delay information.
1. Build Eureka-Server
1.1. Dependence
Eureka-Server
The 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
@EnableEurekaServer
Annotations are used to start Eureka
the 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.port
It is used to indicate Eureka
access to the service interface.
eureka.client.register-with-eureka
Other not represent Eureka
the service registration, because only one of the current node.
eureka.client.fetch-registry
It indicates whether from another Eureka
to 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 Eureka
the 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.
2. Build Eureka-Client
2.1. Dependency
Eureka-Client
The 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.defaultZone
For locating Eureka
registry
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:8761
page. 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 defaultspring.application.name
Availability Zones
: The number of instances, if multiple services with the same name are deployed, the corresponding number will be displayedStatus
: Shows the current instance status (UP: indicated as available), as well as those accessedURL
,URL
which point to the instance/actuator/info
2.5. Test the scenario where the client is closed
Will just start Eureka-Client
off and wait 1 minute, refresh localhost:9761
the page.
In the figure, although the Eureka-Client
service 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 Eureka
self-protection mechanism, if the heart rate is less than the expected threshold value is updated, Eurka
the service instance will not be removed, because the cause of the failure to prevent the network.