Microservice combat series (two)-registration center Springcloud Eureka client

1. Scene description

A few days ago, I introduced the Eureka registration center of springcloud ( springcloud-registration center rapid construction ). Today, combined with springboot-web, I introduced the eureka client service registration.

2. Solve the problem

2.1 New eureka client project

2.1.1 new->project

2.1.2 Change the project name

2.1.3 Dependent package selection

(1) Select the web starter and wait for the next run to test.

(2) The registry client depends on package selection

next-》finish

2.2 Project introduction

Three classes can be run.

2.2.1 pomx file
<?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>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.4.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.spc</groupId>
    <artifactId>eurekaclient</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>eurekaclient</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Greenwich.SR1</spring-cloud.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <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-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </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>

Note : Just two starters, one web starter and one eureka-client.

2.2.2 Startup
package com.spc.eurekaclient;

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.RestController;

@SpringBootApplication
@RestController
@EnableEurekaClient
public class EurekaclientApplication {

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

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

    @RequestMapping("/")
    public String home() {
        return "i'm 软件老王,欢迎光临,服务端口号为:" + port;
    }
}

Description :

(1) There are three labels on the startup class, @SpringBootApplication, @RestController, and @EnableEurekaClient.

(2) Use the springmvc tag @RequestMapping("/") to get a port number from the request and print it.

如果你觉得文章对你有些帮助,欢迎微信搜索「软件老王」第一时间阅读或交流!
2.2.3 application.yml
eureka:
    client:
        service-url:
            defaultZone: http://localhost:8761/eureka/
        healthcheck:
            enabled: true
        instance:
            lease-expiration-duration-in-seconds: 30
            lease-renewal-interval-in-seconds: 10
        registry-fetch-interval-seconds: 5
server:
    port: 9001
spring:
    application:
         name: client



Highlights :

defaultZone: http://localhost:8761/eureka/, this address is the address of the eureka registration center ( springcloud-registration is built at k speed ), the others are optional, and the others are to quickly see the registration The heartbeat time set by the message can be ignored.

2.3 Effect picture

Start the eureka registry and client service registration locally through different port numbers ( springcloud starts multiple instances )

2.3.1 euraka registration center

2.3.2 eurake client access


For more knowledge, please pay attention to the public account: "Software King" , share IT technology and related dry goods, reply to keywords to get the corresponding dry goods, java , send 10 must-see "Martial Arts Cheats"; pictures , send more than 1 million copies for commercial use High-definition pictures; interview , send java interview questions with a monthly salary of "20k" just after graduation, soft test , send official pdf books and customs clearance papers, follow-up will continue to update, such as " tools ", " videos ", etc., have been sorted out.
Insert picture description here

Guess you like

Origin blog.csdn.net/wjg8209/article/details/108776454