企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
<mark>服务熔断处理是在服务端实现的,与消费端没有关系</mark>。为了方便演示熔断效果,在这里搭建一个服务端模块。 <br/> 步骤如下: **1. 构建服务端模块:cloud-provider-hystrix-payment8001** **2. 在当前模块的`pom.xml`中添加 hystrix 依赖** ```xml <dependencies> <!--hystrix --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-hystrix</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> ... </dependencies> ``` **3. 当前模块的`resources/application.yml`** ```yml server: port: 8001 eureka: client: service-url: defaultZone: http://www.eureka7001.com:7001/eureka/,http://www.eureka7002.com:7002/eureka/ spring: application: name: cloud-payment-service ``` **4. 在当前模块的启动类上标注注解`@EnableCircuitBreaker`** ```java @SpringBootApplication @EnableDiscoveryClient @EnableCircuitBreaker public class PaymentHystrixMain8001 { public static void main(String[] args) { SpringApplication.run(PaymentHystrixMain8001.class, args); } /** * 此配置是为了服务监控而配置,与服务容错本身无关,springcloud升级后的坑 * ServletRegistrationBean因为springboot的默认路径不是/hystrix.stream * 只要在自己的项目里配置下面的Servlet就可以了 */ @Bean public ServletRegistrationBean getServlet() { HystrixMetricsStreamServlet streamServlet = new HystrixMetricsStreamServlet(); ServletRegistrationBean registrationBean = new ServletRegistrationBean(streamServlet); registrationBean.setLoadOnStartup(1); registrationBean.addUrlMappings("/hystrix.stream"); registrationBean.setName("HystrixMetricsStreamServlet"); return registrationBean; } } ``` **5. 构建完成!**