亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Spring Cloud Alibaba 使用 Feign+Sentinel 完成熔斷的示例

 更新時間:2021年03月13日 10:12:24   作者:編碼妙♂妙♂屋  
這篇文章主要介紹了Spring Cloud Alibaba 使用 Feign+Sentinel 完成熔斷的示例,幫助大家更好的理解和學(xué)習(xí)使用Spring Cloud,感興趣的朋友可以了解下

Feign的使用

Feign也是網(wǎng)飛開發(fā)的,SpringCloud 使用 Feign 非常簡單,我下邊演示一下:
首先 服務(wù)消費者這邊肯定需要一個對應(yīng)的依賴:

compile("org.springframework.cloud:spring-cloud-starter-openfeign")

需要啟用Feign的話,也得在啟動類上面加個注解 @EnableFeignClients
然后,創(chuàng)建一個 Feign 的接口,像這樣子

package com.skypyb.sc.feign;
import com.skypyb.sc.entity.User;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@FeignClient("sc-demo-microservice-user")
public interface UserFeignClient {
 @RequestMapping(value = "/user/{id}",method = RequestMethod.GET)
 public User getUser(@PathVariable("id") Long id);
}

@FeignClient 注解里邊的默認屬性,也就是name屬性是一個客戶端的名字,如果使用了Eureka的話,會給他自動解析為 Eureka Server 服務(wù)注冊表中的服務(wù)。
要是配了Ribbon,也會使用默認的負載均衡策略來執(zhí)行請求。
Feign默認使用SpringMVC的注解聲明請求,當然也可以用Feign自帶的注解。不過沒啥必要,還需要配置東西。
 
我上邊這個例子寫完了,實際使用的話只需要注入該類然后調(diào)用對應(yīng)的方法就完事了。非常簡便。

package com.skypyb.sc.controller;
import com.skypyb.sc.entity.User;
import com.skypyb.sc.feign.UserFeignClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import java.util.List;
@RestController
@RequestMapping("/movie")
public class MovieController {
 private Logger logger = LoggerFactory.getLogger(MovieController.class);
 @Autowired
 private UserFeignClient userFeignClient;
 @GetMapping("/user/{id}")
 public User getUser(@PathVariable("id") Long id) {
  return userFeignClient.getUser(id);
 }
}

不過有幾個點需要注意
在使用@FeignClient 聲明的Feign偽裝類中:
使用 @PathVariable 注解,必須加上參數(shù)!
GET請求無法使用對象作為入?yún)ⅲ?要不有多少參數(shù)寫多少參數(shù)(每個都要加@RequestParam(“參數(shù)名”) 注解),要不就用接受一個Map對象,也得加@RequestParam
POST請求可以接收對象,需要加上@RequestBody注解
 
 
Feign論使用的話,其實已經(jīng)差不多了。
但是還有一些相關(guān)的操作也比較重要。
 
Feign 的請求都是使用的默認配置,我們其實可以實現(xiàn)自己的配置供 Feign 使用以實現(xiàn)編碼、解碼、日志記錄、驗證 等等等等。
比如我可以這么定義一個配置類:

package com.skypyb.sc.config;
import feign.Logger;
import feign.auth.BasicAuthRequestInterceptor;
import org.springframework.context.annotation.Bean;
public class FeignConfiguration {
 @Bean
 public Logger.Level feignLog() {
  return Logger.Level.FULL;
 }
 /**
  * 使用指定的用戶名和密碼驗證所有請求
  * @return
  */
 @Bean
 public BasicAuthRequestInterceptor basicAuthRequestInterceptor(){
  return new BasicAuthRequestInterceptor("user","614");
 }
}

該類配置了對應(yīng)的日志記錄器,和一個簡單的效驗,以應(yīng)對請求的驗證。
在對應(yīng)的Feign偽裝類中,上邊的@FeignClient注解改一下,就可以使用自己寫的配置:

@FeignClient(name = "sc-demo-microservice-user", configuration = FeignConfiguration.class)

之所以在 @FeignClient 注解中指定配置,是因為我的配置類是沒有加 @Configuration 注解的,我想要實現(xiàn)細粒度的控制。 推薦這樣做。
如果加了@Configuration 注解,則 Spring 會將其自動解析自動應(yīng)用到全局,這樣子就不方便為每個請求進行細粒度調(diào)整。

Alibaba的使用

首先肯定是要上pom.xml配置起來。加上對應(yīng)的依賴。

  <dependency>
   <groupId>com.alibaba.cloud</groupId>
   <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
  </dependency>
  <dependency>
   <groupId>org.springframework.cloud</groupId>
   <artifactId>spring-cloud-starter-openfeign</artifactId>
  </dependency>

