激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|JavaScript|易語言|

服務器之家 - 編程語言 - Java教程 - Spring Cloud Alibaba 使用 Feign+Sentinel 完成熔斷的示例

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

2021-08-24 10:56編碼妙♂妙♂屋 Java教程

這篇文章主要介紹了Spring Cloud Alibaba 使用 Feign+Sentinel 完成熔斷的示例,幫助大家更好的理解和學習使用Spring Cloud,感興趣的朋友可以了解下

Feign的使用

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

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

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

?
1
2
3
4
5
6
7
8
9
10
11
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 服務注冊表中的服務。
要是配了Ribbon,也會使用默認的負載均衡策略來執行請求。
Feign默認使用SpringMVC的注解聲明請求,當然也可以用Feign自帶的注解。不過沒啥必要,還需要配置東西。
 
我上邊這個例子寫完了,實際使用的話只需要注入該類然后調用對應的方法就完事了。非常簡便。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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 注解,必須加上參數!
GET請求無法使用對象作為入參! 要不有多少參數寫多少參數(每個都要加@RequestParam(“參數名”) 注解),要不就用接受一個Map對象,也得加@RequestParam
POST請求可以接收對象,需要加上@RequestBody注解
 
 
Feign論使用的話,其實已經差不多了。
但是還有一些相關的操作也比較重要。
 
Feign 的請求都是使用的默認配置,我們其實可以實現自己的配置供 Feign 使用以實現編碼、解碼、日志記錄、驗證 等等等等。
比如我可以這么定義一個配置類:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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");
 }
}

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

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

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

Alibaba的使用

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

?
1
2
3
4
5
6
7
8
<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接口

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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客戶端
 * 指定調用 sc-demo-alibaba-provider 的服務
 */
@FeignClient(value = "sc-demo-alibaba-provider",fallback = HelloServiceFallback.class)
public interface NacosHelloFeign {
 @RequestMapping(value = "/provider/hello/{msg}")
 String hello(@PathVariable("msg") String msg);
}

調用的話就是這樣子調用:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
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 結合的方式來訪問
  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 的實現,其實寫起來和特么的Hystrix一模一樣。不得不說SpringCloud這一點是真的做得好。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
package com.skypyb.provider.fallback;
import com.skypyb.provider.feign.NacosHelloFeign;
import org.springframework.stereotype.Component;
/**
 * 熔斷類
 * 要是 Feign 的接口調用失敗(或者被快速失敗)就會走這個類的方法進行處理
 */
@Component
public class HelloServiceFallback implements NacosHelloFeign {
 @Override
 public String hello(String msg) {
  return "觸發熔斷機制~";
 }
}

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
package com.skypyb.provider.fallback;
import com.skypyb.provider.feign.NacosHelloFeign;
import org.springframework.stereotype.Component;
/**
 * 熔斷類
 * 要是 Feign 的接口調用失敗(或者被快速失敗)就會走這個類的方法進行處理
 */
@Component
public class HelloServiceFallback implements NacosHelloFeign {
 @Override
 public String hello(String msg) {
  return "觸發熔斷機制~";
 }
}

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

?
1
2
3
feign:
 sentinel:
 enabled: true

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

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

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
spring:
 application:
 name: sc-demo-alibaba-consumer
 cloud:
 nacos:
  discovery:
  server-addr: 192.168.1.14:8848 #注冊進 nacos
 sentinel:
   transport:
   port: 18081 #這個端口的意思是自己這個服務開個端口和 sentinel 控制臺交互
   dashboard: 192.168.1.14:8858 # sentinel 控制臺的端口
server:
 port: 8081
feign:
 sentinel:
 enabled: true
management:
 endpoints:
 web:
  exposure:
  include: "*"

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

以上就是Spring Cloud Alibaba 使用 Feign+Sentinel 完成熔斷的示例的詳細內容,更多關于Spring Cloud Alibaba 使用Feign+Sentinel 完成熔斷的資料請關注服務器之家其它相關文章!

原文鏈接:https://www.skypyb.com/2019/09/jishu/1075/

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 污视频在线免费播放 | 久久思思爱 | 久久99久久99精品 | 国产精品麻豆91 | 色综合久久久久久 | 得得啪在线视频 | 免费91在线| 一级黄色淫片 | 曰韩av在线 | 黄色aaa视频| 性猛aa久久久 | 成人在线视频一区 | 孕妇体内谢精满日本电影 | 意大利av在线 | 最新中文在线视频 | 亚洲第一页中文字幕 | 国产免费人做人爱午夜视频 | 欧美一级黄 | 丁香天堂网 | 日本特级a一片免费观看 | 久久久久久久一区二区三区 | 毛片视频网站在线观看 | 成人情欲视频在线看免费 | 久久出精品 | av在线一区二区三区四区 | 精品一区二区三区欧美 | 久草在线资源福利站 | 日本久久久网站 | 日本一区二区三区四区高清视频 | 色的综合 | 在线看一区二区三区 | 九九热九九 | 四季久久免费一区二区三区四区 | 91在线看黄 | 1级黄色毛片 | 成年人在线视频观看 | 毛片三区 | 久久久久久久久淑女av国产精品 | 中文字幕在线视频日本 | 久久精品a一级国产免视看成人 | 污片视频在线观看 |