The simplest SpringCloud tutorial in history | Part 1: Service registration and discovery (Eureka)

1. Introduction to Spring Cloud Spring Cloud provides developers with some tools to quickly build distributed systems, including configuration management, service discovery, circuit breakers, routing, micro-agents, event buses, global locks, decision-making campaigns, distributed sessions, and more . It runs in a simple environment and can run on the developer's computer. In addition, spring cloud is based on springboot, so you need to have a certain understanding of springboot in development. If you don't know it, you can read this article: Learn springboot in 2 hours. In addition, if you don't know the "microservice architecture", you can search for "microservice architecture" through the search engine.

2. Create a service registry Here, the components we need to use are Eureka of Spring Cloud Netflix. Eureka is a service registration and discovery module.

2.1 First create a maven main project.

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

The following takes creating a server as an example to describe the creation process in detail:

Right-click the project->create model->select spring initialir as shown below:

Paste_Image.png

Next step -> select cloud discovery->eureka server, and then continue to the next step.

Paste_Image.png

The pom.xml file of the created project 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>eurekaserver</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>eurekaserver</name>
<description>Demo project for Spring Boot</description>

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.2.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <java.version>1.8</java.version>
</properties>

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

    <!-- spring boot test-->
    <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>Dalston.RC1</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>

2.3 To start a service registry, only one annotation @EnableEurekaServer is needed. This annotation needs to be added to the startup application class of the springboot project:

@EnableEurekaServer @SpringBootApplication public class EurekaserverApplication {

public static void main(String[] args) {
    SpringApplication.run(EurekaserverApplication.class, args);
}

}

**2.4 **eureka is a highly available component. It has no backend cache. After each instance is registered, it needs to send a heartbeat to the registry (so it can be done in memory). By default, the erureka server is also an eureka client. A server must be specified. The configuration file appication.yml of eureka server:

server: port: 8761

eureka: instance: hostname: localhost client: registerWithEureka: false fetchRegistry: false serviceUrl: defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

Use eureka.client.registerWithEureka: false and fetchRegistry: false to indicate that you are an eureka server.

2.5 eureka server has an interface, start the project, open the browser to access: http://localhost:8761 , the interface is as follows:

Paste_Image.png

No application available No service was discovered... ^_^ Because of course no service can be discovered without a registered service.

3. Create a service provider (eureka client) When the client registers with the server, it will provide some metadata, such as host and port, URL, homepage, etc. The Eureka server receives heartbeat messages from each client instance. If the heartbeat times out, the instance is usually removed from the registration server.

The creation process is similar to the server, and the pom.xml is created 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>service-hi</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

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

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.2.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <java.version>1.8</java.version>
</properties>

<dependencies>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-eureka</artifactId>
    </dependency>
    <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>
</dependencies>

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>Dalston.RC1</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> indicates that it is an eurekaclient by annotating @EnableEurekaClient.

@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 String name) {
    return "hi "+name+",i am from port:" +port;
}

}

Only @EnableEurekaClient is not enough, you also need to indicate the address of your own service registry in the configuration file. The application.yml configuration file is as follows:

eureka: client: serviceUrl: defaultZone: http://localhost:8761/eureka/ server: port: 8762 spring: application: name: service-hi

It is necessary to specify spring.application.name, which is very important. In the future, mutual calls between services and services are generally based on this name. Start the project and open http://localhost:8761 , which is the URL of the eureka server:

Paste_Image.png

You will find that a service has been registered in the service, the service name is SERVICE-HI, and the port is 7862

Then open http://localhost:8762/hi?name=forezp and you will see on your browser:

hi forezp,i am from port:8762

Source code download: https://github.com/forezp/SpringCloudLearning/tree/master/chapter1

4. References springcloud eureka server official documentation

Springcloud eureka client official documentation

Excellent article recommendation: The easiest SpringCloud tutorial in history | The final chapter The easiest SpringCloud tutorial in history | Part 1: Service registration and discovery (Eureka) The simplest SpringCloud tutorial in history | Part 7: Highly available distributed configuration center (Spring Cloud Config)

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324430889&siteId=291194637