SpringCloud搭建Eureka注册中心

通过springboot项目的形式搭建Eureka注册中心,具体包含以下几个步骤:

1、pom文件

<?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 https://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.6.5</version>
    </parent>
    <groupId>com.chengya</groupId>
    <artifactId>eureka-service</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>eureka-service</name>
    <description>Demo project for Spring Cloud</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>

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

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

2、配置文件application.yml

server:
  port: 3301
eureka:
  instance:
    hostname: localhost
  client:
    # 是否将自己注入到eureka
    register-with-eureka: false
    # 是否从eureka中获取注册信息
    fetch-registry: false
    #指定暴露给eureka client的请求地址
    service-url:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka

3、启动类

package com.chengya.eureka;

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

@SpringBootApplication
@EnableEurekaServer
public class EurekaServiceApplication {

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

}

4、测试

如果项目启动时遇到以下报错,可能是springboot版本和springcloud版本不匹配导致的

Caused by: java.lang.ClassNotFoundException: org.springframework.boot.context.properties.ConfigurationBeanFactoryMetadata

SpringCloud和SpringBoot版本对照表:

https://start.spring.io/actuator/info

"spring-cloud": {
  "Hoxton.SR12": "Spring Boot >=2.2.0.RELEASE and <2.4.0.M1",
  "2020.0.5": "Spring Boot >=2.4.0.M1 and <2.6.0-M1",
  "2021.0.0-M1": "Spring Boot >=2.6.0-M1 and <2.6.0-M3",
  "2021.0.0-M3": "Spring Boot >=2.6.0-M3 and <2.6.0-RC1",
  "2021.0.0-RC1": "Spring Boot >=2.6.0-RC1 and <2.6.1",
  "2021.0.1": "Spring Boot >=2.6.1 and <2.6.6-SNAPSHOT",
  "2021.0.2-SNAPSHOT": "Spring Boot >=2.6.6-SNAPSHOT and <3.0.0-M1",
  "2022.0.0-M1": "Spring Boot >=3.0.0-M1 and <3.1.0-M1"
}

修改SpringCloud和SpringBoot版本,重启项目

 访问端口地址,可以看到Spring Eureka,这就表示Eureka注册中心已经搭建成功。

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值