接著寫個Feign接口

package com.skypyb.provider.feign;
import com.skypyb.provider.fallback.HelloServiceFallback;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
/**
 * Feign客戶端
 * 指定調(diào)用 sc-demo-alibaba-provider 的服務(wù)
 */
@FeignClient(value = "sc-demo-alibaba-provider",fallback = HelloServiceFallback.class)
public interface NacosHelloFeign {
 @RequestMapping(value = "/provider/hello/{msg}")
 String hello(@PathVariable("msg") String msg);
}

調(diào)用的話就是這樣子調(diào)用:

package com.skypyb.provider.controller;
import com.skypyb.provider.feign.NacosHelloFeign;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import javax.annotation.Resource;
@RequestMapping("/consumer")
@RestController
public class NacosConsumerController {
 @Autowired
 private LoadBalancerClient loadBalancerClient;
 @Autowired
 private RestTemplate restTemplate;
 @Resource
 private NacosHelloFeign nacosHelloFeign;
 @GetMapping(value = "/hello/{msg}")
 public String hello(@PathVariable("msg") String msg) {
  //使用 LoadBalanceClient 和 RestTemplate 結(jié)合的方式來訪問
  ServiceInstance serviceInstance = loadBalancerClient.choose("sc-demo-alibaba-provider");
  String url = String.format("http://%s:%s/provider/hello/%s",
    serviceInstance.getHost(), serviceInstance.getPort(), msg);
  return restTemplate.getForObject(url, String.class);
 }
 @GetMapping(value = "/hello/feign/{msg}")
 public String helloFeign(@PathVariable("msg") String msg) {
  return nacosHelloFeign.hello(msg);
 }
}

哎,觀察我的NacosHelloFeign類,這里可以看到,我這用了一個fallback回退,這個回退指定的就是Sentinel 的實現(xiàn),其實寫起來和特么的Hystrix一模一樣。不得不說SpringCloud這一點是真的做得好。

package com.skypyb.provider.fallback;
import com.skypyb.provider.feign.NacosHelloFeign;
import org.springframework.stereotype.Component;
/**
 * 熔斷類
 * 要是 Feign 的接口調(diào)用失敗(或者被快速失敗)就會走這個類的方法進行處理
 */
@Component
public class HelloServiceFallback implements NacosHelloFeign {
 @Override
 public String hello(String msg) {
  return "觸發(fā)熔斷機制~";
 }
}

哎,觀察我的NacosHelloFeign類,這里可以看到,我這用了一個fallback回退,這個回退指定的就是Sentinel 的實現(xiàn),其實寫起來和特么的Hystrix一模一樣。不得不說SpringCloud這一點是真的做得好。

package com.skypyb.provider.fallback;
import com.skypyb.provider.feign.NacosHelloFeign;
import org.springframework.stereotype.Component;
/**
 * 熔斷類
 * 要是 Feign 的接口調(diào)用失敗(或者被快速失敗)就會走這個類的方法進行處理
 */
@Component
public class HelloServiceFallback implements NacosHelloFeign {
 @Override
 public String hello(String msg) {
  return "觸發(fā)熔斷機制~";
 }
}

當然,配置肯定是得配置的,他也不會直接就給你用了。
Sentinel 雖說是適配了 Feign 組件。但默認是關(guān)閉的。需要在配置文件中配置打開它,在配置文件增加以下代碼:

feign:
 sentinel:
 enabled: true

其實到現(xiàn)在,整個Feign的使用+熔斷限流機制就已經(jīng)配完了。
不過Sentinel 比起Hystrix真正優(yōu)越的地方還沒來呢。
那就是: 控制臺。
這個控制臺功能豐富、UI好看,比Hystrix那是高到不知道哪里去了。
要是用控制臺,又不得不下載代碼、打包、編譯、運行。
 
所幸,我們有神器docker。我在docker hub 上找了個鏡像,直接用就可以了,鏡像地址:https://hub.docker.com/r/bladex/sentinel-dashboard
直接執(zhí)以下命令,將sentinel控制臺起開綁到8858端口

docker pull bladex/sentinel-dashboard
docker run --name sentinel -d -p 8858:8858 -d bladex/sentinel-dashboard

然后自己的yml文件也得改一下,畢竟都上控制臺了,也得把自己這個服務(wù)給注冊進去。
全部的yml文件如下所示:

spring:
 application:
 name: sc-demo-alibaba-consumer
 cloud:
 nacos:
  discovery:
  server-addr: 192.168.1.14:8848 #注冊進 nacos
 sentinel:
   transport:
   port: 18081 #這個端口的意思是自己這個服務(wù)開個端口和 sentinel 控制臺交互
   dashboard: 192.168.1.14:8858 # sentinel 控制臺的端口
