Spring Cloud Eureka快读入门Demo

1.什么是Eureka?

Eureka 由 Netflix 开发,是一种基于REST(Representational State Transfer)的服务,用于定位服务(服务注册与发现),以实现中间层服务的负载均衡和故障转移,此服务被称为 Eureka Server。同时,它还附带了基于 Java 的客户端组件:Eureka Client,它使得客户端与 Eureka Server 的交互变得更加的容易。 以下就是一个简单的服务调用过程:

  1. 由服务提供方将服务注册到 Eureka Server
  2. 服务消费者通过 Eureka Server 获取服务提供方的真实地址
  3. 服务消费者通过真实的地址调用服务

2.代码工程

实验目的

搭建Eureka集群

pom.xml

复制代码
<?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">
    <parent>
        <artifactId>eureka</artifactId>
        <groupId>com.et</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>eureka-server</artifactId>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
            <version>${eureka.version}</version>
        </dependency>
    </dependencies>

</project>

EurekaServerApplication

复制代码
package com.et.eureka;

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

@EnableEurekaServer
@SpringBootApplication
public class EurekaServerApplication {

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

节点1配置信息

复制代码
server:
  port: 8761

eureka:
  instance:
    hostname: node1 # eureka name
    prefer-ip-address: false
  client:
    fetch-registry: false
    register-with-eureka: true
    # eureka url
    service-url:
      defaultZone: http://node2:8762/eureka
spring:
  application:
    name: "eureka-server-ha"

节点2配置信息

复制代码
server:
  port: 8762

eureka:
  instance:
    hostname: node2 # eureka name
    prefer-ip-address: false
  client:
    fetch-registry: false
    register-with-eureka: true
    # eureka url
    service-url:
      defaultZone: http://node1:8761/eureka
spring:
  application:
    name: "eureka-server-ha"

配置本机host

/etc/hosts

复制代码
127.0.0.1 node1
127.0.0.1 node2

以上只是一些关键代码,所有代码请参见下面代码仓库

代码仓库

3.测试

启动node1,node2

访问http://127.0.0.1:8761/

4.引用

相关推荐
爱尚你199318 分钟前
Java 泛型与类型擦除:为什么解析对象时能保留泛型信息?
java
全栈派森36 分钟前
云存储最佳实践
后端·python·程序人生·flask
电商数据girl41 分钟前
酒店旅游类数据采集API接口之携程数据获取地方美食品列表 获取地方美餐馆列表 景点评论
java·大数据·开发语言·python·json·旅游
CircleMouse41 分钟前
基于 RedisTemplate 的分页缓存设计
java·开发语言·后端·spring·缓存
ktkiko111 小时前
顶层架构 - 消息集群推送方案
java·开发语言·架构
zybsjn1 小时前
后端系统做国际化改造,生成多语言包
java·python·c#
Unity官方开发者社区1 小时前
《Cryptical Path》开发诀窍:像玩游戏一样开发一款类Rogue游戏
java·游戏·玩游戏
_星辰大海乀2 小时前
表的设计、聚合函数
java·数据结构·数据库·sql·mysql·数据库开发
IT成长史2 小时前
deepseek梳理java高级开发工程师微服务面试题-进阶版
java·spring cloud·微服务
紫璨月2 小时前
Feign异步模式丢失上下文问题
spring cloud·openfeign