Springboot: 2.1.6. RELEASE

SpringCloud: Greenwich.SR1

Unless otherwise noted, the above versions are used for this series of tutorials

Eureka is an open source Service registration and Discovery product from Netflix that provides complete implementations of Service Registry and Service Discovery. It is also one of the most important and core components of the springcloud architecture.

The significance of the registry

The registry

Manage various service functions including service registration, discovery, fusing, load, downgrade, etc., such as dubbo Admin background functions.

With the registry, the call relationship changes, so let’s draw a few sketches to see what happens.

Service A invokes service B

With a registry, any service is no longer directly connected, need to call through the registry.

If it is a continuous call:

Service A calls service B, and service B calls service C

If you add A registry here, the whole invocation process is divided into two steps, with service A requesting service B from the registry, and service B requesting service C from the registry

The above example only describes the call each other, between two or three services may add registry will be slightly tedious, if a call chain with dozens of services (this bit is no joke oh, normal business process is likely to appear this kind of complex call process), I just met in a work of more than 20 service call each other, Without the use of a registry, these complex business scenarios can be drawn into a network structure that makes it difficult to find the upstream and downstream relationships of services. If a service is changed, it will involve multiple machine reboots upstream and downstream, and the entire architecture design is completely coupled, requiring more work per change than you can imagine. Through the registry to register services, completely do not need to care about the upstream and downstream machine IP address, composed of several servers, whether restart will take effect, the registry has helped us to register and discover the service done, we just need to know where the registry is, what is the corresponding service name is OK ~~

Since various services are registered in the service center, there are many advanced features to do. For example, several services provide the same service to balance the load. Monitor the success rate of server calls to make fuses and remove failure points from the service list; Monitor service invocation times to set different weights for different servers, and so on.

The past and present of Netflix

Before we get to Eureka, let’s talk about Netflix:

The following introduction is from Baidu Baike:

Netflix(Nasdaq NFLX), founded in 1997, is an online video rental provider, mainly providing Netflix with a large number of DVDS and free delivery, headquartered in Los Gatto, California, the United States.

Netflix has topped the customer satisfaction list five times in a row. You can watch movies and TV shows on your PC, TV, iPad, iPhone, and connect your TV to your Wii, Xbox360, PS3 and other devices. Starting in October 2006, Netflix released about 100 million anonymous ratings of 1-5 movies in a data set containing only titles. Rating star and rating date without any text rating content. The competition asked participants to predict which Netflix customers would like, improving their prediction efficiency by more than 10 percent.

All in all, Netflix is one of the largest streaming media companies in the world. The list starts at the beginning of various American TV shows and movies, which include House of Cards, Narcos and Stranger Things. Springcloud’s microservices are based on Netflix’s open-source offerings.

The open source framework component of Netflix has been verified in the large-scale distributed micro-service environment of Netflix for many years, and is gradually accepted by the community as the standard component of the construction of micro-service framework. The Open source product of Spring Cloud is mainly based on the further encapsulation of Netflix open source components to facilitate Spring developers to build microservices infrastructure. For some companies planning to build microservice framework system, it is undoubtedly a shortcut to microservice architecture to make full use of or refer to Netflix’s open source microservice component (or Spring Cloud) and carry out necessary enterprise customization on this basis.

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 used primarily in the AWS cloud to locate services for load balancing and failover of mid-tier servers.

Spring Cloud encapsulates the Eureka module developed by Netflix for service registration and discovery. Eureka adopts the C-S design architecture. Eureka Server serves as the Server for the service registry function. It is the service registry. Other microservices in the system use Eureka’s clients to connect to Eureka Server and maintain heartbeat connections. In this way, the maintenance personnel of the system can monitor the normal operation of each micro-service in the system through Eureka Server. Some other modules in Spring Cloud, such as Zuul, can use Eureka Server to discover other microservices in the system and perform related logic.

Eureka consists of two components: Eureka server and Eureka client. The Eureka server serves as the service registry server. The Eureka client is a Java client designed to simplify interactions with servers, act as a polling load balancer, and provide failover support for services. Netflix uses a separate client in its production environment that provides weighted load balancing based on traffic, resource utilization, and error status.

Here’s an official picture:

The diagram above briefly describes the basic architecture of Eureka, consisting of three roles:

1, had been Server

  • Provide Service registration and discovery
  • Service Provider
  • Register your Service with Eureka, so that Service consumers can find Service consumers
  • Service consumer
  • Get a list of registered services from Eureka to be able to consume services

Case study

Finally to the main part, began the code ~~~

Eureka Server

There are currently two convenient solutions for creating a SpringCloud project. The core is the same, and you can choose the one that is convenient for you to use.

A:

Open the official spring link:

start.spring.io/

Enter your organization in Group, usually fill in the name of the company’s domain name, such as com.jd or com.baidu. Here, I directly write com.springCloud.

