從零搭建springcloud微服務(七)----使用Sentinel代替Hystrix


一、使用Sentinel代替Hystrix

1.修改POM文件
(1)cloud-dependencies模塊加上spring-cloud-openfeign-dependencies依賴
注意:這里添加的目的是為了解決在spring-cloud-starter-openfeign2.2.0.RELEASE里接口方法名拼寫錯誤的問題。
具體可以參考文章https://www.cnblogs.com/cdfive2018/p/12537412.html
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-openfeign-dependencies</artifactId>
<version>${spring-cloud-alibaba.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
(2)cloud-consumer模塊加上spring-cloud-starter-alibaba-sentinel的依賴
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>
2.修改cloud-consumer模塊配置文件
使用feign.sentinel.enabled=true替換feign.hystrix.enabled=true
修改后結果
#feign.hystrix.enabled=true
feign.sentinel.enabled=true
3.修改cloud-consumer模塊代碼
修改CloudConsumerApplication類
在public RestTemplate restTemplate() {
return new RestTemplate();
}
方法上增加@SentinelRestTemplate
修改后的效果變成
@LoadBalanced
@Bean
@SentinelRestTemplate
public RestTemplate restTemplate() {
return new RestTemplate();
}

二、使用Sentinel
1.修改cloud-consumer模塊代碼
(1)新增SentinelGlobalDefaultUtil類,作為統一的block和Fallback處理,這里要注意接口返回值必須統一
package com.plkd.consumer.sentinel;

import com.alibaba.csp.sentinel.slots.block.BlockException;

public class SentinelGlobalDefaultUtil {
public static final int BLOCK_FLAG = 88888;
public static final String FALLBACK_DEFAULT_RESULT = "fallback";

public static int globalBlockHandler(BlockException ex) {
System.out.println("blocked by: " + ex.getClass().getSimpleName());
return BLOCK_FLAG;
}

public static String globalDefaultFallback(Throwable t) {
System.out.println("Fallback caught: " + t.getClass().getSimpleName());
return FALLBACK_DEFAULT_RESULT;
}
}

(2)修改OrderController類增加使用SentinelResource注解
修改后結果:
@RequestMapping(value = "/getUserByOrderId/{id}",method = RequestMethod.GET)
@SentinelResource(blockHandler = "globalBlockHandler", blockHandlerClass = SentinelGlobalDefaultUtil.class,
defaultFallback = "globalDefaultFallback",fallbackClass = {SentinelGlobalDefaultUtil.class})
public String getUserByOrderId(@PathVariable("id") int id){
return userRemoteClient.findUserByUserName("michael").toString();
}

2.修改user-open-api模塊代碼
(1)新增UserFeignClientFallbackFactory類
package com.plkd.usercenter.client.sentinel;

import com.plkd.usercenter.client.UserRemoteClient;
import com.plkd.usercenter.dto.UseDto;
import feign.hystrix.FallbackFactory;
import org.springframework.stereotype.Component;

/*
推薦使用這個方式,fallbackFactory屬性在fallback的基礎上可以拿到異常信息
*/
@Component
public class UserFeignClientFallbackFactory implements FallbackFactory<UserRemoteClient> {
@Override
public UserRemoteClient create(Throwable throwable) {
return new UserRemoteClient() {
@Override
public UseDto findUserByUserName(String userName) {
System.out.println("遠程調用被限流或降級了,throwable:"+throwable);
return null;
}
};
}
}
(2)修改UserRemoteClient類
將FeignClient注解修改成@FeignClient(name = "cloud-provider",fallbackFactory = UserFeignClientFallbackFactory.class)

三、部署Sentinel Dashboard
1.下載Sentinel控制台:https://github.com/alibaba/Sentinel/releases
執行命令啟動dashboard:
java -Dserver.port=8081 -jar sentinel-dashboard-1.7.1.jar
這樣默認是8081端口,在瀏覽器輸入:http://localhost:8081。默認賬號密碼:sentinel:sentinel,看到控制台界面為部署成功

2.cloud-consumer模塊引入配置:
# sentinel dashboard
spring.cloud.sentinel.transport.dashboard=localhost:8081

3.重新啟動cloud-consumer模塊
在瀏覽器訪問http://localhost:8080/echo/2018,以及http://localhost:8080/order/getUserByOrderId/1
這個時候就會在Sentinel控制台出現應用cloud-consumer

 

 

 4.設置流控規則

 

 

 5.連續訪問http://localhost:8080/order/getUserByOrderId/1  結果被限流

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM