DEV Community

Keith
Keith

Posted on • Edited on • Originally published at Medium

The easiest SpringCloud tutorial ever | Chapter 1: Service registration and discovery Eureka (Finchley version)

Image description

  1. Introduction to spring cloud
    Spring Cloud provides developers with tools to quickly build distributed systems, including configuration management, service discovery, circuit breakers, routing, micro-agents, event buses, global locks, decision 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 spring boot in development.

  2. Create a service registry
    Here, I use Eureka as the component for service registration and discovery.

2.1 First create a maven main project.

First create a main Maven project and introduce dependencies in its pom file. The spring Boot version is 2.0.3.RELEASE, and the Spring Cloud version is Finchley.RELEASE. This pom file acts as a parent pom file and plays a role in dependent version control, and other module projects inherit the pom.

This series of articles all adopt this mode, and the pom of other articles is the same as this pom. To explain again, it will not be introduced again in the future. code show as below:

<?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>sc-f-chapter1</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>pom</packaging>
<name>sc-f-chapter1</name>
    <description>Demo project for Spring Boot</description>
<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.3.RELEASE</version>
        <relativePath/>
    </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.RELEASE</spring-cloud.version>
    </properties>
<dependencies>
        <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>
Enter fullscreen mode Exit fullscreen mode

2.2 Then create 2 model projects

One model project serves as the service registry, namely Eureka Server, and the other serves 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:

Image description
Next step -> select cloud discovery->eureka server, and then continue to the next step.
Image description
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>
<groupId>com.forezp</groupId>
    <artifactId>eureka-server</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>
<name>eureka-server</name>
    <description>Demo project for Spring Boot</description>
<parent>
        <groupId>com.forezp</groupId>
        <artifactId>sc-f-chapter1</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
<dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>
    </dependencies>
</project>
Enter fullscreen mode Exit fullscreen mode

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

@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {
public static void main(String[] args) {
        SpringApplication.run( EurekaServerApplication.class, args );
    }
}
Enter fullscreen mode Exit fullscreen mode

2.4 eureka is a highly available component. It has no back-end 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, and one must be specified. server. The configuration file application.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/
spring:
  application:
    name: eurka-server
Enter fullscreen mode Exit fullscreen mode

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:
Image description

No application available No service was found..._
Because of course no service can be discovered without a registered service.
Enter fullscreen mode Exit fullscreen mode
  1. Create a service provider (eureka client) When the client registers with the server, it provides 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>com.forezp</groupId>
        <artifactId>sc-f-chapter1</artifactId>
        <version>0.0.1-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>
Enter fullscreen mode Exit fullscreen mode

Indicate yourself as 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(value = "name", defaultValue = "forezp") String name) {
        return "hi " + name + " ,i am from port:" + port;
    }
}
Enter fullscreen mode Exit fullscreen mode

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:

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

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:
Image description
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
Enter fullscreen mode Exit fullscreen mode

Top comments (0)