介绍一下 Hystrix 的隔离策略,你用哪个?

典型回答 Hystrix提供两种主要的隔离策略来控制服务之间的交互,防止系统中一个组件的失败影响到其他组件。分别是线程池隔离和信号量隔离。基本上大多数场景,都是使用线程池隔离的! 线程池隔离 这是Hystrix的默认隔离策略,也是推荐方式。在这种策略中,每一个依赖服务调用都在一个单独的线程中执行,这个线程从专门为每个Hystrix命令或服务依赖配置的线程池中获取。如果线程池已满,新的请求将会被立即拒绝,不会继续等待或阻塞调用者线程。 优点: 提供了真正的任务隔离;如果依赖服务变得缓慢或无响应,只会影响到运行在同一个线程池中的请求。 可以限制并发调用的数量,避免系统过载。 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 import com.netflix.hystrix.HystrixCommand; import com.netflix.hystrix.HystrixCommandGroupKey; import com.netflix.hystrix.HystrixCommandProperties; import com.netflix.hystrix.HystrixThreadPoolProperties; public class RemoteServiceCommand extends HystrixCommand<String> { protected RemoteServiceCommand() { super(Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey("HollisExampleGroup")) .andThreadPoolPropertiesDefaults(HystrixThreadPoolProperties.Setter() .withCoreSize(10) // 核心线程数 .withMaxQueueSize(5)) // 等待队列长度 .andCommandPropertiesDefaults(HystrixCommandProperties.Setter() .withExecutionTimeoutInMilliseconds(1000))); } @Override protected String run() { // 远程服务调用 } } 信号量隔离 信号量隔离使用Java的信号量来限制并发访问的数量,而不是在不同的线程中执行命令。当请求达到信号量计数的限制时,新的请求会被立即拒绝,而不会导致线程的创建或切换。 ...

March 22, 2026 · 1 min · santu

Feign 和 RestTemplate 有什么不同?

典型回答 Feign和RestRemplate都是用于在 Java 中进行 HTTP 请求的客户端,经常被拿来对比。 RestTemplate是传统的 HTTP 客户端,提供了更多的灵活性,但需要手动编写 HTTP 请求的细节(如 URL、请求方法等)。你需要使用代码编写具体的 HTTP 请求,并手动处理响应。 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 import org.springframework.web.client.RestTemplate; import org.springframework.http.ResponseEntity; public class RestTemplateExample { public static void main(String[] args) { RestTemplate restTemplate = new RestTemplate(); String url = "https://api.example.com/data"; // 发送 GET 请求 ResponseEntity<String> response = restTemplate.getForEntity(url, String.class); System.out.println(response.getBody()); // 发送 POST 请求 String requestPayload = "{\"name\": \"John\"}"; String responsePost = restTemplate.postForObject(url, requestPayload, String.class); System.out.println(responsePost); } } **Feign 是声明式的 HTTP 客户端,**允许你通过接口定义服务调用,Feign 自动实现 HTTP 请求的细节。这种方式通常与 Spring Cloud 结合使用,可以简化微服务间的调用。 ...

March 22, 2026 · 1 min · santu

Feign和OpenFeign 有什么区别?

典型回答 Feign是由 Netflix 开发的一个 HTTP 客户端库,它简化了 Web 服务的调用。Feign 使得服务之间的 HTTP 调用变得更加简单,提供了基于注解的声明式 HTTP 请求方式。 OpenFeign是 Spring Cloud 团队对Feign的封装与扩展,它增强了Feign的功能,特别是通过与 Spring Cloud 的其他组件(如 Spring Cloud LoadBalancer, Spring Cloud Eureka, Spring Cloud Config)进行集成,实现了更强的功能和易用性。 所以可以认为**OpenFeign是 Feign 的一个“Spring Cloud 版”,并且在 Spring Cloud 中得到了维护和扩展。**OpenFeign的版本通常会紧跟 Spring Cloud 的更新(但是后来也不更新了),而 Feign是由 Netflix 维护的独立项目。 ✅OpenFeign 不支持了怎么办? 特性 Feign OpenFeign 维护者 Netflix Spring Cloud 官方 集成负载均衡 需要与 Ribbon 集成 自动集成 Spring Cloud LoadBalancer 或 Ribbon 服务发现 不支持或需要手动配置 自动集成 Spring Cloud Eureka 或 Consul 自动配置 无自动配置 提供自动配置

March 22, 2026 · 1 min · santu

OpenFeign 是如何实现负载均衡的?

OpenFeign自己是没有负载均衡的能力的,他的负载均衡是通过与 Spring Cloud LoadBalancer 集成的(以前支持Ribbon)。 主要流程:OpenFeign会通过服务名称(如 hollis-service)查询服务发现系统(如 Eureka、Consul)中注册的服务实例。Spring Cloud LoadBalancer会选择一个服务实例进行请求,并通过负载均衡策略(如轮询、随机等)来决定哪个实例处理请求。Feign 客户端会通过 Spring Cloud LoadBalancer 获取到的服务实例的地址,向该实例发起 HTTP 请求。 ✅LoadBalancer和Ribbon的区别是什么?为什么用他替代Ribbon? ✅LoadBalancer支持哪些负载均衡策略?如何修改?

March 22, 2026 · 1 min · santu

application.yml 和 bootstrap.yml 这两个配置文件有什么区别?

典型回答 application.yml 和 bootstrap.yml 是 Spring Boot / Spring Cloud 项目中常见的两种配置文件,它们的主要区别在于 加载时机、用途和作用范围。 bootstrap.yml 是 Spring Cloud 的特性,不是 Spring Boot 本身的特性。application.yml是Spring Boot的特性。所以,如果是纯Spring Boot应用,则不会加载bootstrap.yml。 加载顺序不同 **bootstrap.yml**(或 bootstrap.properties): 在 Spring 应用上下文启动之前加载。优先级 高于 application.yml。 **application.yml**(或 application.properties): 在 Spring 应用上下文创建过程中加载。 是 Spring Boot 默认的主配置文件。 bootstrap 翻译过来就是"引导程序"、“启动程序”,所以,以他命名的文件要最先开始做加载。 使用场景不同 bootstrap.yml主要用于初始化 Spring Cloud 相关的配置(如配置中心、加密解密等)。常见的就是如果你的spring 应用需要接入配置中心,比如nacos,那么nacos相关的配置就需要放在bootstrap.yml中。 bootstrap.yml典型使用场景: 使用 Spring Cloud Config Server 获取远程配置 使用 Spring Cloud Vault 管理敏感信息 使用 Nacos / Consul / Zookeeper 作为配置中心 application.yml用来配置普通的应用配置信息,如数据库连接、端口、日志级别等,还有一些业务相关配置项等。 ...

March 22, 2026 · 1 min · santu

Feign调用超时,会自动重试吗?如何设置?

典型回答 默认情况下 Feign 调用超时不会自动重试。 发生超时或其他错误时,Feign 会抛异常 。 当然,我们可以通过 **Retryer** 来启用重试机制。 Retryer 允许你配置请求失败后的重试策略。Retryer 可以配置重试次数、重试间隔等参数。你可以通过 @FeignClient 注解的 configuration 属性来配置 Feign 的重试行为。 1、 定义一个 Retryer 配置类 : 1 2 3 4 5 6 7 8 9 10 11 12 13 14 import feign.Retryer; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import java.util.concurrent.TimeUnit; @Configuration public class FeignRetryConfig { @Bean public Retryer retryer() { // 设置最大重试次数为 3,重试间隔为 1 秒 return new Retryer.Default(100, TimeUnit.SECONDS.toMillis(1), 3); } } 2、在 @FeignClient 注解中应用配置: ...

March 22, 2026 · 1 min · santu

OpenFeign如何处理超时?如何处理异常?如何记录客户端日志?

典型回答 在 Spring Cloud 中,OpenFeign 的超时配置可以通过 application.yml 或 application.properties 文件来设置: 1 2 3 4 5 6 feign: client: config: default: connectTimeout: 5000 # 连接超时,单位:毫秒 readTimeout: 5000 # 读取超时,单位:毫秒 OpenFeign 提供了 ErrorDecoder 接口来处理请求异常的情况。我们可以自定义 ErrorDecoder 来捕获不同的 HTTP 状态码并处理相应的逻辑。 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 @Configuration public class FeignConfig { @Bean public ErrorDecoder errorDecoder() { return new ErrorDecoder.Default(); // 可以替换为自定义的 ErrorDecoder } } @FeignClient(name = "hollis-service", configuration = FeignConfig.class) public interface ExampleServiceClient { @GetMapping("/api/data") String getData(); } 可以在 application.yml 或 application.properties 中配置 Feign 的日志级别, 还可以自定义日志记录 ...

March 22, 2026 · 1 min · santu

留言给博主