Spring Cloud (Finchley Version) Tutorial: Registration and Discovery of Services Eureka

Keywords: PHP Spring Maven Apache xml

Introduction to spring cloud

The current version of spring cloud has been updated to Finchley, which supports Springbott version 2.0 or more. The specific version can refer to the table below.

Cloud code Boot version (train) Boot version (tested) lifecycle
Angle 1.2.x incompatible with 1.3 EOL in July 2017
Brixton 1.3.x 1.4.x 2017-07
Camden 1.4.x 1.5.x -
Dalston 1.5.x not expected 2.x -
Edgware 1.5.x not expected 2.x -
Finchley 2.x not expected 1.5.x -

Finchley version of the official document link:

http://cloud.spring.io/spring-cloud-static/Finchley.RELEASE/single/spring-cloud.html

II. Creating a Service Registry
Here, I still use Eureka as a component of service registration and discovery. As for Consul, I will introduce it in detail later.

2.1 First create a maven master project.

First, create a main Maven project to introduce dependencies in its POM file, spring Boot version 2.0.3.RELEASE, Spring Cloud version Finchley.RELEASE. This POM file acts as the parent POM file and relies on version control. Other module projects inherit the pom. All of the articles in this series adopt this mode, and the POM of other articles is the same as the pom. Again, we will not repeat the introduction in the future. The code is as follows:

 

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <project xmlns="http://maven.apache.org/POM/4.0.0"
 3          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 5     <modelVersion>4.0.0</modelVersion>
 6 
 7     <groupId>com.liu</groupId>
 8     <artifactId>SpringCloudLearn01</artifactId>
 9     <version>1.0-SNAPSHOT</version>
10 
11     <name>sc-f-chapter1</name>
12     <description>Demo project for Spring Boot</description>
13 
14     <parent>
15         <groupId>org.springframework.boot</groupId>
16         <artifactId>spring-boot-starter-parent</artifactId>
17         <version>2.0.3.RELEASE</version>
18         <relativePath/>
19     </parent>
20 
21     <modules>
22         <module>eureka-server</module>
23         <module>service-client</module>
24         <module>serice-feign</module>
25     </modules>
26 
27     <properties>
28         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
29         <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
30         <java.version>1.8</java.version>
31         <spring-cloud.version>Finchley.RELEASE</spring-cloud.version>
32     </properties>
33 
34     <dependencies>
35         <dependency>
36             <groupId>org.springframework.boot</groupId>
37             <artifactId>spring-boot-starter-test</artifactId>
38             <scope>test</scope>
39         </dependency>
40     </dependencies>
41 
42     <dependencyManagement>
43         <dependencies>
44             <dependency>
45                 <groupId>org.springframework.cloud</groupId>
46                 <artifactId>spring-cloud-dependencies</artifactId>
47                 <version>${spring-cloud.version}</version>
48                 <type>pom</type>
49                 <scope>import</scope>
50             </dependency>
51         </dependencies>
52     </dependencyManagement>
53 
54     <build>
55         <plugins>
56             <plugin>
57                 <groupId>org.springframework.boot</groupId>
58                 <artifactId>spring-boot-maven-plugin</artifactId>
59             </plugin>
60         </plugins>
61     </build>
62 
63 </project>

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

Following is an example of creating server to illustrate the creation process in detail:

Right-click Project - > Create Model - > Select spring initialir as follows:

 

Next - > select cloud discovery - > Eureka server, and then proceed to the next step.

After the project is created, its pom.xml inherits the parent POM file and introduces the dependency of spring-cloud-starter-netflix-eureka-server. 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>
    <parent>
        <groupId>com.liu</groupId>
        <artifactId>SpringCloudLearn01</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>

    <groupId>com.liu.example</groupId>
    <artifactId>config-client</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>config-client</name>
    <description>Demo project for Spring Boot</description>

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

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

</project>

2.3 Starting a service registry requires only a comment @EnableEurekaServer, which needs to be added to the Startup application class of the springboot project:

package com.liu.example.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);
    }

}

**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, erureka server is also an eureka client, and a server must be specified. Ereka server configuration file appication.yml:

server:
  port: 8761

#Express yourself as an eureka server through eureka.client.registerWithEureka: false and fetchRegistry: false.
eureka:
  instance:
    hostname: localhost
  client:
    registerWithEureka: false
    fetchRegistry: false
    serviceUrl:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

spring:
  application:
    name: eurka-server

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

 

No application available no service was found... _
Because of course, no service can be found without registration service.

3. Create a service provider (eureka client)
When client registers with server, it provides some metadata, such as host and port, URL, home page, etc. Eureka server receives heartbeat messages from each client instance. If the heartbeat is timed out, the instance is usually deleted from the registration server.

The process of creating pom.xml is similar to that of 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>
    <parent>
        <groupId>com.liu</groupId>
        <artifactId>SpringCloudLearn01</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>

    <groupId>com.liu.example</groupId>
    <artifactId>service-client</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>service-client</name>
    <description>Demo project for Spring Boot</description>

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

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

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

</project>

By annotating @Enable EurekaClient, you indicate that you are an eurekaclient.

package com.liu.example.serviceclient;

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

@SpringBootApplication
@EnableEurekaClient
public class ServiceClientApplication {

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

}

Write a method on the new service type:

package com.liu.example.serviceclient.service;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.Mapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class helloSevice {

    @Value("${server.port}")
    String port;

    @RequestMapping("/hi")
    public String getHello(){
        return "hello! My port number is:" + port;
    }
}

It is not enough to just @Enable EurekaClient. You also need to specify the address of your service registry in the configuration file. The application.yml configuration file is as follows:

server:
  port: 8762
spring:
  application:
    name: service-client

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/

It's important to specify spring.application.name, which is the basis for subsequent calls between services.
Start the project and open http://localhost:8761, the website of eureka server:

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

When you open http://localhost:8762/hi?name=forezp, you will see in the browser:

Source download: https://github.com/MrLiu1227/MySpringCloud.git

Posted by khanuja.sunpreet on Sat, 20 Jul 2019 18:47:51 -0700