springcloudalibaba集成sentinel从nacos拉取规则配置文件失败

问题如题,提前做了sentinel控制台的推模式集成nacos,客户端也配置了nacos动态数据源,但是看日志规则没拉下来,日志中发现是去接口拉规则的时候没有tenant参数导致找不到配置文件(因为我的控制台把规则推送到了指定命名空间下,不是公共命名空间)。

2022-02-28 18:36:16.893 DEBUG 12600 --- [           main] s.n.www.protocol.http.HttpURLConnection  : sun.net.www.MessageHeader@79627d2712 pairs: {GET /nacos/v1/cs/configs?dataId=spring-cloud-sentinel-sample-param-rules&group=SENTINEL_GROUP HTTP/1.1: null}{exConfigInfo: true}{RequestId: 212e61e4-5fd4-4a5f-8252-6a342eda900d}{Client-Version: 1.1.4}{Content-Type: application/x-www-form-urlencoded;charset=UTF-8}{Client-AppName: spring-cloud-sentinel-sample}{Client-RequestTS: 1646044576893}{Client-RequestToken: 926718a15304aa3b88ef797e2e2c2e43}{User-Agent: Java/17.0.1}{Host: 192.168.20.133:8848}{Accept: text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2}{Connection: keep-alive}
2022-02-28 18:36:16.897 DEBUG 12600 --- [           main] s.n.www.protocol.http.HttpURLConnection  : sun.net.www.MessageHeader@6371cf2f6 pairs: {null: HTTP/1.1 404}{Content-Type: application/json;charset=UTF-8}{Content-Length: 22}{Date: Mon, 28 Feb 2022 10:36:16 GMT}{Keep-Alive: timeout=60}{Connection: keep-alive}
2022-02-28 18:36:16.898  WARN 12600 --- [           main] c.a.c.s.d.converter.SentinelConverter    : converter can not convert rules because source is empty

跟踪代码发现,sringcloudalibaba是通过注入NacosDataSourceFactoryBean类的实例来拉规则的,这个类里面的属性就对应的是nacos动态数据源的配置属性,里面有一个属性namespace,把这个配置值加上,数据源就可以正确拉取配置了,例子如下:

spring:
  cloud: # cloud
    sentinel:
      transport:
        port: 8719 # Sentinel 控制台添加了一个限流规则,会把规则数据 push 给这个端口的 Http Server 接收
        dashboard: 192.168.20.134:8100
      datasource:
        flow:
          nacos:
            server-addr: 192.168.20.133:8848
            dataId: ${spring.application.name}-flow-rules
            groupId: SENTINEL_GROUP
            namespace: xxx   #就是它
            data-type: json
            rule-type: flow

NacosDataSourceFactoryBean类如下:



package com.alibaba.cloud.sentinel.datasource.factorybean;

import java.util.Properties;

import com.alibaba.csp.sentinel.datasource.Converter;
import com.alibaba.csp.sentinel.datasource.nacos.NacosDataSource;
import com.alibaba.nacos.api.PropertyKeyConst;

import org.springframework.beans.factory.FactoryBean;
import org.springframework.util.StringUtils;

/**
 * A {@link FactoryBean} for creating {@link NacosDataSource} instance.
 *
 * @author <a href="mailto:fangjian0423@gmail.com">Jim</a>
 * @see NacosDataSource
 */
public class NacosDataSourceFactoryBean implements FactoryBean<NacosDataSource> {

	private String serverAddr;

	private String groupId;

	private String dataId;

	private Converter converter;

	private String endpoint;

	private String namespace;

	private String accessKey;

	private String secretKey;

	@Override
	public NacosDataSource getObject() throws Exception {
		Properties properties = new Properties();
		if (!StringUtils.isEmpty(this.serverAddr)) {
			properties.setProperty(PropertyKeyConst.SERVER_ADDR, this.serverAddr);
		}
		else {
			properties.setProperty(PropertyKeyConst.ACCESS_KEY, this.accessKey);
			properties.setProperty(PropertyKeyConst.SECRET_KEY, this.secretKey);
			properties.setProperty(PropertyKeyConst.ENDPOINT, this.endpoint);
		}
		if (!StringUtils.isEmpty(this.namespace)) {
			properties.setProperty(PropertyKeyConst.NAMESPACE, this.namespace);
		}
		return new NacosDataSource(properties, groupId, dataId, converter);
	}

	@Override
	public Class<?> getObjectType() {
		return NacosDataSource.class;
	}

	public String getServerAddr() {
		return serverAddr;
	}

	public void setServerAddr(String serverAddr) {
		this.serverAddr = serverAddr;
	}

	public String getGroupId() {
		return groupId;
	}

	public void setGroupId(String groupId) {
		this.groupId = groupId;
	}

	public String getDataId() {
		return dataId;
	}

	public void setDataId(String dataId) {
		this.dataId = dataId;
	}

	public Converter getConverter() {
		return converter;
	}

	public void setConverter(Converter converter) {
		this.converter = converter;
	}

	public String getEndpoint() {
		return endpoint;
	}

	public void setEndpoint(String endpoint) {
		this.endpoint = endpoint;
	}

	public String getNamespace() {
		return namespace;
	}

	public void setNamespace(String namespace) {
		this.namespace = namespace;
	}

	public String getAccessKey() {
		return accessKey;
	}

	public void setAccessKey(String accessKey) {
		this.accessKey = accessKey;
	}

	public String getSecretKey() {
		return secretKey;
	}

	public void setSecretKey(String secretKey) {
		this.secretKey = secretKey;
	}
}

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值