不知不觉已经三年了。继续加油。
微服务限流https://github.com/alibaba/Sentinel/releases sentinel下载地址,我用的1.8.4
启动方式
java -Dserver.port=8888 -Dcsp.sentinel.dashboard.server=localhost:8888 -Dproject.name=sentinel-dashboard -jar sentinel-dashboard-1.8.4.jar
启动之后访问控制台:http://localhost:8888,账号密码都是sentinel
微服务中添加依赖
com.alibaba.cloud spring-cloud-starter-alibaba-sentinel
配置项
spring:
cloud:
sentinel:
transport:
port: 9999 #指定和Sentinel控制台交互的端口,任意指定一个未使用的端口即可
dashboard: 127.0.0.1:8888 #Sentinel控制台服务地址
先写一个测试接口
@GetMapping(value = "/test_sentinel")
public String testSentinel(){
log.info("测试Sentinel");
return "sentinel限流测试";
}
第一次进入到sentinel控制台的时候可能没有服务显示,我们需要访问一下接口,进行一下类似刷新。这就出来了
点击流控,按照这样配置,就表明每秒只能有一个请求
测试效果,这就实现了限流
feign:
sentinel:
enabled: true
我们以用户微服务为例,首先这是我们的feign接口
@FeignClient(value = "server-user")
public interface UserService {
@GetMapping(value = "/user/get/{uid}")
User getUser(@PathVariable("uid") Long uid);
}
创建容错服务
@Component
public class UserServiceFallBack implements UserService {
@Override
public User getUser(Long uid) {
User user = new User();
user.setId(-1L);
return user;
}
}
修改user服务
@FeignClient(value = "server-user",fallback = UserServiceFallBack.class)
public interface UserService {
@GetMapping(value = "/user/get/{uid}")
User getUser(@PathVariable("uid") Long uid);
}
这时候我们停掉用户服务,其他服务调用用户服务
这样入门就够了吧,着急写的,有点乱 凑合看吧



