启动控制台命令 (注意先把文件拖入linux下的opt)
#1 打开编辑文件 vim dashboard.sh #复制下面的内容 账号cm 密码ok(注意 - 有可能要自己手动打) java -Dserver.port=8858 -Dsentinel.dashboard.auth.username=cm -Dsentinel.dashboard.auth.password=ok -jar /opt/sentinel-dashboard-1.8.1.jar #2 授权文件名 chmod 777 dashboard.sh ls ./dashboard.sh cat dashboard.sh ls /opt/ #3 浏览器启动(地址输入自己的) http://192.168.64.200:8858/#/login #4 账号cm 密码ok
显示上面内容成功进入!!!
Spring Cloud Alibaba整合Sentinel 第一步 配置pomSentinel 会在客户端首次调用的时候进行初始化,开始向控制台发送心跳包,所以要确保客户端有访问量;
第二步配置controllercom.alibaba.cloud spring-cloud-starter-alibaba-sentinel
@RestController
@RequestMapping("/nssent")
public class InitCtrl {
@RequestMapping("/init")
public String sayHello(){
return "Hello World";
}
}
第三步配置yml
第四步启动浏览器输入server: port: 12005 spring: application: name: nessentinelctl cloud: sentinel: transport: dashboard: 192.168.64.200:8858
第五步实现流控http://localhost:12005/nssent/init #刷新几次 在进入下面地址 http://192.168.64.200:8858/#/dashboard
安装jmeter(压力测试)#1 在controller添加代码 自定义流控提示
@RestController @RequestMapping("/nssent") public class InitCtrl { @RequestMapping("/init") @SentinelResource(value = "it",blockHandler = "ctlHandler") public String sayHello(){ return "Hello World"; } public String ctlHandler(BlockException ex){ return "你被流控了o(╥﹏╥)o"; } }
java代码实现代替jemeter测试(纯代码版) #第一步导入pom依赖点击jemter.bat
#第二步编写代码org.apache.httpcomponents httpclient4.5.13
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import java.io.IOException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class App
{
public static void main( String[] args )
{
//创建一个线程池 默认10个线程
ExecutorService pool = Executors.newFixedThreadPool(10);
//for循环执行10次
for (int i = 0; i < 10; i++) {
pool.execute(()->{
try {
CloseableHttpClient chc= HttpClients.createDefault();
HttpGet get = new HttpGet("http://localhost:12005/nssent/init");
CloseableHttpResponse response= chc.execute(get);
System.out.println( Thread.currentThread().getName()+"--------->"+
EntityUtils.toString(response.getEntity()));
} catch (IOException e) {
throw new RuntimeException(e);
}
});
}
}
}
-------------项目 编写接口-------------(拓展知识 与sentinel无关)
get长度 255字节 2048字符 post 2g 理论没限制
{
"high":100,
"weight":60,
"scene-manager":"2022-SBZL-05966",
"indate":"2022-03-12",
"range":"2.6-60"
}
openfeign调用搭配sentinel nacos 服务降级 第一步导入pom依赖
第二步导入yml依赖org.springframework.cloud spring-cloud-starter-openfeigncom.alibaba.cloud spring-cloud-starter-alibaba-nacos-discoverycom.alibaba.cloud spring-cloud-starter-alibaba-sentinel
第三步配置Applicationserver: port: 12105 spring: application: name: nssentinelctl2 #调用sentinel控制台 开启限流 cloud: sentinel: transport: dashboard: 192.168.64.200:8858 #nacos服务注册中心 配置账户密码 nacos: discovery: server-addr: 192.168.64.200:8848 username: nacos password: nacos namespace: public #feign开启限流驱动 feign: sentinel: enabled: true
#启动 @EnableFeignClients 远程调用
@SpringBootApplication
@EnableFeignClients
public class NsssApplication {
public static void main(String[] args) {
SpringApplication.run(NsssApplication.class,args);
}
}
第四步配置service接口
#如果触发降级规则 开启回滚 否则执行远程调用
@FeignClient(value = "stockserv",path = "/stock-server",fallback = StockFeignServiceimpl.class)
public interface StockFeifgnService {
@RequestMapping("/reduced")
public String reduced();
}
#配置service接口实现类
#降级后的执行规则----保证用户访问体验
@Component
public class StockFeignServiceimpl implements StockFeifgnService {
@Override
public String reduced(){
return "我是一个降级的服务o(╥﹏╥)oo(╥﹏╥)o";
}
}
第五步配置controller层
@RestController
@RequestMapping("/nssent")
public class InitCtrl {
//注入seriver的接口
@Resource
private StockFeifgnService sfs;
@RequestMapping("init")
@SentinelResource(value = "it" ,blockHandler="ctlHandler")
public String sayHello(){
return "hello,world,"+sfs.reduced();
}
public String ctlHandler(BlockException ex){
return "你被限流了";
}
}
第六步nacos开启服务
#访问地址 192.168.64.200:8848/nacos
第七步sentinel控制台编写降级规则
#输入地址访问!! http://localhost:12105/nssent/init 可以看见触发了降级规则