server:
 port: 8081
feign:
 sentinel:
 enabled: true
management:
 endpoints:
 web:
  exposure:
  include: "*"

可以看到配置里有一個 sentinel.transport.port屬性,這個屬性的意思是在自己本體這個服務(wù)里邊,在開個新的端口專門用來和 Sentinel 控制臺交互
Sentinel 控制臺自身也有,默認端口則是8719
 
到這里就差不多了,所有東西打開,然后就可以訪問 http://ip:8858 進入Sentinel 控制臺,默認賬號密碼都是sentinel
有一點還需要注意,那就是為了確保客戶端有訪問量,Sentinel 會在客戶端首次調(diào)用的時候進行初始化,開始向控制臺發(fā)送心跳包。意思就是說你一個服務(wù)注冊進我這來,首先還是看不到的。
你得先經(jīng)過別人的請求,我才會去監(jiān)控你,所以在服務(wù)剛啟動的時候進入Sentinel 控制臺找不到自己的服務(wù)是很正常的,只要啟動時沒有報錯就不會有問題。

以上就是Spring Cloud Alibaba 使用 Feign+Sentinel 完成熔斷的示例的詳細內(nèi)容,更多關(guān)于Spring Cloud Alibaba 使用Feign+Sentinel 完成熔斷的資料請關(guān)注腳本之家其它相關(guān)文章!

您可能感興趣的文章:

相關(guān)文章

  • Java中的ObjectOutputStream類使用

    Java中的ObjectOutputStream類使用

    ObjectOutputStream是Java.io包中的一個類,用于將Java對象的狀態(tài)信息序列化為字節(jié)流,序列化是將對象狀態(tài)轉(zhuǎn)換為字節(jié)流的過程,反序列化則是將字節(jié)流恢復(fù)為對象,本文介紹了ObjectOutputStream的原理、主要方法、使用步驟以及注意事項
    2024-09-09
  • 如何修改JSON字符串中的敏感信息

    如何修改JSON字符串中的敏感信息

    這篇文章主要介紹了如何修改JSON字符串中的敏感信息,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-12-12
  • SpringMVC請求、響應(yīng)和攔截器的使用實例詳解

    SpringMVC請求、響應(yīng)和攔截器的使用實例詳解

    攔截器(Interceptor) 它是一個Spring組件,并由Spring容器管理,并不依賴Tomcat等容器,是可以單獨使用的,這篇文章給大家介紹SpringMVC請求、響應(yīng)和攔截器的使用,感興趣的朋友一起看看吧
    2024-03-03
  • idea springboot遠程debug的操作方法

    idea springboot遠程debug的操作方法

    這篇文章主要介紹了idea springboot遠程debug的操作方法,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-10-10
  • Java四種權(quán)限修飾符知識點詳解

    Java四種權(quán)限修飾符知識點詳解

    在本篇文章里小編給大家分享的是關(guān)于Java四種權(quán)限修飾符知識點詳解內(nèi)容,需要的朋友們可以參考下。
    2020-05-05
  • JAVA新手小白學(xué)正則表達式、包裝類、自動裝箱/自動拆箱以及BigDecimal

    JAVA新手小白學(xué)正則表達式、包裝類、自動裝箱/自動拆箱以及BigDecimal

    這篇文章主要給大家介紹了關(guān)于JAVA新手小白學(xué)正則表達式、包裝類、自動裝箱/自動拆箱以及BigDecimal的相關(guān)資料,文中通過實例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2022-03-03
  • springboot2.0+elasticsearch5.5+rabbitmq搭建搜索服務(wù)的坑

    springboot2.0+elasticsearch5.5+rabbitmq搭建搜索服務(wù)的坑

    這篇文章主要介紹了springboot2.0+elasticsearch5.5+rabbitmq搭建搜索服務(wù)的坑,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-06-06
  • java自加和自減運算過程

    java自加和自減運算過程

    這篇文章主要介紹了java自加和自減運算過程,非常不錯,具有參考借鑒價值,需要的朋友可以參考下
    2017-03-03
  • SpringBoot feign動態(tài)設(shè)置數(shù)據(jù)源(https請求)

    SpringBoot feign動態(tài)設(shè)置數(shù)據(jù)源(https請求)

    這篇文章主要介紹了SpringBoot如何在運行時feign動態(tài)添加數(shù)據(jù)源,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2021-08-08
  • 常用Maven庫,鏡像庫及maven/gradle配置(小結(jié))

    常用Maven庫,鏡像庫及maven/gradle配置(小結(jié))

    這篇文章主要介紹了常用Maven庫,鏡像庫及maven/gradle配置(小結(jié)),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-12-12

最新評論