SpringCloud災(zāi)難性雪崩效應(yīng)處理方法之降級(jí)實(shí)現(xiàn)流程詳解
一.前言
解決服務(wù)雪崩效應(yīng),都是避免application client請(qǐng)求application service時(shí),出現(xiàn)服務(wù)調(diào)用錯(cuò)誤或網(wǎng)絡(luò)問題。處理手法都是在application client中實(shí)現(xiàn)。我們需要在application client相關(guān)工程中導(dǎo)入hystrix依賴信息。并在對(duì)應(yīng)的啟動(dòng)類上增加新的注解@EnableCircuitBreaker,這個(gè)注解是用于開啟hystrix熔斷器的,簡(jiǎn)言之,就是讓代碼中的hystrix相關(guān)注解生效。
二.代碼實(shí)現(xiàn)
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新建啟動(dòng)類
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class,args);
}
}2.新建DemoFallback
新建項(xiàng)目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及實(shí)現(xiàn)類
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 "托底數(shù)據(jù)";
}
}2.5新建控制器
@Controller
public class FallbackController {
@Autowired
private DemoService demoService;
@RequestMapping("/demo")
@ResponseBody
public String demo(){
return demoService.test();
}
}
2.6新建啟動(dòng)類
@SpringBootApplication
@EnableCircuitBreaker
public class ApplicationClientApplication {
public static void main(String[] args) {
SpringApplication.run(ApplicationclientdemoApplication.class, args);
}
}2.7訪問
在瀏覽器輸入http://localhost:8081/demo
3.測(cè)試降級(jí)
停止ApplicationServiceDemo項(xiàng)目。再次訪問
到此這篇關(guān)于SpringCloud災(zāi)難性雪崩效應(yīng)處理方法之降級(jí)實(shí)現(xiàn)流程詳解的文章就介紹到這了,更多相關(guān)SpringCloud降級(jí)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- SpringCloud Feign隔離與降級(jí)詳細(xì)分析
- SpringCloud hystrix服務(wù)降級(jí)學(xué)習(xí)筆記
- SpringCloud?hystrix斷路器與局部降級(jí)全面介紹
- SpringCloud hystrix服務(wù)降級(jí)概念介紹
- SpringCloud降級(jí)規(guī)則使用介紹
- SpringCloud-Alibaba-Sentinel服務(wù)降級(jí),熱點(diǎn)限流,服務(wù)熔斷
- springcloud 服務(wù)降級(jí)的實(shí)現(xiàn)方法
- 詳解springcloud 基于feign的服務(wù)接口的統(tǒng)一hystrix降級(jí)處理
- springcloud使用Hystrix進(jìn)行微服務(wù)降級(jí)管理
相關(guān)文章
Java實(shí)現(xiàn)的生成二維碼和解析二維碼URL操作示例
這篇文章主要介紹了Java實(shí)現(xiàn)的生成二維碼和解析二維碼URL操作,結(jié)合實(shí)例形式分析了Java創(chuàng)建與解析二維碼,以及文件讀寫等相關(guān)操作技巧,需要的朋友可以參考下2018-07-07
Java上轉(zhuǎn)型和下轉(zhuǎn)型對(duì)象
這篇文章給大家講述了Java上轉(zhuǎn)型和下轉(zhuǎn)型對(duì)象的詳細(xì)用法以及相關(guān)的代碼分享,有興趣的朋友可以學(xué)習(xí)下。2018-03-03
Spring事務(wù)傳播屬性和隔離級(jí)別詳細(xì)介紹
這篇文章主要介紹了Spring事務(wù)傳播屬性和隔離級(jí)別詳細(xì)介紹,同時(shí)涉及傳播行為介紹,超時(shí)設(shè)置等相關(guān)內(nèi)容,需要的朋友可以參考下。2017-09-09
Java如何根據(jù)key值修改Hashmap中的value值
這篇文章主要介紹了Java如何根據(jù)key值修改Hashmap中的value值問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-03-03
java結(jié)合email實(shí)現(xiàn)自動(dòng)推送功能
這篇文章主要介紹了java結(jié)合email實(shí)現(xiàn)自動(dòng)推送功能,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-03-03
解析SpringBoot中@Autowire注解的實(shí)現(xiàn)原理
在開發(fā)Java項(xiàng)目時(shí),依賴注入是一種常見的實(shí)現(xiàn)方式,SpringBoot框架通過@Autowired注解來實(shí)現(xiàn)依賴注入的功能,本文將介紹SpringBoot中 Autowired注解實(shí)現(xiàn)的原理2023-06-06

