【Sentinel、Nacos】Sentinel同步GateWay网关配置到Nacos中

一、GateWay网关规则持久化

1、介绍:Sentinel官方文档 | 动态规则扩展 | 推荐链接

2、下载源码Sentinel-1.8.0.zip,并解压

3、sentinel-dashboard项目注释pom.xml依赖(sentinel-datasource-nacos)作用域配置(<scope>test</scope>)
在这里插入图片描述
4、复制NacosConfig和NacosConfigUtil文件
在这里插入图片描述
5、修改NacosConfig的配置在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

 	/**
     * 网关API
     *
     * @return
     * @throws Exception
     */
    @Bean
    public Converter<List<ApiDefinitionEntity>, String> apiDefinitionEntityEncoder() {
   
   
        return JSON::toJSONString;
    }

    @Bean
    public Converter<String, List<ApiDefinitionEntity>> apiDefinitionEntityDecoder() {
   
   
        return s -> JSON.parseArray(s, ApiDefinitionEntity.class);
    }

    /**
     * 网关flowRule
     *
     * @return
     * @throws Exception
     */
    @Bean
    public Converter<List<GatewayFlowRuleEntity>, String> gatewayFlowRuleEntityEncoder() {
   
   
        return JSON::toJSONString;
    }

    @Bean
    public Converter<String, List<GatewayFlowRuleEntity>> gatewayFlowRuleEntityDecoder() {
   
   
        return s -> JSON.parseArray(s, GatewayFlowRuleEntity.class);
    }
    
	@Bean
    public ConfigService nacosConfigService() throws Exception {
   
   
        Properties properties = new Properties();
        // nacos 地址
        properties.put(PropertyKeyConst.SERVER_ADDR, "101.101.101.101:7001");
        // 命令空间
        properties.put(PropertyKeyConst.NAMESPACE, "dev");
        // nacos 账号
        properties.put(PropertyKeyConst.USERNAME, "nacos");
        // nacos 账号密码
        properties.put(PropertyKeyConst.PASSWORD, "nacos");
        return ConfigFactory.createConfigService(properties);
    }

6、NacosConfigUtil文件中添加配置
在这里插入图片描述

    public static final String GETWAY_FLOW_DATA_ID_POSTFIX = "-sentinel-gateway-flow-rules";
    public static final String GETWAY_API_DATA_ID_POSTFIX = "-sentinel-gateway-api-rules";

7、创建GateWayFlowRulesNacosProvider、GateWayFlowRulesNacosPunlisher、GetWayApiNacosProvider、GetWayApiNacosPublisher文件。
在这里插入图片描述

package com
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值