Spring cloud netfix 新手教程(2021版)| 第一篇 注册中心Eureka Server

2024/3/27 23:02:24

本文主要是介绍Spring cloud netfix 新手教程(2021版)| 第一篇 注册中心Eureka Server,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

创建Maven父工程

本教程我们使用的Spring boot版本为2.7,Spring cloud 版本为2021。Spring boot和Spring cloud版本对应表如下
file
父工程的xml如下,后面子模块都继承父工程统一项目的版本。

<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>org.example</groupId>
    <artifactId>spring-cloud-netflix-learn</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>pom</packaging>
   

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>


    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.7.18</version><!-- 使用你需要的Spring Boot版本 -->
    </parent>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>2021.0.9</version>
								
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

</project>

创建Eureka Server子模块

子模块xml如下,子模块集成父工程(后面教程所有的子模块都集成于父工程)

<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.example</groupId>
        <artifactId>spring-cloud-netflix-learn</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>

    <artifactId>spring-cloud-netflix-learn-eureka-server</artifactId>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

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

    </dependencies>

</project>

properties配置

server.port=7001

#Eureka Server 主机名
eureka.instance.hostname=localhost

#是否注册到注册中心,Eureka Server就是服务端不需要注册
eureka.client.register-with-eureka=false

#是否需要从注册中心获取服务,Eureka Server就是服务端不需要获取
eureka.client.fetch-registry=false

添加@EnableEurekaServer注解到主启动类上

package com.tubabaxuebiancheng;

import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

@SpringBootApplication
@EnableEurekaServer
public class EurekaServer {

    public static void main(String[] args) {
        // 启动Eureka服务器
        org.springframework.boot.SpringApplication.run(EurekaServer.class, args);
        System.out.println("Eureka Server启动成功!");
    }
}

启动Eureka Server


打开浏览器访问

原文链接 兔爸爸学编程博客

本文由博客一文多发平台 OpenWrite 发布!



这篇关于Spring cloud netfix 新手教程(2021版)| 第一篇 注册中心Eureka Server的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程