1. 程式人生 > 其它 >sentinel改造nacos資料來源web客戶端

sentinel改造nacos資料來源web客戶端

技術標籤:sentinelsentinel

第一步:新增jar包

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

        <dependency>
            <groupId>com.alibaba.csp</groupId>
            <artifactId>sentinel-transport-simple-http</artifactId>
        </dependency>

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

        <dependency>
            <groupId>com.alibaba.csp</groupId>
            <artifactId>sentinel-annotation-aspectj</artifactId>
        </dependency>

        <dependency>
            <groupId>com.alibaba.csp</groupId>
            <artifactId>sentinel-spring-webmvc-adapter</artifactId>
        </dependency>

        <dependency>
            <groupId>com.alibaba.csp</groupId>
            <artifactId>sentinel-web-servlet</artifactId>
        </dependency>

第二步:配置yaml資訊

spring:
  application:
    name: application-name
  cloud:
    sentinel:
      transport:
        dashboard: localhost:18080
        port: 8719 #預設8719,如果被佔用會自動+1直到埠可用
      eager: true
      datasource:
        ds1:
          nacos:
            server-addr: ${nacos.address}
            namespace: ${nacos.namespace}
            username: nacos
            password: nacos
            group: sentinel
            dataId: ${spring.application.name}-flow-rules
            data-type: json
            rule-type: flow
        ds2:
          nacos:
            server-addr: ${nacos.address}
            namespace: ${nacos.namespace}
            username: nacos
            password: nacos
            group: sentinel
            dataId: ${spring.application.name}-degrade-rules
            data-type: json
            rule-type: degrade
        ds3:
          nacos:
            server-addr: ${nacos.address}
            namespace: ${nacos.namespace}
            username: nacos
            password: nacos
            group: sentinel
            dataId: ${spring.application.name}-param-flow-rules
            data-type: json
            rule-type: param-flow
        ds4:
          nacos:
            server-addr: ${nacos.address}
            namespace: ${nacos.namespace}
            username: nacos
            password: nacos
            group: sentinel
            dataId: ${spring.application.name}-authority-rules
            data-type: json
            rule-type: authority
        ds5:
          nacos:
            server-addr: ${nacos.address}
            namespace: ${nacos.namespace}
            username: nacos
            password: nacos
            group: sentinel
            dataId: ${spring.application.name}-system-rules
            data-type: json
            rule-type: system

第三步:新增獲取配置資訊工具類(因為初始化nacos規則時,無法直接獲取配置資訊,所以使用了工具類進行獲取配置資訊),如果有獲取配置資訊的方式,則不需要該工具類

import org.springframework.context.EmbeddedValueResolverAware;
import org.springframework.stereotype.Component;
import org.springframework.util.StringValueResolver;

/**
 * @author chengmeng
 * @description
 * @date 2021/2/2 11:15
 */
@Component
public class PropertiesUtil implements EmbeddedValueResolverAware {

    private static StringValueResolver resolver;

    @Override
    public void setEmbeddedValueResolver(StringValueResolver stringValueResolver) {
        PropertiesUtil.resolver = stringValueResolver;
    }

    public static String getPropertiesValue(String key){
        return resolver.resolveStringValue(key);
    }
}

第四步:初始化nacos資料來源規則資訊並且監聽資料來源變動

import com.alibaba.csp.sentinel.datasource.ReadableDataSource;
import com.alibaba.csp.sentinel.datasource.nacos.NacosDataSource;
import com.alibaba.csp.sentinel.init.InitFunc;
import com.alibaba.csp.sentinel.slots.block.authority.AuthorityRule;
import com.alibaba.csp.sentinel.slots.block.authority.AuthorityRuleManager;
import com.alibaba.csp.sentinel.slots.block.degrade.DegradeRule;
import com.alibaba.csp.sentinel.slots.block.degrade.DegradeRuleManager;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRule;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager;
import com.alibaba.csp.sentinel.slots.block.flow.param.ParamFlowRule;
import com.alibaba.csp.sentinel.slots.block.flow.param.ParamFlowRuleManager;
import com.alibaba.csp.sentinel.slots.system.SystemRule;
import com.alibaba.csp.sentinel.slots.system.SystemRuleManager;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.alibaba.nacos.api.PropertyKeyConst;

import java.util.List;
import java.util.Properties;

/**
 * @author chengmeng
 * @description 動態讀取並監聽nacos配置
 * @date 2021/2/1 15:52
 */
public class DataSourceInitFunc implements InitFunc {

