What is Service registration and discovery

The Spring Cloud Eureka module provides passive service discovery.

Service registration: Each user goes to the chatroom server to register.

Service discovery: His friends will see you, and you’ll get a list of your friends.

In microservices, the service acts as a chat room user and the service registry acts as a chat room server.

Current service discovery solutions include Eureka, Consul, Zookeeper, and more. SpringCloud uses eureka as its service registry by default.

Ii. Use process of Eureka

It’s simple:

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

1. Eureka Server provides service registration and discovery

2. Service Provider registers its Service with Eureka, so that Service consumers can find it

The Consumer obtains the list of registered services from Eureka and is able to consume the services

 

Create a service registry

Here, I will continue to use Eureka as a component for service registration and discovery, which Consul will cover in more detail later.

2.1 First create a Maven master project.

First, create a main Maven project and introduce dependencies in its POM file with spring Boot version 2.0.3.RELEASE and Spring Cloud version finchley.release. This POM file acts as a parent that depends on version control and is inherited by other Module projects. This is the pattern for all articles in this series, and the POM for the other articles is the same as this POM. Again, I’m not going to introduce it again. The code 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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0. 0</modelVersion>

    <groupId>com.forezp</groupId>
    <artifactId>spring-cloud</artifactId>
    <version>0.01.-SNAPSHOT</version>
    <packaging>pom</packaging>

    <name>spring-cloud</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.04..RELEASE</version> <relativePath/> <! -- lookup parent from repository --> </parent> <modules> <module>eureka-server</module>
        <module>service-hi</module>
    </modules>

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

2.2 Then create two Model projects: one model project as the service registry, namely Eureka Server, and the other as Eureka Client.

The following uses server creation as an example to explain the creation process in detail:

Right-click project -> Create Model -> Select Spring Initialir as shown below:

Next -> Select Cloud Discovery -> Eureka Server and proceed to the next step.

The poM. XML inherits the parent POM file and introduces the spring-cloud-starter-Netflix-Eureka-server dependency 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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0. 0</modelVersion>

    <groupId>com.forezp</groupId>
    <artifactId>eureka-server</artifactId>
    <version>0.01.-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>eureka-server</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>com.forezp</groupId>
        <artifactId>spring-cloud</artifactId>
        <version>0.01.-SNAPSHOT</version>
    </parent>

    <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>Finchley.SR1</spring-cloud.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>
    </dependencies>

</project>
Copy the code

2.3 Starting a service registry requires only one annotation @enableeurekaserver. This annotation needs to be added to the SpringBoot project startup Application class:

package com.forezp.eurekaserver;

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

**2.4 ** Eureka is a highly available component, it has no back-end cache, every instance needs to send a heartbeat to the registry after registration (so it can be done in memory), erureka Server is also a Eureka Client by default, and a server must be specified. Eureka server configuration file application.properties:

Server.port =8761Name =eureka server ## Eureka instance.hostname=localhost ## The service registry will also try to register itself as a client, so we need to disable its client registration behavior ## Whether to get registration information from the Eureka server, so here it isfalse
eureka.client.fetch-registry=false## Whether to register itself to eureka server, because the current application is eureka server, there is no need to register itself, so here isfalse
eureka.client.register-with-eureka=falseEureka.client.service-url.defaultzone = HTTP://${eureka.instance.hostname}:${server.port}/eureka/
Copy the code

Through eureka. Client. RegisterWithEureka: false and fetchRegistry: false to show itself to be a eureka server.

2.5 Eureka Server provides a UI. Start the project, open a browser, and visit http://localhost:8761. The UI is as follows:

No application available No service found…… ^_^ because of course no service can be found without registration service.

Create a service provider (Eureka Client)

When a client registers with a server, it provides metadata such as host and port, URL, home page, and so on. Eureka Server receives heartbeat messages from each client instance. If the heartbeat times out, the instance is usually removed from the registry server.

The process for creating pom.xml is similar to that for server.

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

    <groupId>com.forezp</groupId>
    <artifactId>service-hi</artifactId>
    <version>0.01.-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>service-hi</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>com.forezp</groupId>
        <artifactId>spring-cloud</artifactId>
        <version>0.01.-SNAPSHOT</version>
    </parent>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>

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

</project>
Copy the code

Indicate that you are a Eurekaclient by annotating @enableeurekaclient.

package com.forezp.servicehi;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
@EnableEurekaClient
@RestController
public class ServiceHiApplication {

	public static void main(String[] args) {
		SpringApplication.run(ServiceHiApplication.class, args);
	}
	@Value("${server.port}")
	String port;

	@RequestMapping("/hi")
	public String home(@RequestParam(value = "name", defaultValue = "forezp") String name) {
		return "hi " + name + " ,i am from port:"+ port; }}Copy the code

@enableeurekaclient @enableeurekaclient @enableeurekaclient @enableeurekaclient @enableeurekaclient @enableeurekaclient

Port = server.port=8762Register the name in the Eureka registry, automatically convert to all caps, Name =service-hi eureka.client.register-with-eureka=true
eureka.client.fetch-registry=trueEureka.client.service -url.defaultzone = HTTP://localhost:8761/eureka/
Copy the code

It is important to specify spring.application.name, which will be used for future calls between services. To start the project, go to http://localhost:8761, which is eureka Server’s website:

You will find that a SERVICE is already registered with the SERVICE, service-hi, and port 8762

Open http://localhost:8762/hi? Name =forezp, you will see in your browser:

 

Iv. Reference materials

The original link: blog.csdn.net/forezp/arti…

The original link: blog.csdn.net/saytime/art…

Blog.csdn.net/forezp/arti…

Cloud. Spring. IO/spring – clou…