Skip to content
This repository was archived by the owner on May 30, 2022. It is now read-only.

Commit 682e0ca

Browse files
authored
Cherry pick 601. (#162)
* Consul registry support (#126) * consul registry support * fix cr * Support nacos converter (#130) * Upgrade version * Support nacos address converter * Hystrix switcher (#137) * support enableHystrix * support enableHystrix * hystrix enable * Add nacos client dependency support. (#148) * Zk auth support. (#156) * Server config for rest cors. (#155) * server config for rest cors * add test case for cors
1 parent 64635b8 commit 682e0ca

File tree

19 files changed

+515
-128
lines changed

19 files changed

+515
-128
lines changed

sofa-boot-core/pom.xml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
<cxf.version>3.0.14</cxf.version>
2525
<skipTests>false</skipTests>
2626
<rpc.all.version>5.5.1</rpc.all.version>
27+
<nacos.version>0.6.0</nacos.version>
2728
</properties>
2829

2930
<dependencies>
@@ -175,6 +176,18 @@
175176
<scope>provided</scope>
176177
</dependency>
177178

179+
<!--nacos-->
180+
<dependency>
181+
<groupId>com.alibaba.nacos</groupId>
182+
<artifactId>nacos-api</artifactId>
183+
<version>${nacos.version}</version>
184+
</dependency>
185+
<dependency>
186+
<groupId>com.alibaba.nacos</groupId>
187+
<artifactId>nacos-client</artifactId>
188+
<version>${nacos.version}</version>
189+
</dependency>
190+
178191
<dependency>
179192
<groupId>org.springframework</groupId>
180193
<artifactId>spring-beans</artifactId>
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package com.alipay.sofa.rpc.boot.common;
18+
19+
import java.util.HashMap;
20+
import java.util.Map;
21+
22+
import com.alipay.sofa.rpc.common.utils.StringUtils;
23+
24+
/**
25+
*
26+
* @author JervyShi
27+
* @version $Id: RegistryParseUtil.java, v 0.1 2018-12-03 17:18 JervyShi Exp $$
28+
*/
29+
public class RegistryParseUtil {
30+
31+
/**
32+
* Parse address string.
33+
*
34+
* @param config the config
35+
* @param protocol the protocol
36+
* @return the string
37+
*/
38+
public static String parseAddress(String config, String protocol) {
39+
String address = null;
40+
41+
if (StringUtils.isNotEmpty(config) && config.startsWith(protocol)) {
42+
final String nacosProtocol = protocol + "://";
43+
String value = config.substring(nacosProtocol.length());
44+
if (!value.contains("?")) {
45+
address = value;
46+
} else {
47+
int index = value.lastIndexOf('?');
48+
address = value.substring(0, index);
49+
}
50+
}
51+
52+
return address;
53+
}
54+
55+
/**
56+
* Parse param map.
57+
*
58+
* @param address the address
59+
* @param protocol the protocol
60+
* @return the map
61+
*/
62+
public static Map<String, String> parseParam(String address, String protocol) {
63+
64+
String host = parseAddress(address, protocol);
65+
66+
//for config ?
67+
String paramString = address.substring(address.indexOf(host) + host.length());
68+
69+
if (StringUtils.isNotEmpty(paramString) && paramString.startsWith("?")) {
70+
paramString = paramString.substring(1);
71+
}
72+
73+
Map<String, String> map = new HashMap<String, String>();
74+
if (paramString.contains("&")) {
75+
String[] paramSplit = paramString.split("&");
76+
for (String param : paramSplit) {
77+
Map<String, String> tempMap = parseKeyValue(param);
78+
map.putAll(tempMap);
79+
}
80+
} else {
81+
Map<String, String> tempMap = parseKeyValue(paramString);
82+
map.putAll(tempMap);
83+
}
84+
85+
return map;
86+
}
87+
88+
/**
89+
* Parse key value map.
90+
*
91+
* @param kv the kv
92+
* @return the map
93+
*/
94+
public static Map<String, String> parseKeyValue(String kv) {
95+
Map<String, String> map = new HashMap<String, String>();
96+
if (StringUtils.isNotEmpty(kv)) {
97+
String[] kvSplit = kv.split("=");
98+
String key = kvSplit[0];
99+
String value = kvSplit[1];
100+
map.put(key, value);
101+
}
102+
return map;
103+
}
104+
}
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package com.alipay.sofa.rpc.boot.config;
18+
19+
import com.alipay.sofa.rpc.common.utils.StringUtils;
20+
import com.alipay.sofa.rpc.config.RegistryConfig;
21+
22+
import java.util.HashMap;
23+
import java.util.Map;
24+
25+
/**
26+
* Consul 配置
27+
* <p>
28+
* 配置格式: com.alipay.sofa.rpc.registry.address=consul://xxx:8500
29+
*
30+
* @author <a href="mailto:[email protected]">zhiyuan.lzy</a>
31+
*/
32+
public class ConsulConfigurator implements RegistryConfigureProcessor {
33+
34+
public ConsulConfigurator() {
35+
}
36+
37+
/**
38+
* 解析配置 value
39+
*
40+
* @param config 配置 value
41+
*/
42+
String parseAddress(String config) {
43+
String address = null;
44+
45+
if (StringUtils.isNotEmpty(config) && config.startsWith(SofaBootRpcConfigConstants.REGISTRY_PROTOCOL_CONSUL)) {
46+
final String consulProtocol = SofaBootRpcConfigConstants.REGISTRY_PROTOCOL_CONSUL + "://";
47+
String value = config.substring(consulProtocol.length());
48+
if (!value.contains("?")) {
49+
address = value;
50+
} else {
51+
int index = value.lastIndexOf('?');
52+
address = value.substring(0, index);
53+
}
54+
}
55+
56+
return address;
57+
}
58+
59+
/**
60+
* 传递原始 url
61+
*
62+
* @param address
63+
* @return
64+
*/
65+
public Map<String, String> parseParam(String address) {
66+
67+
String host = parseAddress(address);
68+
69+
//for config ?
70+
String paramString = address.substring(address.indexOf(host) + host.length());
71+
72+
if (StringUtils.isNotEmpty(paramString) && paramString.startsWith("?")) {
73+
paramString = paramString.substring(1);
74+
}
75+
76+
Map<String, String> map = new HashMap<String, String>();
77+
if (paramString.contains("&")) {
78+
String[] paramSplit = paramString.split("&");
79+
for (String param : paramSplit) {
80+
Map<String, String> tempMap = parseKeyValue(param);
81+
map.putAll(tempMap);
82+
}
83+
} else {
84+
Map<String, String> tempMap = parseKeyValue(paramString);
85+
map.putAll(tempMap);
86+
}
87+
88+
return map;
89+
}
90+
91+
private Map<String, String> parseKeyValue(String kv) {
92+
Map<String, String> map = new HashMap<String, String>();
93+
if (StringUtils.isNotEmpty(kv)) {
94+
String[] kvSplit = kv.split("=");
95+
String key = kvSplit[0];
96+
String value = kvSplit[1];
97+
map.put(key, value);
98+
}
99+
return map;
100+
}
101+
102+
@Override
103+
public RegistryConfig buildFromAddress(String address) {
104+
String consulAddress = parseAddress(address);
105+
Map<String, String> map = parseParam(address);
106+
return new RegistryConfig()
107+
.setAddress(consulAddress)
108+
.setProtocol(SofaBootRpcConfigConstants.REGISTRY_PROTOCOL_CONSUL)
109+
.setParameters(map);
110+
111+
}
112+
}

sofa-boot-core/src/main/java/com/alipay/sofa/rpc/boot/config/MeshConfigurator.java

Lines changed: 4 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
*/
1717
package com.alipay.sofa.rpc.boot.config;
1818

19-
import com.alipay.sofa.rpc.common.utils.StringUtils;
19+
import com.alipay.sofa.rpc.boot.common.RegistryParseUtil;
2020
import com.alipay.sofa.rpc.config.RegistryConfig;
2121

2222
/**
@@ -33,33 +33,13 @@ public class MeshConfigurator implements RegistryConfigureProcessor {
3333
public MeshConfigurator() {
3434
}
3535

36-
/**
37-
* 读取配置 key ,获取其 value 进行解析。
38-
*/
39-
public String parseConfig(String config) {
40-
String address = null;
41-
42-
if (StringUtils.isNotEmpty(config) && config.startsWith(SofaBootRpcConfigConstants.REGISTRY_PROTOCOL_MESH)) {
43-
final String meshProtocol = SofaBootRpcConfigConstants.REGISTRY_PROTOCOL_MESH + "://";
44-
String value = config.substring(meshProtocol.length());
45-
if (!value.contains("?")) {
46-
address = value;
47-
} else {
48-
int index = value.lastIndexOf('?');
49-
address = value.substring(0, index);
50-
}
51-
}
52-
53-
return address;
54-
}
55-
5636
@Override
5737
public RegistryConfig buildFromAddress(String address) {
58-
String meshAddress = parseConfig(address);
38+
String meshAddress = RegistryParseUtil.parseAddress(address,
39+
SofaBootRpcConfigConstants.REGISTRY_PROTOCOL_MESH);
5940

6041
meshAddress = HTTP + meshAddress;
61-
return new RegistryConfig()
62-
.setAddress(meshAddress)
42+
return new RegistryConfig().setAddress(meshAddress)
6343
.setProtocol(SofaBootRpcConfigConstants.REGISTRY_PROTOCOL_MESH);
6444
}
6545

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package com.alipay.sofa.rpc.boot.config;
18+
19+
import java.util.Map;
20+
21+
import com.alipay.sofa.rpc.boot.common.RegistryParseUtil;
22+
import com.alipay.sofa.rpc.config.RegistryConfig;
23+
24+
/**
25+
* Nacos 配置
26+
* <p>
27+
* 配置格式: com.alipay.sofa.rpc.registry.address=nacos://xxx:8848?k1=v1
28+
* </p>
29+
*
30+
* @author jervyshi
31+
* @version $Id: NacosConfigurator.java, v 0.1 2018-12-03 15:43 jervyshi Exp $$
32+
*/
33+
public class NacosConfigurator implements RegistryConfigureProcessor {
34+
35+
public NacosConfigurator() {
36+
}
37+
38+
@Override
39+
public RegistryConfig buildFromAddress(String address) {
40+
String nacosAddress = RegistryParseUtil.parseAddress(address,
41+
SofaBootRpcConfigConstants.REGISTRY_PROTOCOL_NACOS);
42+
Map<String, String> map = RegistryParseUtil.parseParam(address,
43+
SofaBootRpcConfigConstants.REGISTRY_PROTOCOL_NACOS);
44+
45+
return new RegistryConfig().setAddress(nacosAddress).setParameters(map)
46+
.setProtocol(SofaBootRpcConfigConstants.REGISTRY_PROTOCOL_NACOS);
47+
}
48+
}

sofa-boot-core/src/main/java/com/alipay/sofa/rpc/boot/config/SofaBootRpcConfigConstants.java

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,14 +44,12 @@ public class SofaBootRpcConfigConstants {
4444

4545
/* registry default configuration */
4646
public static final String REGISTRY_FILE_PATH_DEFAULT = System.getProperty("user.home")
47-
+
48-
System
49-
.getProperty("file.separator") +
50-
"localFileRegistry"
51-
+
52-
System
53-
.getProperty("file.separator") +
54-
"localRegistry.reg";
47+
+ System.getProperty(
48+
"file.separator")
49+
+ "localFileRegistry"
50+
+ System.getProperty(
51+
"file.separator")
52+
+ "localRegistry.reg";
5553

5654
/* possible config value start ********************************************************/
5755

@@ -60,6 +58,11 @@ public class SofaBootRpcConfigConstants {
6058
public static final String REGISTRY_PROTOCOL_ZOOKEEPER = "zookeeper";
6159
public static final String REGISTRY_PROTOCOL_MESH = "mesh";
6260

61+
//@since 5.5.0
62+
public static final String REGISTRY_PROTOCOL_CONSUL = "consul";
63+
64+
public static final String REGISTRY_PROTOCOL_NACOS = "nacos";
65+
6366
/* server */
6467
public static final String RPC_PROTOCOL_BOLT = "bolt";
6568
public static final String RPC_PROTOCOL_REST = "rest";

0 commit comments

Comments
 (0)