    @Override
    public void init() throws Exception {
        String remoteAddress = PropertiesUtil.getPropertiesValue("${nacos.address}");
        String namespace = PropertiesUtil.getPropertiesValue("${nacos.namespace}");
        String flowGroupId = PropertiesUtil.getPropertiesValue("${spring.cloud.sentinel.datasource.ds1.nacos.group}");
        String flowDataId = PropertiesUtil.getPropertiesValue("${spring.cloud.sentinel.datasource.ds1.nacos.dataId}");
        String degradeGroupId = PropertiesUtil.getPropertiesValue("${spring.cloud.sentinel.datasource.ds2.nacos.group}");
        String degradeDataId = PropertiesUtil.getPropertiesValue("${spring.cloud.sentinel.datasource.ds2.nacos.dataId}");
        String paramFlowGroupId = PropertiesUtil.getPropertiesValue("${spring.cloud.sentinel.datasource.ds3.nacos.group}");
        String paramFlowDataId = PropertiesUtil.getPropertiesValue("${spring.cloud.sentinel.datasource.ds3.nacos.dataId}");
        String authGroupId = PropertiesUtil.getPropertiesValue("${spring.cloud.sentinel.datasource.ds4.nacos.group}");
        String authDataId = PropertiesUtil.getPropertiesValue("${spring.cloud.sentinel.datasource.ds4.nacos.dataId}");
        String sysGroupId = PropertiesUtil.getPropertiesValue("${spring.cloud.sentinel.datasource.ds5.nacos.group}");
        String sysDataId = PropertiesUtil.getPropertiesValue("${spring.cloud.sentinel.datasource.ds5.nacos.dataId}");
        Properties properties = new Properties();
        properties.put(PropertyKeyConst.SERVER_ADDR, remoteAddress);
        properties.put(PropertyKeyConst.NAMESPACE, namespace);

        //流控規則
        ReadableDataSource<String, List<FlowRule>> flowRuleDataSource = new NacosDataSource<>(properties, flowGroupId, flowDataId,
                source -> JSON.parseObject(source, new TypeReference<List<FlowRule>>() {}));
        FlowRuleManager.register2Property(flowRuleDataSource.getProperty());

        //降級規則
        ReadableDataSource<String, List<DegradeRule>> degradeRuleDataSource = new NacosDataSource<>(properties,
                degradeGroupId, degradeDataId,
                source -> JSON.parseObject(source, new TypeReference<List<DegradeRule>>() {
                }));
        DegradeRuleManager.register2Property(degradeRuleDataSource.getProperty());

        //熱點引數規則
        ReadableDataSource<String, List<ParamFlowRule>> paramFlowRuleDataSource = new NacosDataSource<>(properties,
                paramFlowGroupId, paramFlowDataId,
                source -> JSON.parseObject(source, new TypeReference<List<ParamFlowRule>>() {}));
        ParamFlowRuleManager.register2Property(paramFlowRuleDataSource.getProperty());

        //授權規則
        ReadableDataSource<String, List<AuthorityRule>> authDataSource = new NacosDataSource<>(properties,
                authGroupId, authDataId,
                source -> JSON.parseObject(source, new TypeReference<List<AuthorityRule>>() {}));
        AuthorityRuleManager.register2Property(authDataSource.getProperty());

        //系統規則
        ReadableDataSource<String, List<SystemRule>> sysDataSource = new NacosDataSource<>(properties,
                sysGroupId, sysDataId,
                source -> JSON.parseObject(source, new TypeReference<List<SystemRule>>() {}));
        SystemRuleManager.register2Property(sysDataSource.getProperty());
    }
}

第五步:啟用sentinel註解

import com.alibaba.csp.sentinel.annotation.aspectj.SentinelResourceAspect;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author chengmeng
 * @description 啟用sentinel註解
 * @date 2021/1/26 14:38
 */
@Configuration
public class SentinelAspectConfiguration {

    @Bean
    public SentinelResourceAspect sentinelResourceAspect() {
        return new SentinelResourceAspect();
    }
}

第六步:自定義設定訪問來源(主要用於系統規則中的黑白名單)此處取請求頭中的Host引數值作為訪問來源

import com.alibaba.csp.sentinel.adapter.spring.webmvc.callback.RequestOriginParser;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;

/**
 * @author chengmeng
 * @description 自定義訪問者來源
 * @date 2021/2/4 14:40
 */
@Component
public class RequestOriginParserDefinition implements RequestOriginParser {
    @Override
    public String parseOrigin(HttpServletRequest httpServletRequest) {
        String origin = httpServletRequest.getHeader("Host");
        return origin;
    }
}

第七步:介面中新增sentinel註解

    @GetMapping("/test")
    @SentinelResource("test")
    @ResponseBody
    public String test() {
        return "hello world";
    }

第八步:新增web客戶端JVM啟動引數:

-Dcsp.sentinel.dashboard.server=localhost:18080 -Dproject.name=application-name

第九步:啟動sentinel-dashbord服務端

第十步:啟動web客戶端