This is the 21st day of my participation in the First Challenge 2022

0, preface

Micro service is a hot concept in the past two years, known as one of the necessary skills for programmers, which shows its wide application. Recently I sorted out the data for systematic study and shared them with everyone.

Microservices are as opposed to traditional singleton architectures. Singleton architecture is a piece of code, and deployment is based on a single unit. Its advantage is that it is easy to deploy, but it faces low availability and poor scalability. Microservices solve this problem by providing a function as a separate service. Therefore, each separate function is called a service, and each service can call each other.

(For more information about micro services, see Introduction to micro services.)

This chapter mainly explains how various services invoke each other, namely, SpringCloud Eureka, which is cloud Service discovery, a REST-based service used to locate services to achieve cloud middle-layer service discovery and failover.

1. Introduction to SpringCloud Eureka

1.1 Eureka Service governance

1.1.1 Service Governance

Service governance is the core and basic module of microservice architecture, which is mainly used to realize automatic registration and discovery of each microservice instance.

Spring Cloud Eureka is a part of Spring Cloud’s Netflix microservices suite, which is based on Netflix Eureka as a secondary package. Mainly responsible for completing the service governance functions in the microservice architecture.

Eureka service governance system is as follows:

1.1.2 Service Registration

In the service governance framework, a registry is usually built, and each service unit registers its services to the registry, including the host and port number of the service, service version number, communication protocol and other additional information. The registry organizes the service list according to the service name. At the same time, it also needs to monitor whether the services in the list are available by heartbeat detection. If the services are not available, they need to be removed from the service list to achieve the effect of troubleshooting services.

1.1.3 Discovering Services

Under the service governance framework, the invocation between services is no longer implemented by specifying specific instance addresses, but by invoking requests from service names. The service caller retrieves a list of service instances from the service registry’s list of services by service name and retrieves a service instance location by specified load balancing policy for service invocation.

1.2 had been

1.2.1 had been introduced

Spirng Cloud Eureka uses Netflix Eureka for service registration and discovery. It contains both server and client components, and both are written in Java, so Eureka is mainly suitable for distributed systems implemented in Java or systems built in JVM-compatible languages. The Eureka server provides a complete REST API, so Eureka also supports the inclusion of non-Java language services into the Eureka service governance architecture, only other language platforms to implement their own Eureka client programs. At present, Steeltoe of.NET platform and Eureka-js-client of Node.js have implemented Ereka client components of their respective platforms.

1.2.2 Eureka Server

Eureka server, the service registry. Like other service registries, it supports highly available configurations. It provides good service instance availability based on strong consistency and can cope with different fault scenarios.

The Eureka server supports cluster deployment. When a fragment in a cluster fails, the Eureka server automatically switches to self-protection mode. It allows the discovery and registration of services to continue in the event of a shard failure, and when fault allocation is restored, the other shards in the cluster will synchronize their states back again. Different service registries in the cluster replicate their state to each other in asynchronous mode, which also means that each instance may have inconsistent state for all services at a given point in time.

1.2.3 Eureka Client

Eureka client, which handles registration and discovery of services. The client service is embedded in the code of the client application through registration and parameter configuration. When the application starts, the Eureka client registers its services with the service registry and periodically sends heartbeats to renew its service lease. At the same time, it can also query the current registered service information from the server, cache them locally and refresh the service status periodically.

2. Service Registry (Eureka server)

2.1 Registry project construction

2.1.1 Creating the parent project of the Modular Project

(project j for the SpringCloud series will be created in Maven modular project style for easy project management of individual microservices.)

The parent project is not required to create a registry, but is intended to facilitate subsequent management of individual service projects.

(1) As with new general SpringBoot project, please refer to [SpringBoot series] i. SpringBoot project construction.

A new project named “springCloud” is created as the parent project. The project structure is shown as follows:

(2) Modify and add pom. XML configuration and SpringCloud dependency package.

1 Change the packaging label value to POM. (If you do not modify add, you cannot add subitems), as follows:

	<groupId>com.xcbeyond.springcloud</groupId>
	<artifactId>springCloud</artifactId>
	< version > 0.0.1 - the SNAPSHOT < / version >
	<packaging>pom</packaging>
	<name>springCloud</name>
	<url>http://maven.apache.org</url>
Copy the code

Add the dependency package of SpringCloud:

<parent>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-parent</artifactId>
	. < version > 2.0.0 RELEASE < / version >
	<relativePath/>
</parent>
Copy the code

③ Adding dependency is valid for all submodules, that is, there is no need to add additional dependencies in submodules:

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

    <dependency>
   		<groupId>junit</groupId>
		<artifactId>junit</artifactId>
		The < version > 3.8.1 < / version >
		<scope>test</scope>
	</dependency>
</dependencies>
Copy the code

(3) Use modules tag to achieve modular project management.

Add modules tags to pop.xml to introduce the service discovery springCloudEureka project as follows:

<! Use modules tags for modular project management -->
<modules>
	<! -- Service discovery. (Eureka Server)-->
	<module>springCloudEureka</module>
</modules>
Copy the code

Or in the Overview view of POM.xml, add or Create subprojects directly in the Modules option. The diagram below:

The complete configuration of POM.xml is as follows:

