微服务组件Sentinel控制台调用 nacos openFeign降级组合

02微服务组件Sentinel (控制台版)

第一步进入linux配置

微服务组件Sentinel控制台调用 nacos openFeign降级组合_第1张图片

启动控制台命令 (注意先把文件拖入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

微服务组件Sentinel控制台调用 nacos openFeign降级组合_第2张图片

显示上面内容成功进入!!!

Sentinel 会在客户端首次调用的时候进行初始化,开始向控制台发送心跳包,所以要确保客户端有访问量;

Spring Cloud Alibaba整合Sentinel

第一步 配置pom

        
            com.alibaba.cloud
            spring-cloud-starter-alibaba-sentinel
        

第二步配置controller

@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

第五步实现流控

#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";
    }
}

微服务组件Sentinel控制台调用 nacos openFeign降级组合_第3张图片

微服务组件Sentinel控制台调用 nacos openFeign降级组合_第4张图片

安装jmeter(压力测试)

点击jemter.bat

微服务组件Sentinel控制台调用 nacos openFeign降级组合_第5张图片

微服务组件Sentinel控制台调用 nacos openFeign降级组合_第6张图片

java代码实现代替jemeter测试(纯代码版)

#第一步导入pom依赖


    org.apache.httpcomponents
    httpclient
    4.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;

/**
 * Hello world!
 *
 */
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控制台调用 nacos openFeign降级组合_第7张图片

-------------项目 编写接口-------------(拓展知识 与sentinel无关)

微服务组件Sentinel控制台调用 nacos openFeign降级组合_第8张图片

微服务组件Sentinel控制台调用 nacos openFeign降级组合_第9张图片

 微服务组件Sentinel控制台调用 nacos openFeign降级组合_第10张图片

get长度 255字节  2048字符
post 2g 理论没限制

微服务组件Sentinel控制台调用 nacos openFeign降级组合_第11张图片

{
	"high":100,
	"weight":60,
	"scene-manager":"2022-SBZL-05966",
	"indate":"2022-03-12",
	"range":"2.6-60"
	
}

 

openfeign调用搭配sentinel nacos 服务降级

第一步导入pom依赖

        
            org.springframework.cloud
            spring-cloud-starter-openfeign
        

        
            com.alibaba.cloud
            spring-cloud-starter-alibaba-nacos-discovery
        

        
            com.alibaba.cloud
            spring-cloud-starter-alibaba-sentinel
        

第二步导入yml依赖

server:
  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

第三步配置Application

#启动 @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控制台调用 nacos openFeign降级组合_第12张图片 

 

第七步sentinel控制台编写降级规则

微服务组件Sentinel控制台调用 nacos openFeign降级组合_第13张图片  

#输入地址访问!!
http://localhost:12105/nssent/init
可以看见触发了降级规则

微服务组件Sentinel控制台调用 nacos openFeign降级组合_第14张图片

 

你可能感兴趣的:(Java,大数据,微服务,sentinel)