返回介绍

5.4 搭建许可服务器以使用 Spring Cloud 和 Hystrix

发布于 2025-04-22 21:54:07 字数 2256 浏览 0 评论 0 收藏

要开始对 Hystrix 的探索,需要创建项目的 pom.xml 文件来导入 Spring Hystrix 依赖项。我们将使用之前一直在构建的许可证服务,并通过添加 Hystrix 的 Maven 依赖项来修改 pom.xml 文件:

<dependency>
  <groupId>org.springframework.cloud</groupId>
  <artifactId>spring-cloud-starter-hystrix</artifactId>
</dependency>
<dependency>
  <groupId>com.netflix.hystrix</groupId>
  <artifactId>hystrix-javanica</artifactId>
  <version>1.5.9</version>
</dependency>

第一个 <dependency> 标签(spring-cloud-starter-hystrix)告诉 Maven 去拉取 Spring Cloud Hystrix 依赖项。第二个 <dependency> 标签(hystrix-javanica)将拉取核心 Netflix Hystrix 库。创建完 Maven 依赖项后,我们可以继续,使用在前几章中构建的许可证服务和组织服务来开始 Hystrix 的实现。

注意

读者不一定要在 pom.xml 中直接包含 hystrix-javanica 依赖项。在默认情况下,spring-cloud- starter-hystrix 包括一个 hystrix-javanica 依赖项的版本。本书使用的 Camden.SR5 发行版本使用了 hystrix-javanica-1.5.6。这个 hystrix-javanica 的版本有一个不一致的地方,它导致 Hystrix 代码在没有后备的情况下会抛出 java.lang.reflect.UndeclaredThrowableException 而不是 com.netflix.hystrix.exception.HystrixRuntimeException 。对于使用旧版 Hystrix 的许多开发人员来说,这是一个破坏性的变化。hystrix-javanica 库在后来的版本中解决了这个问题,所以我专门使用了更高版本的 hystrix-javanica,而不是使用 Spring Cloud 引入的默认版本。

在应用程序代码中开始使用 Hystrix 断路器之前,需要完成的最后一件事情是,使用 @EnableCircuitBreaker 注解来标注服务的引导类。例如,对于许可证服务,最好将 @EnableCircuitBreaker 注解添加到 licensing-service/src/main/java/com/thoughtmechanix/licenses/Application.java 中。代码清单 5-1 展示了这段代码。

代码清单 5-1 用于在服务中激活 Hystrix 的 @EnableCircuitBreaker 注解

package com.thoughtmechanix.licenses

import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
// 为了简洁,省略了其余的 import 语句

@SpringBootApplication
@EnableEurekaClient
@EnableCircuitBreaker  ⇽---  告诉 Spring Cloud 将要为服务使用 Hystrix
public class Application {
    @LoadBalanced
    @Bean
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }

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

注意

如果忘记将 @EnableCircuitBreaker 注解添加到引导类中,那么 Hystrix 断路器不会处于活动状态。在服务启动时,不会收到任何警告或错误消息。

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。