<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>

	<groupId>com.xcbeyond.springcloud</groupId>
	<artifactId>springCloud</artifactId>
	<version>0.0.1 - the SNAPSHOT</version>
	<packaging>pom</packaging>
	<name>springCloud</name>
	<url>http://maven.apache.org</url>

	<! Use modules tags for modular project management -->
	<modules>
		<! -- Service discovery. (Eureka Server)-->
		<module>springCloudEureka</module>
	</modules>
	
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>. 2.0.0 RELEASE</version>
		<relativePath/>
	</parent>
	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
	</properties>

	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
		
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>3.8.1</version>
			<scope>test</scope>
		</dependency>
	</dependencies>
	
	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>

	<repositories>
		<repository>
			<id>spring-milestones</id>
			<name>Spring Milestones</name>
			<url>https://repo.spring.io/milestone</url>
			<snapshots>
				<enabled>false</enabled>
			</snapshots>
		</repository>
	</repositories>
</project>
Copy the code

2.1.2 Creating a sub-project of Eureka Service Registry

(1) new springCloudEureka project as Eureka registry, as shown below:

(2) Add dependency package to POM.xml.

Registries depend on Eureka Server, so the following dependencies need to be added:

<dependencies>
	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-web</artifactId>
	</dependency>
	<dependency>
		<groupId>org.springframework.cloud</groupId>
		<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
		<version>. 2.0.0 RELEASE</version>
	</dependency>
</dependencies>
Copy the code

2.2 Eureka Service registry code implementation and configuration

(1) springCloudEureka startup class.

com.xcbeyond.springcloud.eureka.SpringCloudEureka.java

(This is a regular SpringBoot project, and the bootstrap class only needs to be enabled with an additional @enableEurekaserver annotation.)

package com.xcbeyond.springcloud.eureka;

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

/** * Eureka boot class *@authorXcbeyond * August 3, 2018 9:40:48 am */
@SpringBootApplication
// Enable support for EurekaServer, that is, serve as the Eureka server
@EnableEurekaServer
public class SpringCloudEureka {
	public static void main(String[] args) { SpringApplication.run(SpringCloudEureka.class, args); }}Copy the code

(2) Configure application.properties as follows:

The current Eureka server port
server.port=8761
The hostname of the service registry instance
eureka.instance.hostname=localhost
Whether to register yourself with a service registry
eureka.client.registerWithEureka=false
# Whether to retrieve services
eureka.client.fetchRegistry=false
Copy the code

2.3. Start Eureka

Run springCloudEureka, the startup class of the springCloudEureka project. After the startup is successful, visit http://localhost:8761/ and you can see the Eureka information panel as shown in the following figure. It indicates that Eureka is successfully started and configured.

3. Service provider (Eureka client)

3.1 Eureka Client

At startup, the client registers itself with the Eureka Server through a Rest request, along with some meta information about the service itself. After the Eureka Server receives the Rest request, it stores the metadata information in a two-tier Map, where the first key is the service name. The key in the second layer is the instance name of the specific service.

On the Eureka client, you need to check whether the eureka.client.register-with-eureka=true parameter is correct. The default value is true. If set to FASLE, the registration operation will not start.

The Eureka client essentially represents a service that performs specific business function modules.

3.2 Creating the Eureka client project

Create Eureka client project springCloudEurekaClient according to “2.1.2 creating Eureka service registry sub-project” as follows:

3.3 Importing dependency Packages

Pom. XML is as follows:


      
<project
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
	xmlns="http://maven.apache.org/POM/4.0.0"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
	<modelVersion>4.0.0</modelVersion>

	<parent>
		<groupId>com.xcbeyond.springcloud</groupId>
		<artifactId>springCloud</artifactId>
		<version>0.0.1 - the SNAPSHOT</version>
	</parent>
	
	<groupId>com.xcbeyond.springcloud</groupId>
	<artifactId>springCloudEurekaClient</artifactId>
	<version>0.0.1 - the SNAPSHOT</version>
	
	<name>springCloudEurekaClient</name>
	<url>http://maven.apache.org</url>
	<description>Service provider.</description>
	
	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
	</properties>
	
	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
			<version>. 2.0.0 RELEASE</version>
		</dependency>

		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>3.8.1</version>
			<scope>test</scope>
		</dependency>
	</dependencies>
</project>
Copy the code

3.4 Eureka client code implementation and configuration

(1) Start class implementation

com.xcbeyond.springcloud.eurekaclient.EurekaClientApplication.java

package com.xcbeyond.springcloud.eurekaclient;

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

/** * Eureka Client (service provider) startup class *@authorXcbeyond * August 3, 2018 3:05:44 PM */
@SpringBootApplication
// Enable client support for Eureka Server
@EnableEurekaClient
public class EurekaClientApplication {
	public static void main(String[] args) { SpringApplication.run(EurekaClientApplication.class, args); }}Copy the code

(2) Application. Properties:

(/springCloudEurekaClient/src/main/resources/application.properties)

The current server port
server.port=8001
# application name
spring.application.name=Eureka-Client
The service registry address must be changed according to the registry IP address
eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/
Copy the code

3.5 Starting the Eureka Client

Run the startup class EurekaClientApplication. After the startup, visit the Eureka information panel http://localhost:8761/. The Instancescurrently Registered with Eureka column shows that Eureka-client has been successfully registered, as shown below: