本文源码:GitHub·点这里 || GitEE·点这里
一、Eureka基本架构
1、Eureka简介
Eureka是Netflix开发的服务发现框架,本身是一个基于REST的服务,SpringCloud将它集成在其子项目spring-cloud-netflix中,以实现SpringCloud的服务发现功能。
2、Eureka角色结构图

角色职责如下:
1)、Register:服务注册中心,它是一个Eureka Server ,提供服务注册和发现功能。
2)、Provider:服务提供者,它是一个Eureka Client ,提供服务。
3)、Consumer:服务消费者,它是一个Eureka Cient ,消费服务。
3、Eureka中几个核心概念
1)、Registe服务注册
当Client向Server 注册时,Client 提供自身的元数据,比如IP 地址、端口、运行状况指标的Uri 、主页地址等信息。
2)、Renew服务续约
Client 在默认的情况下会每隔30 秒发送一次心跳来进行服务续约。通过服务续约来告知Server该Client仍然可用。正常情况下,如果Server在90 秒内没有收到Client 的心跳,Server会将Client 实例从注册列表中删除。官网建议不要更改服务续约的间隔时间。
3)、Fetch Registries获取服务注册列表信息
Client 从Server 获取服务注册表信息,井将其缓存在本地。Client 会使用服务注册列表信息查找其他服务的信息,从而进行远程调用。该注册列表信息定时(每30 秒) 更新一次。
4)Cancel服务下线
Client 在程序关闭时可以向Eureka Server 发送下线请求。发送请求后,该客户端的实例信息将从Server 的服务注册列表中删除。该下线请求不会自动完成,需要在程序关闭时调用以下代码:
DiscoveryManager.getinstance().shutdownComponent();5) Eviction服务下线
在默认情况下,当Client 连续90 秒没有向Server 发送服务续约(即心跳〉时,Server 会将该服务实例从服务注册列表删除,即服务下线。
二、Eureka案例代码
1、项目基本结构图
主要包括两个注册中心(集群)node01-eureka-7001node01-eureka-7002一个服务提供方node01-provider-8001一个服务消费方node01-consume-80022、配置本机的Host文件
# cloud host127.0.0.1 registry01.com127.0.0.1 registry02.com127.0.0.1 provider-8001.com3、注册中心代码
1)Eureka注册中心依赖
<dependencies> <!--eureka-server服务端 --> <dependency> <groupId>org.spring work.cloud</groupId> <artifactId>spring-cloud-starter-eureka-server</artifactId> </dependency></dependencies>2)核心配置文件
server: port: 7001spring: application: name: node01-eureka-7001eureka: instance: hostname: registry01.com prefer-ip-address: true client: # false表示不向注册中心注册自己 register-with-eureka: false # false表示该端就是注册中心,维护服务实例,不去检索服务 fetch-registry: false service-url: # 单点注册中心 # defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/ # 集群注册中心 defaultZone: http://registry02.com:7002/eureka/这里采用集群的配置,如果有多个集群,逗号分隔,如下写法就好:
defaultZone: http://registry02.com:7002/eureka/,http://registry02.com:7002/eureka/3)启动类注解
import org.spring work.boot.SpringApplication;import org.spring work.boot.autoconfigure.SpringBootApplication;import org.spring work.cloud.netflix.eureka.server.EnableEurekaServer;@SpringBootApplication@EnableEurekaServer // 注册中心注解public class Application_7001 { public static void main(String[] args) { SpringApplication.run(Application_7001.class,args) ; }}这样注册中心代码完成。
4)启动项目,如图
暂时没有服务注册进来。
4、服务提供方代码
1)核心依赖
<dependencies> <!-- 将微服务provider侧注册进eureka --> <dependency> <groupId>org.spring work.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency></dependencies>2)配置文件如下
server: port: 8003spring: application: name: node01-provider-8001eureka: instance: hostname: provider-8001 prefer-ip-address: true client: service-url: # 集群注册中心 defaultZone: http://registry01.com:7001/eureka/,http://registry02.com:7002/eureka/3)提供一个接口服务
@RestControllerpublic class ProviderController { @RequestMapping("/getInfo") public Map<String,String> getInfo (){ Map<String,String> infoMap = new HashMap<>() ; infoMap.put("作者:","知了一笑") ; infoMap.put("时间:","2019-05-18") ; infoMap.put("主题:","SpringCloud微服务框架") ; return infoMap ; }}4)启动类上需要注解
import org.spring work.boot.SpringApplication;import org.spring work.boot.autoconfigure.SpringBootApplication;import org.spring work.cloud.client.discovery.EnableDiscoveryClient;import org.spring work.cloud.netflix.eureka.EnableEurekaClient;@SpringBootApplication@EnableEurekaClient // 本服务启动后会自动注册进eureka服务中@EnableDiscoveryClientpublic class Application_8001 { public static void main(String[] args) { SpringApplication.run(Application_8001.class,args) ; }}5、服务消费方代码
服务消费方和提供方的代码逻辑基本一致。
1)配置文件
这里不需要注册自己,只是单纯从注册中心获取服务提供方的消息。
server: port: 8002spring: application: name: node01-consume-8002eureka: client: register-with-eureka: false service-url: # 集群注册中心 defaultZone: http://registry01.com:7001/eureka/,http://registry02.com:7002/eureka/2)消费服务代码
注意这里的【server_name】就服务提供方的
spring: application: name: node01-provider-8001使用RestTemplate调用服务。
@RestControllerpublic class ConsumeController { @Autowired private RestTemplate restTemplate ; String server_name = "node01-provider-8001" ; @RequestMapping("/showInfo") public Map<String,String> showInfo (){ return restTemplate.getFor ("http://"+server_name+":8001/getInfo",Map.class) ; }}到这里,一个基于Eureka的服务注册与发现就完成了。
3)四个服务全部启动
4)访问如下地址
http://localhost:8002/showInfo获取服务接口结果
{ "主题:": "SpringCloud微服务框架", "作者:": "知了一笑", "时间:": "2019-05-18"}三、源代码案例
GitHub地址:知了一笑https://github.com/cicadasmile/spring-cloud- 码云地址:知了一笑https://gitee.com/cicadasmile/spring-cloud- 继续阅读与本文标签相同的文章
-
【阿里云新品发布·周刊】第27期:函数计算2.0重磅发布!直击函数计算核心价值
2026-05-18栏目: 教程
-
2019 DevOps 必备面试题——容器化和虚拟化
2026-05-18栏目: 教程
-
从零开始入门 K8s| K8s 的应用编排与管理 | 9月23号栖夜读
2026-05-18栏目: 教程
-
实习生4面美团Java岗,已拿offer!(框架+多线程+集合+JVM)
2026-05-18栏目: 教程
-
Flutter浪潮下的音视频研发探索
2026-05-18栏目: 教程
