-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSentinelDatasourceProperty.java
47 lines (39 loc) · 2.01 KB
/
SentinelDatasourceProperty.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package com.sentinel.client.demo.config;
import com.alibaba.csp.sentinel.datasource.AbstractDataSource;
import com.alibaba.csp.sentinel.datasource.Converter;
import com.alibaba.csp.sentinel.datasource.nacos.NacosDataSource;
import com.alibaba.csp.sentinel.datasource.zookeeper.ZookeeperDataSource;
import com.alibaba.nacos.api.PropertyKeyConst;
import lombok.AllArgsConstructor;
import lombok.Getter;
import java.util.Properties;
import java.util.function.Function;
/**
* sentinel的数据源选择
*/
@Getter
@AllArgsConstructor
public class SentinelDatasourceProperty<T> {
private SentinelClientConfigEntity configEntity;
private Function<SentinelClientConfigEntity.RulePath, String> pathFunc;
private Converter<String, T> parser;
public AbstractDataSource<String, T> buildDataSource() {
return buildDataSourceZookpeer();
// return buildZkSourceNacos();
}
private AbstractDataSource<String, T> buildDataSourceZookpeer() {
SentinelClientConfigEntity.ZkConfig config = this.getConfigEntity().getZkConfig();
Function<SentinelClientConfigEntity.RulePath, String> pathFunc = this.getPathFunc();
return new ZookeeperDataSource<>(config.getAddress(), pathFunc.apply(config.getRulePath()), this.getParser());
}
private AbstractDataSource<String, T> buildZkSourceNacos() {
SentinelClientConfigEntity.NacosConfig config = this.getConfigEntity().getNacosConfig();
Function<SentinelClientConfigEntity.RulePath, String> pathFunc = this.getPathFunc();
Properties properties = new Properties();
properties.setProperty(PropertyKeyConst.SERVER_ADDR, config.getAddress());
properties.setProperty(PropertyKeyConst.NAMESPACE, config.getNamespace());
properties.setProperty(PropertyKeyConst.USERNAME, config.getUsername());
properties.setProperty(PropertyKeyConst.PASSWORD, config.getPassword());
return new NacosDataSource<>(properties, config.getGroupId(), pathFunc.apply(config.getRulePath()), this.getParser());
}
}