Eurek两个重要组件

Eurek两个重要组件分别是Eureka Server和Eureka Client

Eureka Server提供服务注册

各个微服务节点通过配置启动后,会在EurekaServer中进行注册,这样EruekaServer注册表中会存储所有可用服务节点的信息,服务节点的信息可以在界面中直观的看到

Eureka Client通过注册中心进行访问

是一个Java客户端,用于简化Eureka Server的交互,客户端同时也具备一个内置的、使用轮询(round-robin)负载算法的负载均衡器。在应用启动后,将会向Eurek Server发送心跳(默认周期是30秒)。如果EurekaServer在多个心跳周期内没有接收到某个节点的心跳。Eureka将会从注册表中把这个服务节点移除(默认90秒)

好的,以下是一个简单的 Spring MVC 项目中集成 Eureka 和 Feign 的示例: 1. 引入依赖 首先,需要在项目的 pom.xml 文件中引入 Spring Cloud Eureka 和 Spring Cloud Feign 相关的依赖: ```xml <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> <version>2.2.2.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-feign</artifactId> <version>2.2.2.RELEASE</version> </dependency> ``` 2. 添加配置 接下来,需要在项目的配置文件中添加 Eureka Server 和 Feign Client 相关的配置: ```yaml spring: application: name: sample-service # 应用程序名称,注册到 Eureka Server 中 cloud: config: uri: http://localhost:8888 # Config Server 的地址 profile: dev # 配置文件的环境 label: master # Git 仓库的分支 eureka: client: serviceUrl: defaultZone: http://localhost:8761/eureka/ # Eureka Server 的地址 feign: client: config: default: connectTimeout: 5000 # 连接超时时间 readTimeout: 5000 # 读取超时时间 ``` 这里假设 Config Server 的地址为 http://localhost:8888,Eureka Server 的地址为 http://localhost:8761/eureka/。 3. 创建 Feign Client 接下来,需要创建一个 Feign Client,用于访问另一个服务。这里以访问名为 `sample-service` 的服务为例: ```java @FeignClient(name = "sample-service") public interface SampleServiceClient { @GetMapping("/foo") String getFoo(); } ``` 这里使用 `@FeignClient` 注解来创建一个 Feign Client,指定服务名为 `sample-service`。`getFoo` 方法用于访问服务中的 `/foo` 接口,返回值为字符串类型。 4. 注册服务 最后,在项目启动时,需要将应用程序注册到 Eureka Server 中: ```java @SpringBootApplication @EnableDiscoveryClient @EnableFeignClients public class SampleApplication { public static void main(String[] args) { SpringApplication.run(SampleApplication.class, args); } } ``` 这里使用 `@EnableDiscoveryClient` 注解来启用服务注册功能,使用 `@EnableFeignClients` 注解来启用 Feign Client 功能。 需要注意的是,为了能够动态刷新配置,还需要在 `SampleServiceClient` 类上添加 `@RefreshScope` 注解。这样,在配置发生变化时,可以通过调用 `/actuator/refresh` 接口来刷新配置。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值