JeecgBoot在gateway中的sentinel持久化

1. 环境搭建

Jeecgboot 3.0

2. 坐标引入

在jeecg-cloud-gateway的pom文件中引入依赖

 <dependency>
    <groupId>com.alibaba.csp</groupId>
    <artifactId>sentinel-datasource-nacos</artifactId>
 </dependency>

3.配置文件编写

在jeecg-cloud-gateway中编辑配置文件,编辑后的配置文件为

spring:
  application:
    name: jeecg-gateway
  cloud:
    #Sentinel配置
    sentinel:
      web-context-unify: false
      transport:
        dashboard: localhost:8087
      # 懒加载Sentinel Dashboard菜单
      eager: false
      datasource:
        flow-rules:
          nacos:
            server-addr: 127.0.0.1:8848
            dataId: test111
            username: naocs
            password: nacos
            groupId: DEFAULT_GROUP
            data-type: json
            rule-type: flow

4.配置类编写

在jeecg-cloud-gateway中增加配置类

package org.jeecg.config;


import com.alibaba.cloud.sentinel.SentinelProperties;
import com.alibaba.cloud.sentinel.datasource.config.NacosDataSourceProperties;
import com.alibaba.csp.sentinel.adapter.gateway.common.rule.GatewayFlowRule;
import com.alibaba.csp.sentinel.adapter.gateway.common.rule.GatewayRuleManager;
import com.alibaba.csp.sentinel.datasource.ReadableDataSource;
import com.alibaba.csp.sentinel.datasource.nacos.NacosDataSource;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;

import java.util.Set;

/**
 * @Description: 限流规则持久化
 * @author: cmwl
 * @Date: 2021/12/26 12:01
 */
@Configuration
@Order(2)
public class SentinelPersistenceConfig {

    @Autowired
    private SentinelProperties sentinelProperties;

    @Bean
    public SentinelPersistenceConfig init() throws Exception {
        loadGWFlowRule();
        return new SentinelPersistenceConfig();
    }

    private void loadGWFlowRule() {
        sentinelProperties.getDatasource().entrySet().stream().filter(map -> {
            return map.getValue().getNacos() != null;
        }).forEach(map -> {
            NacosDataSourceProperties nacos = map.getValue().getNacos();
            ReadableDataSource<String, Set<GatewayFlowRule>> gwFlowRuleDataSource = new NacosDataSource<>(
                    nacos.getServerAddr(), nacos.getGroupId(), nacos.getDataId(),
                    source -> JSON.parseObject(source, new TypeReference<Set<GatewayFlowRule>>() {
                    }));
            GatewayRuleManager.register2Property(gwFlowRuleDataSource.getProperty());
        });
    }
}

5. nacos增加配置文件

上图中的配置内容如下:

[
    {
        "resource": "jeecg-system",
        "limitApp": "default",
        "grade": 1,
        "count": 2,
        "strategy": 0,
        "controlBehavior": 0,
        "clusterMode": false
    }
]

 

6. 测试接口编写

在jeecg-boot-module-system微服务中,增加controller接口

package org.jeecg.modules.sentinel;

import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Slf4j
@RestController
@RequestMapping("/sys")
public class TestController {
    @GetMapping(value = "/testSentinel")
    public Result<?> testSentinel(HttpServletRequest request, HttpServletResponse response) {
        Result<?> result = new Result<>();
        return Result.OK("成功获取sentinel接口today");
    }
}

记得在shiroconfig中增加该路径的过滤

        filterChainDefinitionMap.put("/sys/testSentinel", "anon"); //sentinel测试

7.测试结果

依次启动JeecgNacosApplication, JeecgGateWayApplication, JeecgSentinelDashboardApplication和JeecgSystemCloudApplication, 然后在浏览器输入地址http://localhost:9999/sys/testSentinel,在sentinel面板中可以看到流控规则

快速点击浏览器的刷新按钮,连续访问该controller接口,可以看到如下提示

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值