Search in sources :

Example 1 with TpsControlRuleChangeEvent

use of com.alibaba.nacos.core.remote.control.TpsControlRuleChangeEvent in project nacos by alibaba.

the class InternalConfigChangeNotifier method onEvent.

@Override
public void onEvent(LocalDataChangeEvent event) {
    String groupKey = event.groupKey;
    String dataId = GroupKey.parseKey(groupKey)[0];
    String group = GroupKey.parseKey(groupKey)[1];
    if (DATA_ID_CONNECTION_LIMIT_RULE.equals(dataId) && NACOS_GROUP.equals(group)) {
        try {
            String content = loadLocalConfigLikeClient(dataId, group);
            NotifyCenter.publishEvent(new ConnectionLimitRuleChangeEvent(content));
        } catch (NacosException e) {
            Loggers.REMOTE.error("connection limit rule load fail.", e);
        }
    }
    if (dataId.startsWith(DATA_ID_TPS_CONTROL_RULE) && NACOS_GROUP.equals(group)) {
        try {
            String pointName = dataId.replaceFirst(DATA_ID_TPS_CONTROL_RULE, "");
            String content = loadLocalConfigLikeClient(dataId, group);
            NotifyCenter.publishEvent(new TpsControlRuleChangeEvent(pointName, content));
        } catch (NacosException e) {
            Loggers.REMOTE.error("connection limit rule load fail.", e);
        }
    }
}
Also used : TpsControlRuleChangeEvent(com.alibaba.nacos.core.remote.control.TpsControlRuleChangeEvent) ConnectionLimitRuleChangeEvent(com.alibaba.nacos.core.remote.event.ConnectionLimitRuleChangeEvent) NacosException(com.alibaba.nacos.api.exception.NacosException)

Example 2 with TpsControlRuleChangeEvent

use of com.alibaba.nacos.core.remote.control.TpsControlRuleChangeEvent in project nacos by alibaba.

the class InternalConfigChangeNotifierTest method testOnEvent.

@Test
public void testOnEvent() {
    final String groupKey = GroupKey2.getKey("nacos.internal.tps.control_rule_1", "nacos", "tenant");
    final String limitGroupKey = GroupKey2.getKey("nacos.internal.tps.nacos.internal.connection.limit.rule", "nacos", "tenant");
    NotifyCenter.registerSubscriber(new Subscriber() {

        @Override
        public void onEvent(Event event) {
            ConnectionLimitRuleChangeEvent connectionLimitRuleChangeEvent = (ConnectionLimitRuleChangeEvent) event;
            Assert.assertEquals("content", connectionLimitRuleChangeEvent.getLimitRule());
        }

        @Override
        public Class<? extends Event> subscribeType() {
            return ConnectionLimitRuleChangeEvent.class;
        }
    });
    NotifyCenter.registerSubscriber(new Subscriber() {

        @Override
        public void onEvent(Event event) {
            TpsControlRuleChangeEvent tpsControlRuleChangeEvent = (TpsControlRuleChangeEvent) event;
            Assert.assertEquals("content", tpsControlRuleChangeEvent.getRuleContent());
        }

        @Override
        public Class<? extends Event> subscribeType() {
            return TpsControlRuleChangeEvent.class;
        }
    });
    internalConfigChangeNotifier.onEvent(new LocalDataChangeEvent(groupKey));
    internalConfigChangeNotifier.onEvent(new LocalDataChangeEvent(limitGroupKey));
    try {
        Thread.sleep(500);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
}
Also used : TpsControlRuleChangeEvent(com.alibaba.nacos.core.remote.control.TpsControlRuleChangeEvent) Subscriber(com.alibaba.nacos.common.notify.listener.Subscriber) ConnectionLimitRuleChangeEvent(com.alibaba.nacos.core.remote.event.ConnectionLimitRuleChangeEvent) TpsControlRuleChangeEvent(com.alibaba.nacos.core.remote.control.TpsControlRuleChangeEvent) ConnectionLimitRuleChangeEvent(com.alibaba.nacos.core.remote.event.ConnectionLimitRuleChangeEvent) LocalDataChangeEvent(com.alibaba.nacos.config.server.model.event.LocalDataChangeEvent) Event(com.alibaba.nacos.common.notify.Event) LocalDataChangeEvent(com.alibaba.nacos.config.server.model.event.LocalDataChangeEvent) Test(org.junit.Test)

Aggregations

TpsControlRuleChangeEvent (com.alibaba.nacos.core.remote.control.TpsControlRuleChangeEvent)2 ConnectionLimitRuleChangeEvent (com.alibaba.nacos.core.remote.event.ConnectionLimitRuleChangeEvent)2 NacosException (com.alibaba.nacos.api.exception.NacosException)1 Event (com.alibaba.nacos.common.notify.Event)1 Subscriber (com.alibaba.nacos.common.notify.listener.Subscriber)1 LocalDataChangeEvent (com.alibaba.nacos.config.server.model.event.LocalDataChangeEvent)1 Test (org.junit.Test)1