Fill in the Artifact with the name of the project; here I’ll just write Eureka.

Package selects jar and Java selects 8. Now that the basic choices are all selected, it is time to select the components of springcloud that we use, namely the main Eureka component.

Find Spring Cloud Discovery in Dependencies and select Eureka Serve.

Finally, click the long green button Generate the Project to download. After downloading, decompress the package and import it into our editing tool IDEA.

Method 2:

To create a service based on idea, start with file->new-> Project, and select Spring Initializr. Then you can see that the right side of the service let us select an initial url, the default is the official link above, start.spring. IO /

Click Next and fill in the same information for Group, Artifact, Java version, package, etc. Continue next, select Dependencies, and find Spring Cloud Discovery in the Dependencies section as you did before. Select Eureka Serve, click Next, select the project name and storage path, click Finish, wait for a while, and the first project Eureka will come out

I generally choose the first way to create SpringCloud projects, which does not rely on IDE tools.

pom.xml

For the Maven project, let’s start with pom.xml:

<? 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"> < modelVersion > 4.0.0 < / modelVersion > < the parent > < groupId > org. Springframework. Boot < / groupId > The < artifactId > spring - the boot - starter - parent < / artifactId > < version > 2.1.6. RELEASE < / version > < relativePath / > <! -- lookup parent from repository --> </parent> <groupId>com.springcloud</groupId> <artifactId>Eureka</artifactId> < version > 0.0.1 - the SNAPSHOT < / version > < name > Eureka < / name > < description > Demo projectforSpring Boot < / description > < properties > < Java version > 1.8 < / Java version > <spring-cloud.version>Greenwich.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>
		</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>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>

</project>

Copy the code
  • Parent: parent dependent project, you can see that the parent of the dependent springBoot is 2.1.6.RELEASE.
  • Properties: The current configuration file has some configurations, you can see the Java version is 1.8, springCloud version is Greenwich.SR1.
  • Eureka test framework dependencies: Eureka
  • DependencyManagement: the parent project does not inherit a dependency if the parent project does not declare a dependency. The dependency is inherited from the parent project only if the dependency is written in the child project and no version is specified, and both version and scope are read from the parent POM. In addition, if the version number is specified in the subproject, the jar version specified in the subproject is used.
  • The spring-boot-Maven-plugin can package a Spring Boot application as an executable JAR or WAR file, and then run the Spring Boot application in the usual way.

configuration

The default configuration file is application.properties under Resource. In SpringBoot projects, two configuration files are currently supported, as well as yamL, and all the configurations I’m using here are yamL.

server:
  port: 8761
spring:
  application:
    name: eureka-serve
eureka:
  server:
    enable-self-preservation: false
  client:
    register-with-eureka: false
    service-url:
      defaultZone: http://localhost:8761/eureka/
Copy the code
  • Enable-self-preservation: Prevents clients from being displayed online incorrectly due to the Eureka mechanism. This function is available only in the development environment. This function needs to be cached in the production environment to prevent frequent online and offline services due to network fluctuations.
  • Register-with-eureka: Not like a registry that registers itself
  • Register-with-eureka: indicates the application registration address of the Eureka server

Start the EurekaApplication. Java

package com.springcloud.Eureka; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; @SpringBootApplication @EnableEurekaServer public class EurekaApplication { public static void main(String[] args) { SpringApplication.run(EurekaApplication.class, args); }}Copy the code

Add annotation @enableeurekaserver. In the main function, directly right-click debug to start, as shown in the picture below:

Now single registry has successfully launched, extended out a problem, the registry is a place where all of the service provider registration services, if only one machine, if for some reason, cause downtime, can cause the overall service is not available, so the center services in the production environment must be clustered deployment, If you have higher requirements on high availability, DISASTER recovery, and backup, you can deploy it in different rooms or in different regions.

High availability cluster

Double machine deployment

To add an IDEA launch configuration, click On Edit Configurations in the upper right corner, as shown below:

Select Eureka1 as the boot option and add Program arguments: –server.port=8080. Click Apply and save.

Start the service with the newly created startup configuration and you can now see that it started normally. The next step is to modify the configuration file so that the two independent services become clusters.

server:
  port: 8761
spring:
  application:
    name: eureka-serve
eureka:
  server:
    enable-self-preservation: false
  client:
    register-with-eureka: false
    service-url:
      defaultZone: http://localhost:8761/eureka/,http://localhost:8080/eureka/
Copy the code

Only need a new one on defaultZone address of our new service, http://localhost:8080/eureka/, to change from single machine immediately to double machine.

Now start Eureka with two separate startup configurations, as shown below:

The red box shows that we now have two Eureka services.

In the same way as the two-machine configuration scheme, the multi-machine configuration is to add the service addresses of other machines after defaultZone.

Example code -Github