springcloudalibaba集成sentinel从nacos拉取规则配置文件失败
Sentinel
alibaba/Sentinel: Sentinel 是阿里巴巴开源的一款面向分布式服务架构的流量控制、熔断降级组件,提供实时监控、限流、降级和系统保护功能,适用于微服务治理场景。
项目地址:https://gitcode.com/gh_mirrors/sentine/Sentinel
免费下载资源
·
问题如题,提前做了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;
}
}
GitHub 加速计划 / sentine / Sentinel
19
6
下载
alibaba/Sentinel: Sentinel 是阿里巴巴开源的一款面向分布式服务架构的流量控制、熔断降级组件,提供实时监控、限流、降级和系统保护功能,适用于微服务治理场景。
最近提交(Master分支:4 个月前 )
195150bc
* fix issue 2485 which occur oom when using async servlet request.
* optimize imports
* 1. fix the same issue in the webmvc-v6x
2. improve based on review comments 4 个月前
b78b09d3
4 个月前
更多推荐
已为社区贡献3条内容
所有评论(0)