SpringCloud灾难性雪崩效应处理方法之降级实现流程详解
小虾仁芜湖 人气:0一.前言
解决服务雪崩效应,都是避免application client请求application service时,出现服务调用错误或网络问题。处理手法都是在application client中实现。我们需要在application client相关工程中导入hystrix依赖信息。并在对应的启动类上增加新的注解@EnableCircuitBreaker,这个注解是用于开启hystrix熔断器的,简言之,就是让代码中的hystrix相关注解生效。
二.代码实现
1.新建ApplicationServiceDemo
1.1配置pom.xml
需要web和eureka-client
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.3.5.RELEASE</version> </parent> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Hoxton.SR9</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> </dependencies>
1.2新建配置文件
spring:
application:
name: application-service-demo
eureka:
client:
service-url:
defaultZone: http://eurekaserver1:8761/eureka/
1.3新建控制器
@Controller public class DemoController { @RequestMapping("/demo") @ResponseBody public String demo(){ return "demo-service"; } }
1.4新建启动类
@SpringBootApplication public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class,args); } }
2.新建DemoFallback
新建项目DemoFallback作为Application Client
2.1编写pom.xml
需要配置web,eureka-client,netflix-hystrix
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.3.5.RELEASE</version> </parent> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Hoxton.SR9</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </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> </dependencies>
2.2新建配置文件
spring:
application:
name: fallback-demo
eureka:
client:
service-url:
defaultZone: http://eurekaserver1:8761/eureka/
server:
port: 8081
2.3新建配置类
@Configuration public class RibbonConfig { @Bean @LoadBalanced public RestTemplate restTemplate(){ return new RestTemplate(); } }
2.4新建service及实现类
public interface DemoService { String test(); }
@Service public class DemoServiceImpl implements DemoService { @Autowired private RestTemplate restTemplate; @HystrixCommand(fallbackMethod = "myFallback") @Override public String test() { String result = restTemplate.postForObject("http://application-service-demo/demo", null, String.class); System.out.println(result); return result; } public String myFallback(){ return "托底数据"; } }
2.5新建控制器
@Controller public class FallbackController { @Autowired private DemoService demoService; @RequestMapping("/demo") @ResponseBody public String demo(){ return demoService.test(); } }
2.6新建启动类
@SpringBootApplication @EnableCircuitBreaker public class ApplicationClientApplication { public static void main(String[] args) { SpringApplication.run(ApplicationclientdemoApplication.class, args); } }
2.7访问
在浏览器输入http://localhost:8081/demo
3.测试降级
停止ApplicationServiceDemo项目。再次访问
加载全部内容