spring cloud学习(一)-eureka服务中心搭建

Eureka是由Netflix开源的服务注册和发现产品,适用于AWS云和其他需要注册中心的场景。它是SpringCloud的重要组件,由Eureka服务端和客户端组成,简化了服务间的交互,支持负载均衡和故障切换。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

什么是Eureak

Eureka 是 Netflix 公司开源的产品,它是一种基于 REST(Representational State Transfer)的服务,主要用于 AWS 云。Eureka 提供了完整的 Service Registry 和 Service Discovery 实现,也是 Spring Cloud 体系中最重要最核心的组件之一。

简单来说 ,Eureka 就是 Netflix 开源的一款提供服务注册和发现的产品,并且提供了 Java 客户端。当然在 Spring Cloud 大力优化后的 Eureka,已经不仅仅只是用于 AWS 云,而是可以应用在任何需要使用注册中心的场景。

Eureka 由两个组件组成:Eureka 服务端和 Eureka 客户端。Eureka 服务端就是注册中心。Eureka 客户端是一个 java 客户端,用来简化与服务端的交互、作为轮询负载均衡器,并提供服务的故障切换支持。

下面是Eureka的使用场景

从上面看Eureka Server担任注册中心的角色,提供了服务的发现和注册功能
Service Provider 服务提供者,将自身的服务注册到Eureka Server,同时通过心跳检查服务的运行状态
Service Consumer 服务调用者,从Eureka Server 得到注册的服务列表,找到对应的服务地址在调用并使用 

SpringBoot2.x整合SpringCloud

<properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
        <junit.version>4.12</junit.version>
        <log4j.version>1.2.17</log4j.version>
        <lombok.version>1.16.18</lombok.version>
        <mysql.version>5.1.47</mysql.version>
        <druid.version>1.1.16</druid.version>
        <mybatis.spring.boot.version>1.3.0</mybatis.spring.boot.version>
    </properties>

    <dependencyManagement>

        <dependencies>

            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>2.2.2.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>

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

            <!--            <dependency>-->
            <!--                <groupId>come.alibaba.cloud</groupId>-->
            <!--                <artifactId>spring-cloud-alibaba.dependencies</artifactId>-->
            <!--                <version>2.1.0.RELEASE</version>-->
            <!--                <type>pom</type>-->
            <!--                <scope>import</scope>-->
            <!--            </dependency>-->

            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <version>${mysql.version}</version>
            </dependency>

            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>druid</artifactId>
                <version>${druid.version}</version>
            </dependency>

            <dependency>
                <groupId>org.mybatis.spring.boot</groupId>
                <artifactId>mybatis-spring-boot-starter</artifactId>
                <version>${mybatis.spring.boot.version}</version>
            </dependency>

            <!-- https://mvnrepository.com/artifact/junit/junit -->
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>${junit.version}</version>
                <scope>test</scope>
            </dependency>

            <!-- https://mvnrepository.com/artifact/log4j/log4j -->
            <dependency>
                <groupId>log4j</groupId>
                <artifactId>log4j</artifactId>
                <version>${log4j.version}</version>
            </dependency>

            <!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
            <dependency>
                <groupId>org.projectlombok</groupId>
                <artifactId>lombok</artifactId>
                <version>${lombok.version}</version>
                <scope>provided</scope>
            </dependency>

        </dependencies>
    </dependencyManagement>
    <build>
        <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
            <plugins>
                <plugin>
                    <artifactId>maven-clean-plugin</artifactId>
                    <version>3.1.0</version>
                </plugin>
                <plugin>
                    <artifactId>maven-site-plugin</artifactId>
                    <version>3.7.1</version>
                </plugin>
                <plugin>
                    <artifactId>maven-project-info-reports-plugin</artifactId>
                    <version>3.0.0</version>
                </plugin>
            </plugins>
        </pluginManagement>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>2.1.4.RELEASE</version>
                <configuration>
                    <fork>true</fork>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

在主启动类中添加一下注解:表示该model为服务注册中心的提供者

@EnableEurekaServer

在hosts文件中添加如下:hosts文件地址为->C:\Windows\System32\drivers\etc

  • 127.0.0.1       eureka7001.com
  • 127.0.0.1       eureka7002.com

修改application.yml文件

server:
  port: 7001

eureka:
  instance:
    hostname: eureka7001.com
  client:
    register-with-eureka: false
    fetch-registry: false
    service-url:
      defaultZone: http://eureka7001.com:7001/eureka

成功后的页面如下:

页面内容分析:可以发现后台页面被分为了五大部分

  • System Status 代表的系统状态
  • DS Replicas 该服务从哪里同步数据
  • Instances currently registered with Eureka 注册在Eureka的实例列表
  • General Info 系统运行环境 如cpu、内存等信息
  • Instance Info 本服务的基础信息 如ip地址 状态等

总结:搭建Eureka是非常简单的,并且Eureka是可用高可用的,比如可用集群;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值