SpringCloud災(zāi)難性雪崩效應(yīng)處理方法之降級實(shí)現(xiàn)流程詳解
一.前言
解決服務(wù)雪崩效應(yīng),都是避免application client請求application service時,出現(xiàn)服務(wù)調(diào)用錯誤或網(wǎng)絡(luò)問題。處理手法都是在application client中實(shí)現(xiàn)。我們需要在application client相關(guān)工程中導(dǎo)入hystrix依賴信息。并在對應(yīng)的啟動類上增加新的注解@EnableCircuitBreaker,這個注解是用于開啟hystrix熔斷器的,簡言之,就是讓代碼中的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新建啟動類
@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新建啟動類
@SpringBootApplication
@EnableCircuitBreaker
public class ApplicationClientApplication {
public static void main(String[] args) {
SpringApplication.run(ApplicationclientdemoApplication.class, args);
}
}2.7訪問
在瀏覽器輸入http://localhost:8081/demo
3.測試降級
停止ApplicationServiceDemo項(xiàng)目。再次訪問
到此這篇關(guān)于SpringCloud災(zāi)難性雪崩效應(yīng)處理方法之降級實(shí)現(xiàn)流程詳解的文章就介紹到這了,更多相關(guān)SpringCloud降級內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- SpringCloud Feign隔離與降級詳細(xì)分析
- SpringCloud hystrix服務(wù)降級學(xué)習(xí)筆記
- SpringCloud?hystrix斷路器與局部降級全面介紹
- SpringCloud hystrix服務(wù)降級概念介紹
- SpringCloud降級規(guī)則使用介紹
- SpringCloud-Alibaba-Sentinel服務(wù)降級,熱點(diǎn)限流,服務(wù)熔斷
- springcloud 服務(wù)降級的實(shí)現(xiàn)方法
- 詳解springcloud 基于feign的服務(wù)接口的統(tǒng)一hystrix降級處理
- springcloud使用Hystrix進(jìn)行微服務(wù)降級管理
相關(guān)文章
使用IntelliJ IDEA調(diào)式Stream流的方法步驟
本文主要介紹了使用IntelliJ IDEA調(diào)式Stream流的方法步驟,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-05-05
mybatis中使用InsertProvider注解報(bào)錯解決全過程
這篇文章主要介紹了mybatis中使用InsertProvider注解報(bào)錯解決全過程,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-07-07
Dubbo擴(kuò)展點(diǎn)SPI實(shí)踐示例解析
這篇文章主要為大家介紹了Dubbo擴(kuò)展點(diǎn)SPI實(shí)踐示例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-10-10
java使用google身份驗(yàn)證器實(shí)現(xiàn)動態(tài)口令驗(yàn)證的示例
本篇文章主要介紹了java使用google身份驗(yàn)證器實(shí)現(xiàn)動態(tài)口令驗(yàn)證的示例,具有一定的參考價(jià)值,有興趣的可以了解一下2017-08-08
Json轉(zhuǎn)化為Java對象的實(shí)例詳解
這篇文章主要介紹了Json轉(zhuǎn)化為Java對象的實(shí)例詳解的相關(guān)資料,前后端數(shù)據(jù)交互的情況經(jīng)常會遇到Json串與java 對象的相互轉(zhuǎn)換方便操作,需要的朋友可以參考下2017-08-08
使用spring-data-redis中的Redis事務(wù)
這篇文章主要介紹了使用spring-data-redis中的Redis事務(wù),具有很好的參考價(jià)值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-07-07
你必須得會的SpringBoot全局統(tǒng)一處理異常詳解
程序在運(yùn)行的過程中,不可避免會產(chǎn)生各種各樣的錯誤,這個時候就需要進(jìn)行異常處理,本文主要為大家介紹了SpringBoot實(shí)現(xiàn)全局統(tǒng)一處理異常的方法,需要的可以參考一下2023-06-06
圖數(shù)據(jù)庫NebulaGraph的Java 數(shù)據(jù)解析實(shí)踐與指導(dǎo)詳解
這篇文章主要介紹了圖數(shù)據(jù)庫NebulaGraph的Java 數(shù)據(jù)解析實(shí)踐與指導(dǎo)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-04-04

