Springcloud's (Eureka) service registration and discovery

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 campaigns, distributed sessions, and more.

 

2. Create a service registry

Here, the components we need to use are Spring Cloud Netflix's Eureka, which 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 as the service registry, namely Eureka Server, and the other as Eureka Client.

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

I use spring sts:

Right-click the project->New->Select Spring Starter Project:

The pom for creating the 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.shiziqiu</groupId>
    <artifactId>eurekaServer-study</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>eureka-server-study</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 required. This annotation needs to be added to the startup application class of the springboot project:

 

@EnableEurekaServer
@SpringBootApplication
public class EurekaServerStudyApplication {
    public static void main(String[] args) {
        SpringApplication.run(EurekaServerStudyApplication.class, args);
    }
}

Let's take a look at the configuration file.
By default, the erureka server is also an eureka client, and 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:



 

 

3. 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.shiziqiu</groupId>
    <artifactId>eurekaClient</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

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

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.7.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>


Indicate yourself as an eurekaclient by annotating @EnableEurekaClient.

@EnableEurekaClient
@RestController
@SpringBootApplication
public class EurekaClientStudyApplication {

    public static void main(String[] args) {
        SpringApplication.run(EurekaClientStudyApplication.class, args);
    }
   
    @Value("${server.port}")
    String port;
   
    @RequestMapping("/hello")
    public String home(@RequestParam String name) {
        return " hello" + name + "i am from port:" + port;
    }
}

You need to indicate the address of your 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, after which the 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:

 

 

 

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/hello?name=shiziqiu and you will see on the browser:

 

helloi shiziqiu,i am from port:8762

Source code download: https://github.com/shiziqiu/shiziqiu-springcloud

 

4. References

springcloud eureka server official documentation

Springcloud eureka client official documentation

 

 

Guess you like

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