springcloud-03-Hystrix服务容错保护
2022/1/19 23:57:22
本文主要是介绍springcloud-03-Hystrix服务容错保护,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
1、创建Hystrix-Service服务端
引入依赖: <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.8.RELEASE</version> <relativePath/> </parent> <properties> <maven.compiler.source>8</maven.compiler.source> <maven.compiler.target>8</maven.compiler.target> <spring-cloud.version>Greenwich.SR2</spring-cloud.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-hystrix</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>${spring-cloud.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>
启动类配置: @EnableCircuitBreaker @EnableDiscoveryClient @SpringBootApplication public class HystrixServiceAppRun { public static void main(String[] args) { SpringApplication.run(HystrixServiceAppRun.class, args); } }
配置文件: spring: application: name: hystrix-service server: port: 7004 eureka: client: register-with-eureka: true fetch-registry: true service-url: defaultZone: http://localhost:7001/eureka/ service-url: order-service: http://order-service
编写一个测试接口: @RestController @RequestMapping("/order") public class OrderHystrixController { @Autowired private OrderService orderService; @GetMapping("/testFallback/{id}") public CommonResult testFallbackOrder(@PathVariable Long id) { return orderService.getOrder(id); } }
Service层代码: @Service public class OrderServiceImpl implements OrderService { @Autowired private RestTemplate restTemplate; @Value("${service-url.order-service}") private String orderServiceUrl; @HystrixCommand(fallbackMethod = "getDefaultOrder") @Override public CommonResult getOrder(Long id) { return restTemplate.getForObject(orderServiceUrl + "/order/{1}", CommonResult.class, id); } public CommonResult getDefaultOrder(@PathVariable Long id) { Order defaultOrder = new Order(); defaultOrder.setOrderId(999L); defaultOrder.setOrderName("华为手机"); return new CommonResult<>(defaultOrder); } }
启动eureka-server服务 启动order-service服务 启动hystrix-service服务
2、接口测试
调用此接口:[http://localhost:7004/order/testFallback/1](http://localhost:7004/order/testFallback/1)
然后将Order-service服务关闭,测试降级:
这篇关于springcloud-03-Hystrix服务容错保护的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-11-14后台交互资料入门指南
- 2024-11-14如何轻松创建项目环境:新手入门教程
- 2024-11-14如何抽离公共代码:初级开发者指南
- 2024-11-14Python编程入门指南
- 2024-11-14Python编程入门:如何获取参数
- 2024-11-14JWT 用户校验:简单教程与实践
- 2024-11-14Pre-commit 自动化测试入门指南
- 2024-11-14Python编程基础
- 2024-11-14Server Action入门教程:轻松掌握服务器操作
- 2024-11-14Server Component入门教程:轻松搭建服务器组件