日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

熔断器

發布時間:2023/12/3 编程问答 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 熔断器 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

一、引入依賴

1.consumer_service中加入依賴

<!--開啟熔斷器--> <dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-hystrix</artifactId> </dependency>

2.開啟熔斷的注解

//注解簡化寫法:微服務中,注解往往引入多個,簡化注解可以使用組合注解。@SpringCloudApplication =等同于@SpringBootApplication+@EnableDiscoveryClient+@EnableCircuitBreaker @SpringBootApplication @EnableDiscoveryClient//開啟服務發現 @EnableCircuitBreaker//開啟熔斷 public class ConsumerApplication {@Bean@LoadBalanced//開啟負載均衡public RestTemplate restTemplate(){return new RestTemplate();}public static void main(String[] args) {SpringApplication.run(ConsumerApplication.class,args);} }

3.編寫服務降級處理方法:使用@HystrixCommand定義fallback方法。

package com.william.controller;import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.cloud.client.ServiceInstance; import org.springframework.cloud.client.discovery.DiscoveryClient; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.client.RestTemplate;import javax.annotation.Resource; import java.util.List;/*** @author :lijunxuan* @date :Created in 2019/6/29 9:54* @description :* @version: 1.0*/ @RestController @RequestMapping("/consumer") public class ConsumerController {@ResourceRestTemplate restTemplate;@ResourceDiscoveryClient discoveryClient; //發現客戶端對象@RequestMapping("/findUser")@HystrixCommand(fallbackMethod ="findUserFallback")public String findUser(Integer id){//請求服務提供者的查詢用戶詳情地址// String url="http://localhost:9091/user/findById?id="+id;alt+enter 找到formatString url= String.format("http://user-service/user/findById?id=%d", id); // String url= String.format("http://localhost:9091/user/findById?id=%d", id); // /* // * 動態獲取服務注冊中心的,服務提供者地址*/ // /** // * 操作快捷鍵 // * 找到privider_service-> application.yml-> user-service 先復制 然后按住crtl +e 回到這個位置 // * discoveryClient.getInstances("user-service") ctrl +alt +v 和.var的效果相同 // * List<ServiceInstance> instances = discoveryClient.getInstances("user-service"); // */ // //獲取注冊中心的所有user-service用戶注冊實例 // List<ServiceInstance> instances = discoveryClient.getInstances("user-service"); // //獲取第一個實例 // ServiceInstance serviceInstance = instances.get(0); // int port = serviceInstance.getPort(); // String host = serviceInstance.getHost(); // url= String.format("http://%s:%d/user/findById?id=%d", host, port, id);return restTemplate.getForObject(url,String.class);}public String findUserFallback(Integer id){return "對不起,網絡太擁擠了!";}}

測試結果

需要把這兩個服務給關閉

全局的配置

在 ConsumerController配置

@DefaultProperties(defaultFallback = "defaultFallback")public String defaultFallback(){return "全局默認:對不起,網絡太擁擠了!";} 創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎

總結

以上是生活随笔為你收集整理的熔断器的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。