[Spring Cloud Series] 2. Service registration and discovery Eureka (Hoxton.M3 version)

[Spring Cloud Series] 2. Service registration and discovery Eureka (Hoxton.M3 version)

This is the challenge I participated in more text on the second day event details see: more text challenge

Development environment:

IDEA JDK1 .8 Spring Cloud Hoxton.M3 The Boot the Spring 2.2 .0 copy the code

1. Introduction to Eureka

Eureka is developed by Netflix, a component of service registration and discovery based on REST services. It mainly includes two components: Eureka Server and Eureka Client.

Eureka Client : A Java client used to simplify the interaction with Eureka Server (usually the client and server in microservices)

Eureka Server : Provides service registration and discovery capabilities (usually the registry in microservices)

Eureka : It is a pure servlet application that needs to be built into a war package for deployment. It uses the Jersey framework to implement its own RESTful HTTP interface. The synchronization between peers and service registration are all implemented through the HTTP protocol. Eureka timing tasks (sending heartbeats, timing Cleaning up expired services, node synchronization, etc.) is implemented through the Timer that comes with the JDK, and the memory cache is implemented using Google's guava package. The eureka-core module contains the core implementation of functions:

com.netflix.eureka.cluster -functions related to peer node replication.

com.netflix.eureka.lease -the "lease", used to control the life cycle of registration information (add, clear, renew).

com.netflix.eureka.registry -store and query service registration information.

com.netflix.eureka.resources- "R" in RESTful style, which means resources. It is equivalent to the Controller in SpringMVC.

com.netflix.eureka.transport -the client that sends HTTP requests, such as heartbeats.

com.netflix.eureka.aws -classes related to amazon AWS services.

2. create a project

1. File ----- New -----Project

2. Spring Initializr ----- Next

3. Waiting to create the project

4. Enter Group and Artifact, click Next

5. If the following prompt appears, the artifact is forbidden to capitalize, otherwise the save will prompt "Cannot Save Settings"

6. Choose Spring cloud Discovery ----- Eureka Server ---- Next

7. After the creation is complete, click Finish to open the new project in a new window

8. Spring Cloud is successfully created, and the directory structure is as follows:

3. improve the project

Since the file just created is the default project for initialization, you need to add corresponding annotations and configuration files. The details are as follows:

1. The generated pom.xml is as follows:

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd" > < modelVersion > 4.0.0 </modelVersion > < parent > < groupId > org.springframework.boot </groupId > <artifactId > spring-boot-starter-parent</artifactId> < version > 2.2.0.RELEASE </version > < relativePath/> <!-- lookup parent from repository --> </parent > < groupId > cn.mcus </groupId > < artifactId > eurekaserver </artifactId > < version > 0.0.1-SNAPSHOT </version > < name > eurekaserver </name > <description >Demo project for Spring Boot </description > < properties > < project.build.sourceEncoding > UTF-8 </project.build.sourceEncoding > < project.reporting.outputEncoding > UTF-8 </project.reporting.outputEncoding > < java.version > 1.8 </java.version > < spring-cloud.version > Hoxton.M3 </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 > < exclusions > < exclusion > < groupId > org.junit. vintage </groupId > < artifactId > junit-vintage-engine </artifactId > </exclusion > </exclusions > </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 > < 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 code

2. Add the @EnableEurekaServer annotation to the EurekaserverApplication.java startup class, and Eureka serves as the annotation of the service registry.

3. Set the configuration file application.properties. SpringCloud supports application.properties and)appication.yml in two formats as follows.

3.1application.properties format:
#Registration center default port is 8761#Registration , you can also define other ports in the following way, defultZone: Set the address of the eureka server, registration services and query services rely on this address. server.port= 8761 #eureka.client.serviceUrl.defaultZone=http: //localhost:${server.port}/eureka/ # Whether to register to the eureka server, because you are the eureka server, there is no need to register yourself. eureka.client.registerWithEureka = false #Whether to obtain the registration information from the eureka server, it is not necessary here. eureka.client.fetchRegistry= false #Whether to open the self-protection mode, the default is true . eureka.server.enable-self-preservation= true #Renewal time, that is, the interval of scanning for invalid services (the default is 60 *1000ms) --interval The timer- eureka.server.eviction in -ms = 10000 duplicated code
3.2appication.yml format:
eureka.client.fetchRegistry = false server : port: 8761 eureka : instance: hostname: localhost client : registerWithEureka: false fetchRegistry : false serviceUrl : defaultzone: HTTP: //${eureka.instance.hostname}:${server.port}/eureka/ copy the code

*This project uses the application.properties format as an example.

4. run the project

1. Enter the EurekaserverApplication.java startup class, right-click and select Run.

2. As follows, the project has started

3. Enter http://localhost:8761/ in the browser to start the service registration center, and the following page appears

5. project code

1. 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 https://maven.apache.org/xsd/maven-4.0.0.xsd" > < modelVersion > 4.0.0 </modelVersion > < parent > < groupId > org.springframework.boot </groupId > <artifactId > spring-boot-starter-parent </artifactId > < version > 2.2.0.RELEASE </version > < relativePath/> <!-- lookup parent from repository --> </parent > <groupId > cn.scpro</groupId > < artifactId > eurekaserver </artifactId > < version > 0.0.1-SNAPSHOT </version > < name > eurekaserver </name > <description> Demo project for Spring Boot </description > < properties > < project.build.sourceEncoding > UTF-8 </project.build.sourceEncoding > < project.reporting.outputEncoding > UTF-8 </project.reporting.outputEncoding > < java.version > 1.8 </java.version > < spring-cloud.version > Hoxton.M3 </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 > < exclusions > < exclusion > < groupId > org.junit. vintage </groupId > < artifactId > junit-vintage-engine </artifactId > </exclusion > </exclusions > </dependency > </dependencies > <!--spring-cloud dependency--> < 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 > < 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 code

2, application.properties file

#Registration Center The default port is 8761 , you can also define other ports in the following way, defultZone: Set the address of the eureka server, registration services and query services rely on this address. server.port = 8761 #eureka.client.serviceUrl.defaultZone=http: //localhost:8761/eureka/ # Whether to register to the eureka server, because you are the eureka server, there is no need to register yourself. eureka.client.registerWithEureka = false #Whether to obtain the registration information from the eureka server, it is not necessary here. eureka.client.fetchRegistry= false #Whether to open the self-protection mode, the default is true . eureka.server.enable-self-preservation= true #Renewal time, that is, the interval of scanning for invalid services (default is 60 *1000ms) eureka.server.eviction-interval-timer-in -ms = 10000 duplicated code

3. EurekaserverApplication.java

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

The above is the creation process of a Spring Cloud service registry. Welcome to exchange and learn.