什么是Sentinel?
请查看文章:SpringCloud Alibaba微服务实战(一) - 基础环境搭建
构建服务消费者cloud-sentinel进行服务调用
服务创建请查看文章:SpringCloud Alibaba微服务实战(二) - Nacos服务注册与restTemplate消费
1.在父项目中创建子module项目名字为cloud-sentinel,在pom中引入nacos服务注册依赖
<!--Spring web-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--SpringCloud Alibaba nacos-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>
完整服务消费者cloud-sentinel的pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<artifactId>cloud-alibaba</artifactId>
<groupId>com.zsy.springcloud</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>cloud-sentinel</artifactId>
<dependencies>
<!--Spring web-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--SpringCloud Alibaba nacos-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<repositories>
<!--指定阿里云镜像库下载依赖-->
<repository>
<id>central</id>
<name>aliyunmaven</name>
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
</repository>
</repositories>
</project>
2.配置application.yml或者properties文件
server:
port: 8085
spring:
#服务应用名字
application:
name: cloud-sentinel
cloud:
# 指定nacos控制台地址,配置注册ip:端口,注意即使是80端口也不可能省略
nacos:
discovery:
server-addr: 127.0.0.1:8848
sentinel:
transport:
dashboard: 127.0.0.1:8080 # sentinel服务端地址
eager: true # 取消延迟加载
3.FeignClient接口定义AccountService
package com.zsy.springcloud.service;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import java.util.Map;
@FeignClient("cloud-account")
public interface AccountService {
//获取客户信息
@RequestMapping(value = "/getAccount", method = RequestMethod.GET)
public Map<String,Object> getAccount();
}
4.创建消费者AccountConsumerController
package com.zsy.springcloud.controller;
import com.zsy.springcloud.service.AccountService;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;
@Log4j2
@RestController
public class AccountConsumerController {
@Resource
private AccountService accountService;
@RequestMapping(value = "/getAccount", method = RequestMethod.GET)
public Map<String,Object> getAccount() {
Map<String,Object> account = new HashMap<>();
log.info("---------消费者开始------------");
account = accountService.getAccount();
log.info("---------消费者结束--------account{}----", account);
return account;
}
}
5.配置消费者启动服务的启动类
package com.zsy.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
@SpringBootApplication
@EnableDiscoveryClient //开启服务注册
@EnableFeignClients//开启feign客户端
public class CloudSentinelApplication {
public static void main(String[] args) {
SpringApplication.run(CloudSentinelApplication.class, args);
}
}
6.启动cloud-sentinel服务项目,启动成功,如下图
7. 验证服务,浏览器访问http://localhost:8085/getAccount,返回如下图
查看消费者cloud-sentinel控制台,如下图
查看服务者cloud-account控制台,如下图
限流
限流配置
访问:http://127.0.0.1:8080/,登录sentinel
理想是丰满的,现实是骨感的。为了方便测试,我们就将cloud-sentinel中的getAccount接口的QPS单机阈值设置成1,如果每秒QPS超过1,直接丢弃。
打开浏览器,快速刷新浏览器,当每秒请求书超过1时会看到如下错误:
不要慌,这说明我们的目的达到了,限流成功!
自定义异常
我们可以通过@SentinelResource中添加blockHandler参数,给其添加自定义异常方法。如:
package com.zsy.springcloud.controller;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.zsy.springcloud.service.AccountService;
import lombok.extern.log4j.Log4j2;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;
@Log4j2
@RestController
public class AccountConsumerController {
@Resource
private AccountService accountService;
@RequestMapping(value = "/getAccount", method = RequestMethod.GET)
@SentinelResource(value = "getAccount",blockHandler = "handleException")
public Map<String,Object> getAccount() {
Map<String,Object> account = new HashMap<>();
log.info("---------消费者开始------------");
account = accountService.getAccount();
log.info("---------消费者结束--------account{}----", account);
return account;
}
/**
* 自定义异常策略
* 返回值和参数要跟目标函数一样,参数可以追加BlockException
*/
public Map<String,Object> handleException(BlockException exception){
Map<String,Object> account = new HashMap<>();
log.info("flow exception{}",exception.getClass().getCanonicalName());
account.put("msg","别点了,达到阀值了,稍后再试");
return account;
}
}
注意,自定义的异常方法的参数和返回值要跟目标方法一样,参数可以追加BlockException
打开浏览器,快速刷新浏览器,当每秒请求书超过1时会看到如下错误:
@SentinelResource 属性介绍
熔断
什么是熔断?
消费者order-service需要先调用product-service获取具体的product,然后再处理其他的业务逻辑。但是这个product-service接口不是很稳定,经常抛出异常,或者是响应缓慢,宕机。导致order-service的响应变慢;如果置之不理,order-service可能会被product-service拖垮。这时候为了保护order-service,我们需要对product-service接口进行熔断。
总结:熔断是通过限制自己对外部系统的调用, 起到节约响应时间、维护链路稳定的作用
熔断
Sentinel中的熔断降级有三个降级策略:
RT(平均响应时间):
当资源的平均响应时间超过阈值之后,资源进入准降级状态。接下来如果持续进入 5 个请求,它们的 RT 都持续超过这个阈值,那么在接下的时间窗口之内,对这个方法的调用都会自动抛出 DegradeException 异常。在下一个时间窗口到来时, 会接着再放入5个请求, 再重复上面的判断.
异常比例
当资源的每秒异常总数占通过量的比值超过阈值之后,资源进入降级状态,即在接下的时间窗口之内,对这个方法的调用都会自动地抛出DegradeException异常。异常比率的阈值范围是 [0.0, 1.0],代表 0% - 100%。
异常数
当资源近 1 分钟的异常数目超过阈值之后会进行熔断。
未进行熔断配置,服务异常验证
1.配置服务抛出异常
2.验证,访问http://localhost:8085/getAccount,页面展示如下图:
控制台如下图:
在日常中,我们肯定不希望发生这样的事故。我们可以通过熔断实现服务降级来处理这种情况
熔断配置
1.自定义异常,创建异常本地实现类,实现FeignClient接口
package com.zsy.springcloud.fallback;
import com.zsy.springcloud.service.AccountService;
import lombok.extern.log4j.Log4j2;
import org.springframework.stereotype.Service;
import java.util.HashMap;
import java.util.Map;
@Log4j2
@Service
public class AccountServiceBack implements AccountService {
@Override
public Map<String, Object> getAccount() {
Map<String,Object> account = new HashMap<>();
account.put("msg","服务熔断了,本地处理");
return account;
}
}
.2.修改FeignClient接口定义AccountService
package com.zsy.springcloud.service;
import com.zsy.springcloud.fallback.AccountServiceBack;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import java.util.Map;
@FeignClient(value = "cloud-account",fallback = AccountServiceBack.class)
public interface AccountService {
//获取客户信息
@RequestMapping(value = "/getAccount", method = RequestMethod.GET)
public Map<String,Object> getAccount();
}
3.配置application.yml或者properties文件
server:
port: 8085
spring:
#服务应用名字
application:
name: cloud-sentinel
cloud:
# 指定nacos控制台地址,配置注册ip:端口,注意即使是80端口也不可能省略
nacos:
discovery:
server-addr: 127.0.0.1:8848
sentinel:
transport:
dashboard: 127.0.0.1:8080 # sentinel服务端地址
eager: true # 取消延迟加载
feign:
hystrix:
enabled: true #打开feign对hustrix的支持,FeignClient 中的 fallback不起任何作用
因为Fallback是通过Hystrix实现的, 所以需要开启Hystrix,spring boot application.properties文件配置feign.hystrix.enabled=true,这样就开启了Fallback
4.配置getAccount熔断
5.验证,访问http://localhost:8085/getAccount,页面展示如下图:
控制台如下图:
熔断配置完成,我们已经给我们的微服务加上了限流熔断保护,再也不用担心异常流量的冲击,下游系统不稳定导致自身服务不可用了。本片文章只是对Sentinel进行了一个入门级的交流,Sentinel更高级的等后面有时间了另行分享。
1.Sentinel的配置默认是放在内存中的,每当应用重启或者sentinel重启都会丢失数据,该如何处理?
。。。。。。。。。.。。。。。。。。。。。。。。。
码云地址:https://gitee.com/zlzhaoe/cloud-alibaba