--- Eureka 服务注册发现 ---

Euraka 是netfix开发的基于REST服务基于AP框架的注册中心,主要是用于服务的注册,管理,负载均衡,服务故障转移

Eureka主要分俩部分

Eureka Server:服务中心Server端,提供服务注册 发现 健康检查等服务

Eureka Client:服务提供者,启动之后会向Eureka Server注册自己的服务

搭建一个Eureka Server

在项目的pom文件中添加eureka server依赖

复制代码
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>

在启动类上添加注解 @EnableEurekaServer 表示这是个EurekaServer服务

再添加上配置文件信息

复制代码
server:
  port: 10010
spring:
  application:
    name: eureka-server 
eureka:
  instance:
    hostname: localhost
  client:
    fetch-registry: false #表⽰是否从Eureka Server获取注册信息,默认为true.因为这是⼀个单点的Eureka Server,不需要同步其他的Eureka Server节点的数据,这⾥设置为false
    register-with-eureka: false #表⽰是否将⾃⼰注册到Eureka Server,默认为true.由于当前应⽤就是Eureka Server,故⽽设置为false.
    #设置与Eureka Server的地址,  查询服务和注册服务都需要依赖这个地址
  service-url:
    defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

这样当服务启动之后 通过localhost:10010就能访问到eureka的服务中心页面

服务发现

添加依赖eureka client依赖

复制代码
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>

添加配置信息

复制代码
spring:
  application:
    name: product-server # 注册服务的名字
eureka:
  client:
    service-url:
      defaultZone: http://127.0.0.1:10010/eureka # 注册中心的url

调用服务

使用DiscoveryClient来获取到对应名字的服务 instance

调用的时候通过url调用

通过RestTemplate来发起http请求

复制代码
相关推荐
阿里云云原生4 天前
阿里云获评 Agentic AI 开发平台领导者,函数计算 AgentRun 赢下关键分!
云原生
阿里云云原生4 天前
MSE Nacos Prompt 管理:让 AI Agent 的核心配置真正可治理
微服务·云原生
阿里云云原生4 天前
当 AI Agent 接管手机:移动端如何进行观测
云原生·agent
阿里云云原生4 天前
AI 原生应用开源开发者沙龙·深圳站精彩回顾 & PPT下载
云原生
阿里云云原生4 天前
灵感启发:日产文章 100 篇,打造“实时热点洞察”引擎
云原生
1candobetter5 天前
Docker Compose Build 与 Up 的区别:什么时候必须重建镜像
docker·容器·eureka
~莫子5 天前
Haproxy七层负载详解+实验详细代码
云原生
阿里云云原生5 天前
OpenTelemetry + 云监控 2.0:打造你的云原生全栈可观测
云原生
阿狸猿5 天前
云原生数据库
云原生·软考
至此流年莫相忘5 天前
Kubernetes实战篇之配置与存储
云原生·容器·kubernetes