Eureka is a Netflix open source product that provides service registration and discovery, and it provides a complete implementation of Service Registry and Service Discovery. It is also one of the most important and core components in the springcloud system.
Background introduction
Service Center
The service center is also called the registration center, which manages various service functions including service registration, discovery, circuit breaker, load, downgrade, etc., such as various functions of the dubbo admin background.
What changes will happen with the service center call relationship? Draw a few simple diagrams to help you understand
Project A calls Project B
Normally call project A to request project B
After having a service center, no service can be removed and used directly, and it needs to be called through the service center.
Project A calls Project B, Project B is calling Project C
At this time, the steps to call will be two steps: the first step is that Project A first requests Project B server from the service center, and then Project B requests Project C service from the service center.
The above projects are just simple calls between two or three, but if there are more than 20 projects and 30 projects, our company's distributed projects have reached more than 20 by the end of 2015. Draw a picture to describe the relationship between dozens of projects and all are lines. Any changes to one of the projects will involve several projects restarting, which is very troublesome and prone to errors. To obtain services through the service center, you do not need to pay attention to the project IP address you call. It consists of several servers. Each time you go to the service center to get the available services to call.
Since all kinds of services have been registered in the service center, there are many advanced functions to do. For example, several services provide the same service to balance the load; monitor the success rate of server call to perform circuit breaking, remove the fault points in the service list; monitor the service call time to set different weights for different servers, etc.
Before talking about Eureka, I'll gossip about Netflix
Netflix
The following introduction comes from Baidu Encyclopedia:
Netflix is an American company that provides online streaming media on-site in the United States and Canada, and custom DVD and Blu-ray disc online rental services. Founded in 1997, the company is headquartered in Los Gaitu, California, and has started subscription services in 1999. In 2009, the company could provide up to 100,000 DVD movies and had 10 million subscribers. On February 25, 2007, Netflix announced that it had sold its 1 billionth DVD. A HIS report said that in 2011, Netflix online movie sales accounted for 45% of the total online movie sales of US users.
The first time I saw this word, it was at the beginning of various American dramas or movies. The representative American dramas filmed by Netflix include "House of Cards", "Drug Lords", and "Stranger Things". Later, when I was studying springcloud, I discovered Netflix companies and wondered if they were the same company. After checking the email suffix on Github, I determined that it was indeed the same company. In fact, springcloud's microservices are based on Netflix's open source products.
Netflix's open source framework components have been proven in Netflix's large-scale distributed microservice environment for years and are gradually being accepted by the community as standard components for constructing microservice frameworks. Spring Cloud open source products are mainly based on further packaging of Netflix open source components, which facilitates Spring developers to build a basic microservice framework. For some companies that intend to build a microservice framework system, making full use of or referring to Netflix's open source microservice components (or Spring Cloud) and conducting necessary enterprise customization on this basis is undoubtedly a shortcut to the microservice architecture.
Eureka
According to the official introduction:
Eureka is a REST (Representational State Transfer) based service that is primarily used in the AWS cloud for locating services for the purpose of load balancing and failover of middle-tier servers.
Eureka is a REST-based service, mainly used in the AWS cloud, and locates services to load balancing and failover of intermediate-tier servers.
Spring Cloud encapsulates the Eureka module developed by Netflix to enable service registration and discovery. Eureka adopts the CS design architecture. Eureka Server is the server with service registration function, it is the service registration center. Other microservices in the system use Eureka's client to connect to Eureka Server and maintain a heartbeat connection. In this way, the system maintenance personnel can monitor whether each microservice in the system is running normally through Eureka Server. Some other modules of Spring Cloud (such as Zuul) can use Eureka Server to discover other microservices in the system and execute relevant logic.
Eureka consists of two components: the Eureka server and the Eureka client. The Eureka server is used as a service registration server. The Eureka client is a java client used to simplify interaction with the server, act as a polling load balancer, and provide service failover support. Netflix uses another client in its production environment, which provides weighted load balancing based on traffic, resource utilization, and error status.
Use a picture to understand the following:
The above figure briefly describes the basic architecture of Eureka, consisting of 3 roles:
1. Eureka Server
2. Service Provider
3. Service Consumer
Case practice
Eureka Server
spring cloud has helped me implement the service registration center, and we only need a few simple steps to complete it.
1. Add dependencies in pom
<dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka-server</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency></dependencies>
2. Add @EnableEurekaServer annotation to the startup code
@SpringBootApplication@EnableEurekaServerpublic class SpringCloudEurekaApplication { public static void main(String[] args) { SpringApplication.run(SpringCloudEurekaApplication.class, args); }}3. Configuration file
By default, the service registry will also try to register itself as a client, so we need to disable its client registration behavior and add the following configuration in application.properties :
spring.application.name=spring-cloud-eurekaserver.port=8000eureka.client.register-with-eureka=falseeureka.client.fetch-registry=falseeureka.client.serviceUrl.defaultZone=http://localhost:${server.port}/eureka/eureka.client.register-with-eureka : indicates whether to register yourself with Eureka Server, default is true.eureka.client.fetch-registry : indicates whether to obtain registration information from Eureka Server, default is true.eureka.client.serviceUrl.defaultZone : Sets the address to interact with Eureka Server. Both query services and registration services need to rely on this address. The default is http://localhost:8761/eureka; multiple addresses can be used and separated.After starting the project, visit: http://localhost:8000/ and you can see the page below, where no services have been found
Cluster
Such a critical service as a registration center, if it is a single point, it will be devastating to encounter a failure. In a distributed system, the service registration center is the most important basic part and should be in a state where services can be provided at any time. To maintain its availability, using clusters is a good solution. Eureka implements highly available deployment by registering each other, so we only need to configure Eureke Server to configure other available serviceUrls to achieve highly available deployment.
Dual Node Registration Center
For the first time, we try to build a dual-node registration center.
1. Create application-peer1.properties as the configuration of the peer1 service center, and point serviceUrl to peer2
spring.application.name=spring-cloud-eurekaserver.port=8000eureka.instance.hostname=peer1eureka.client.serviceUrl.defaultZone=http://peer2:8001/eureka/
2. Create application-peer2.properties as the configuration of the peer2 service center, and point serviceUrl to peer1
spring.application.name=spring-cloud-eurekaserver.port=8001eureka.instance.hostname=peer2eureka.client.serviceUrl.defaultZone=http://peer1:8000/eureka/
3. Host conversion
Add the following configuration to the hosts file
127.0.0.1 peer1 127.0.0.1 peer2
4. Package start
Execute the following commands in turn
#Package mvn clean package# Start eurekajava with peer1 and peeer2 configuration information respectively -jar spring-cloud-eureka-0.0.1-SNAPSHOT.jar --spring.profiles.active=peer1java -jar spring-cloud-eureka-0.0.1-SNAPSHOT.jar --spring.profiles.active=peer2
After starting one by one, the browser enters: http://localhost:8000/ The rendering is as follows:
According to the figure, it can be seen that the DS Replicas of peer1's registration center has the relevant configuration information of peer2 and appears in available-replicas. We manually stop peer2 to observe, and find that peer2 will move to the unavailable-replicas column, indicating that peer2 is not available.
The configuration of this dual node has been completed.
eureka cluster usage
In production, we may need three or more registration centers to ensure the stability of the service. The configuration principles are actually the same, and we point the registration center to other registration centers respectively. Here we will only introduce the configuration of the three clusters. In fact, it is similar to the dual-node registration center. Each registration center can point to the other two nodes respectively, and use application.yml to configure it.
The application.yml configuration details are as follows:
--spring: application: name: spring-cloud-eureka profiles: peer1server: port: 8000eureka: instance: hostname: peer1 client: serviceUrl: defaultZone: http://peer2:8001/eureka/,http://peer3:8002/eureka/---spring: application: name: spring-cloud-eureka profiles: peer2server: port: 8001eureka: instance: hostname: peer2 client: serviceUrl: defaultZone: http://peer1:8000/eureka/,http://peer3:8002/eureka/---spring: application: name: spring-cloud-eureka profiles: peer3server: port: 8002eureka: instance: hostname: peer3 client: serviceUrl: defaultZone: http://peer1:8000/eureka/,http://peer2:8001/eureka/
Start the eureka registration center with the configuration parameters of peer1, peer2, and peer3 respectively.
java -jar spring-cloud-eureka-0.0.1-SNAPSHOT.jar --spring.profiles.active=peer1java -jar spring-cloud-eureka-0.0.1-SNAPSHOT.jar --spring.profiles.active=peer2java -jar spring-cloud-eureka-0.0.1-SNAPSHOT.jar --spring.profiles.active=peer3
After starting one by one, the browser enters: http://localhost:8000/ The rendering is as follows:
You can see the relevant information about peer2 and peer3 in peer1. So far, the eureka cluster has been completed
Sample code
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.