Git Product home page Git Product logo

Comments (4)

sczyh30 avatar sczyh30 commented on August 26, 2024

Hi, this makes no sense. Since you are using datasource extension, you must know where your rules is stored.

Sentinel provides an SPI extension - InitFunc. You can implement your own InitFunc to init the datasource. For example:

package com.test.init;

public class DataSourceInitFunc implements InitFunc {

    @Override
    public void init() throws Exception {
        final String remoteAddress = "localhost";
        final String groupId = "Sentinel:Demo";
        final String dataId = "com.alibaba.csp.sentinel.demo.flow.rule";

        DataSource<String, List<FlowRule>> flowRuleDataSource = new NacosDataSource<>(remoteAddress, groupId, dataId,
            source -> JSON.parseObject(source, new TypeReference<List<FlowRule>>() {}));
        FlowRuleManager.register2Property(flowRuleDataSource.getProperty());
    }
}

Then add the class to com.alibaba.csp.sentinel.init.InitFunc file in META-INF/services directory of the resource directory:

com.test.init.DataSourceInitFunc

Then when you trigger a resource access, Sentinel will scan all InitFunc and initialize the datasource automatically. You don't have to manually call it.

from sentinel.

wanhuhou avatar wanhuhou commented on August 26, 2024

真不知道怎么想的

from sentinel.

wanhuhou avatar wanhuhou commented on August 26, 2024

为什么不借鉴下dubbo的方式呢,让使用者更方便呢

from sentinel.

Crazy10552 avatar Crazy10552 commented on August 26, 2024

Hi, this makes no sense. Since you are using datasource extension, you must know where your rules is stored.

Sentinel provides an SPI extension - InitFunc. You can implement your own InitFunc to init the datasource. For example:

package com.test.init;

public class DataSourceInitFunc implements InitFunc {

    @Override
    public void init() throws Exception {
        final String remoteAddress = "localhost";
        final String groupId = "Sentinel:Demo";
        final String dataId = "com.alibaba.csp.sentinel.demo.flow.rule";

        DataSource<String, List<FlowRule>> flowRuleDataSource = new NacosDataSource<>(remoteAddress, groupId, dataId,
            source -> JSON.parseObject(source, new TypeReference<List<FlowRule>>() {}));
        FlowRuleManager.register2Property(flowRuleDataSource.getProperty());
    }
}

Then add the class to com.alibaba.csp.sentinel.init.InitFunc file in META-INF/services directory of the resource directory:

com.test.init.DataSourceInitFunc

Then when you trigger a resource access, Sentinel will scan all InitFunc and initialize the datasource automatically. You don't have to manually call it.

这种自定义InitFunc里面需要使用Spring的@autowire@value 或者Jdk自带@resource 来获取属性会失效吧,我自己写了例子都不能成功,目前所有的demo里都是代码硬编码写死这部分动态参数

from sentinel.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.